-
Notifications
You must be signed in to change notification settings - Fork 5
/
day03.rs
39 lines (33 loc) · 891 Bytes
/
day03.rs
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
//! # Toboggan Trajectory
//!
//! Two dimensional grids of ASCII characters are a common Advent of Code theme,
//! so we use our utility [`Grid`] class to parse the data.
//!
//! [`Grid`]: crate::util::grid
use crate::util::grid::*;
use crate::util::point::*;
pub fn parse(input: &str) -> Grid<u8> {
Grid::parse(input)
}
pub fn part1(input: &Grid<u8>) -> u64 {
toboggan(input, 3, 1)
}
pub fn part2(input: &Grid<u8>) -> u64 {
toboggan(input, 1, 1)
* toboggan(input, 3, 1)
* toboggan(input, 5, 1)
* toboggan(input, 7, 1)
* toboggan(input, 1, 2)
}
fn toboggan(grid: &Grid<u8>, dx: i32, dy: i32) -> u64 {
let mut point = ORIGIN;
let mut trees = 0;
while point.y < grid.height {
if grid[point] == b'#' {
trees += 1;
}
point.x = (point.x + dx) % grid.width;
point.y += dy;
}
trees
}