summaryrefslogtreecommitdiffstats
path: root/src/bin/2021/day1.rs
blob: fce763cc5e52579b4aaa60039199e08e755d1202 (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
use advent_of_code::prelude::*;

pub fn parse(fh: File) -> Result<Vec<i64>> {
    Ok(parse::lines(fh).collect())
}

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

pub fn part2(ints: Vec<i64>) -> Result<usize> {
    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())
}

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