summaryrefslogtreecommitdiffstats
path: root/src/2021/1/mod.rs
blob: be0327539dbbdd05de795cbefc97dcc55932ba26 (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
pub fn part1() -> anyhow::Result<i64> {
    Ok(data_ints!()?
        .collect::<Vec<_>>()
        .windows(2)
        .map(|a| a[1] - a[0])
        .filter(|x| *x > 0)
        .count()
        .try_into()?)
}

pub fn part2() -> anyhow::Result<i64> {
    Ok(data_ints!()?
        .collect::<Vec<_>>()
        .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().unwrap(), 1602);
    assert_eq!(part2().unwrap(), 1633);
}