Rust challenge 31/100 - advent of code 2021 day 1 part 1 & 2
Table of content
What is this
The rules of the game are explained in my original post.
31st Challenge
Challenge
Today I’m solving today’s AoC day 1 2021. It took me longer than it I would have liked.
I wanted to do it quickly with a filter
or fold
function, which didn’t work. I ran out of time and had to leave for work .
Solution
I peaked at the python solution on which used the zip
function I had not come across before. Basically
it makes a tuple out of two iterators. Using this as well as multizip
I was able to implement the functions.
use itertools::multizip;
fn main() {
let a= INPUT.lines()
.map(|x|{x.parse().unwrap()})
.collect::<Vec<u32>>();
let c:Vec<u32> = multizip((a.iter(),a.iter().skip(1),a.iter().skip(2)))
.map(|x|{x.0+x.1+x.2})
.collect();
println!("sol1:{}, sol2:{}",count_bigger(&a),count_bigger(&c));
}
fn count_bigger(a: &Vec<u32>) -> usize {
a.iter()
.zip(a.iter().skip(1))
.filter(|x| { x.0 < x.1 })
.count()
}
const INPUT:&str = "199
200
208
210
200
207
240
269
260
263";
To see the full source see github and see the live demo in playground.