aoc/year2020/
day03.rs

1//! # Toboggan Trajectory
2//!
3//! Two dimensional grids of ASCII characters are a common Advent of Code theme,
4//! so we use our utility [`Grid`] class to parse the data.
5//!
6//! [`Grid`]: crate::util::grid
7use crate::util::grid::*;
8use crate::util::point::*;
9
10pub fn parse(input: &str) -> Grid<u8> {
11    Grid::parse(input)
12}
13
14pub fn part1(input: &Grid<u8>) -> u64 {
15    toboggan(input, 3, 1)
16}
17
18pub fn part2(input: &Grid<u8>) -> u64 {
19    toboggan(input, 1, 1)
20        * toboggan(input, 3, 1)
21        * toboggan(input, 5, 1)
22        * toboggan(input, 7, 1)
23        * toboggan(input, 1, 2)
24}
25
26fn toboggan(grid: &Grid<u8>, dx: i32, dy: i32) -> u64 {
27    let mut point = ORIGIN;
28    let mut trees = 0;
29
30    while point.y < grid.height {
31        if grid[point] == b'#' {
32            trees += 1;
33        }
34        point.x = (point.x + dx) % grid.width;
35        point.y += dy;
36    }
37
38    trees
39}