summaryrefslogtreecommitdiff
path: root/2021/src/bin/day_1.rs
blob: 62ab04598367078108e4cb1ff08b592afead17f6 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use nom::{
    character::complete::{line_ending, u64 as nom_u64},
    combinator::map,
    multi::separated_list1,
    IResult,
};
use std::fs;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input = fs::read_to_string("inputs/day_1.txt")?;
    let sonar_scan = parse_sonar_scan(&input).unwrap().1;

    let simple_increase_counter = count_increases(sonar_scan.iter());
    dbg!(simple_increase_counter);

    let windowed_sonar_scan = sonar_scan
        .iter()
        .zip(sonar_scan.iter().skip(1))
        .zip(sonar_scan.iter().skip(2))
        .map(|((depth1, depth2), depth3)| ThreeDepthWindowSum::new([*depth1, *depth2, *depth3]))
        .collect::<Vec<_>>();

    let windowed_increase_counter = count_increases(windowed_sonar_scan.iter());
    dbg!(windowed_increase_counter);

    Ok(())
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
struct Depth(u64);

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
struct ThreeDepthWindowSum(u64);

impl ThreeDepthWindowSum {
    fn new(depths: [Depth; 3]) -> ThreeDepthWindowSum {
        ThreeDepthWindowSum(depths.into_iter().map(|d| d.0).sum())
    }
}

fn parse_sonar_scan(input: &str) -> IResult<&str, Vec<Depth>> {
    separated_list1(line_ending, parse_depth)(input)
}

fn parse_depth(input: &str) -> IResult<&str, Depth> {
    map(nom_u64, Depth)(input)
}

#[derive(Default, Debug)]
struct DepthIncreaseCounter(u64);

impl DepthIncreaseCounter {
    fn increment(&mut self) {
        self.0 += 1;
    }
}

fn count_increases<T: Ord>(i: impl IntoIterator<Item = T> + Clone) -> DepthIncreaseCounter {
    let mut increases = DepthIncreaseCounter::default();
    for (depth, next_depth) in i.clone().into_iter().zip(i.into_iter().skip(1)) {
        if next_depth > depth {
            increases.increment();
        }
    }
    increases
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn parses_a_depth() {
        assert_eq!(parse_depth("96\n"), Ok(("\n", Depth(96))));
    }
}