aoc

ref: 272d6bd60122c8e81369c4b0c0b141502b77ebb5

2021/day-01/src/main.rs


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::io;
use std::io::prelude::*;

fn main() -> io::Result<()> {
    let stdin = io::stdin();
    let lines = stdin.lock().lines();

    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(())
}