Author: Honza Pokorny <honza@pokorny.ca>
2021: Add day 1
2021/day-01/src/main.rs | 22 +++++++++++++++++++---
diff --git a/2021/day-01/src/main.rs b/2021/day-01/src/main.rs index e2a4a1f81bfd26b1a9732484f41d161858a3ea24..c11ed9680031355cf121fe5bf4ff14748771c243 100644 --- a/2021/day-01/src/main.rs +++ b/2021/day-01/src/main.rs @@ -5,9 +5,25 @@ fn main() -> io::Result<()> { let stdin = io::stdin(); let lines = stdin.lock().lines(); - for line in lines { - println!("{:?}", line?); - } + let nums: Vec<i32> = lines.map(|x| x.unwrap().parse().unwrap()).collect(); + let nums2 = nums.clone(); + + // Part 1: size is 1 + // Part 2: size is 3 + let window_size = 3; + + let niter = nums.windows(window_size); + let niter2 = nums2[1..nums2.len() - 1].windows(window_size); + + let res = niter.zip(niter2).fold(1, |acc, (x, y)| { + if y.iter().sum::<i32>() > x.iter().sum::<i32>() { + acc + 1 + } else { + acc + } + }); + + println!("{:?}", res); Ok(()) }