summaryrefslogtreecommitdiffstats
path: root/src/2020/3/mod.rs
blob: 03ddfce74ec622597c6b48cfd502a18125aaa748 (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
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
use crate::util::grid::*;

pub struct Map {
    grid: Grid<bool>,
}

impl Map {
    fn new(grid: Grid<bool>) -> Self {
        Self { grid }
    }

    fn rows(&self) -> usize {
        self.grid.rows().0
    }

    fn tree_at(&self, row: Row, col: Col) -> anyhow::Result<bool> {
        // unwrap safe because cycle().nth() can never fail
        Ok(*self.grid[row].iter().cycle().nth(col.0).unwrap())
    }

    fn trees_for_slope(
        &self,
        row_incr: usize,
        col_incr: usize,
    ) -> anyhow::Result<i64> {
        let mut trees = 0;
        for r in 0..self.rows() / row_incr {
            let row = r * row_incr;
            let col = r * col_incr;
            if self.tree_at(Row(row), Col(col))? {
                trees += 1;
            }
        }
        Ok(trees)
    }
}

pub fn parse(fh: std::fs::File) -> anyhow::Result<Map> {
    Ok(Map::new(crate::util::parse::bool_grid(
        crate::util::parse::lines(fh),
        b'#',
        b'.',
    )))
}

pub fn part1(map: Map) -> anyhow::Result<i64> {
    map.trees_for_slope(1, 3)
}

pub fn part2(map: Map) -> anyhow::Result<i64> {
    Ok(map.trees_for_slope(1, 1)?
        * map.trees_for_slope(1, 3)?
        * map.trees_for_slope(1, 5)?
        * map.trees_for_slope(1, 7)?
        * map.trees_for_slope(2, 1)?)
}

#[test]
fn test() {
    assert_eq!(
        part1(parse(crate::util::data(2020, 3).unwrap()).unwrap()).unwrap(),
        292
    );
    assert_eq!(
        part2(parse(crate::util::data(2020, 3).unwrap()).unwrap()).unwrap(),
        9354744432
    );
}