Skip to main content

aoc/year2024/
day10.rs

1//! # Hoof It
2//!
3//! [Depth first search](https://en.wikipedia.org/wiki/Depth-first_search) for both parts.
4//! Part two is simpler than part one as we don't need to keep track of already visited points.
5//! Reverse search was slightly faster as my input contained fewer peaks `9` than valleys `0`.
6use crate::util::grid::*;
7use crate::util::point::*;
8
9pub fn parse(input: &str) -> Grid<u8> {
10    // A newline border allows us to avoid boundary checks.
11    Grid::parse_with_border(input)
12}
13
14pub fn part1(grid: &Grid<u8>) -> u32 {
15    solve(grid, false)
16}
17
18pub fn part2(grid: &Grid<u8>) -> u32 {
19    solve(grid, true)
20}
21
22fn solve(grid: &Grid<u8>, distinct: bool) -> u32 {
23    let mut result = 0;
24    let mut seen = grid.same_size_with(-1);
25
26    for point in grid.points() {
27        if grid[point] == b'9' {
28            let id = point.y * grid.width + point.x;
29            result += dfs(grid, distinct, &mut seen, id, point);
30        }
31    }
32
33    result
34}
35
36fn dfs(grid: &Grid<u8>, distinct: bool, seen: &mut Grid<i32>, id: i32, point: Point) -> u32 {
37    let mut result = 0;
38
39    for next in ORTHOGONAL.map(|o| point + o) {
40        if grid[next] + 1 == grid[point] && (distinct || seen[next] != id) {
41            seen[next] = id;
42            result += if grid[next] == b'0' { 1 } else { dfs(grid, distinct, seen, id, next) };
43        }
44    }
45
46    result
47}