summaryrefslogtreecommitdiffstats
path: root/src/2021/1/mod.rs
blob: 3ba61e21a17416ddaaa1f589725e8a0a77c6ea68 (plain) (blame)
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
30
31
32
33
34
35
36
pub fn parse(fh: std::fs::File) -> anyhow::Result<Vec<i64>> {
    Ok(crate::util::parse::ints(crate::util::parse::lines(fh)).collect())
}

pub fn part1(ints: Vec<i64>) -> anyhow::Result<i64> {
    Ok(ints
        .windows(2)
        .map(|a| a[1] - a[0])
        .filter(|x| *x > 0)
        .count()
        .try_into()?)
}

pub fn part2(ints: Vec<i64>) -> anyhow::Result<i64> {
    Ok(ints
        .windows(3)
        .map(|a| a[0] + a[1] + a[2])
        .collect::<Vec<_>>()
        .windows(2)
        .map(|a| a[1] - a[0])
        .filter(|x| *x > 0)
        .count()
        .try_into()?)
}

#[test]
fn test() {
    assert_eq!(
        part1(parse(crate::util::data(2021, 1).unwrap()).unwrap()).unwrap(),
        1602
    );
    assert_eq!(
        part2(parse(crate::util::data(2021, 1).unwrap()).unwrap()).unwrap(),
        1633
    );
}