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>) -> usize {
15    toboggan(input, 3, 1)
16}
17
18pub fn part2(input: &Grid<u8>) -> usize {
19    [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
20        .into_iter()
21        .map(|(dx, dy)| toboggan(input, dx, dy))
22        .product()
23}
24
25fn toboggan(grid: &Grid<u8>, dx: i32, dy: i32) -> usize {
26    (0..grid.height / dy)
27        .filter(|&i| {
28            let point = Point::new((i * dx) % grid.width, i * dy);
29            grid[point] == b'#'
30        })
31        .count()
32}