Solution to the 10th problem

Small device detected! While I've tried to make an effort to make the visualization of this problem as responsive as possible, I strongly recommend viewing this visualization in larger screens, or in horizontal mode!
This visualization is not adapted to small devices and the output may not be correctly visualized. The recommended minimum width for this visualization is 1024px.

The problem is solved by performing a recursive depth-first search (DFS) from each trailhead (height 0) across the topographic map. A valid hiking trail always moves orthogonally to neighboring positions with a height exactly 1 higher than the current position.

Problem A

For part A, the DFS keeps track of visited positions to avoid counting the same path multiple times. Whenever a trail reaches a height of 9, it contributes to the trailhead’s score. By summing these scores over all trailheads, the solution for the first part is obtained.

» cargo build -p aoc-24 --release && hyperfine ./target/release/aoc-24 -N --warmup 5
Benchmark 1: ./target/release/aoc-24
Time (mean ± σ):       6.2 ms ±   3.8 ms    [User: 1.4 ms, System: 1.1 ms]
Range (min  max):     3.7 ms …  37.4 ms    532 runs

Problem B

For part B, the visited tracking is disabled so that all distinct hiking trails are counted, even if they overlap at intermediate positions. The recursive DFS explores every valid path from each trailhead, incrementing the count whenever a trail reaches height 9. Summing these counts across all trailheads gives the total rating.

» cargo build -p aoc-24 --release && hyperfine ./target/release/aoc-24 -N --warmup 5
Benchmark 1: ./target/release/aoc-24
Time (mean ± σ):       8.2 ms ±   5.7 ms    [User: 1.4 ms, System: 1.2 ms]
Range (min  max):     4.1 ms …  40.7 ms    455 runs

This approach efficiently handles both parts of the problem by simply toggling whether visited positions are tracked, avoiding the need for more complex state management or memoization while remaining fast even for larger maps.

Visualization

Configuration

50ms
Iterative

Trails found: 0

Checking points (0):