Advent of Code Day 10: Hoof It
This one (like many other it seems) managed to inadvertently solve Part 2 whilst doing Part 1, or at least Part 2 required very little extra effort.
Summary of Problem & Solution
I think where many of us devs went wrong today as we didn't fully understand the requirement of :
A trailhead's score is the number of 9-height positions reachable from that trailhead via a hiking trail.
But this could be misinterpreted as all the 9's from that trailhead not unique. However, alas it should be unique 9's, but it made for an easy solution to Part 2 (which is why many many quick Part2 solution times haha)
Part 1: Unique Paths to 9
The goal is to count how many unique 9 positions can be reached from each trailhead (0). This involves:
Finding all reachable 9 positions from each trailhead.
Using a HashSet
of visited 9 positions (visitedEndPoints) to ensure uniqueness.
For each trailhead:
Traverse valid paths using the BFS (Breadth First Search) algorithm.
Keep track of all 9 positions reached from that trailhead (unique).
Finally:
Sum up the counts of unique 9 positions reachable from all trailheads.
This forms the "score" of each trailhead.
Part 2: Count All Distinct Paths
Here, the focus shifts to counting the number hiking trails from each trailhead. This involves:
Counting all possible paths (not just unique endpoints).
Problem Solved - As always feel free to give me a follow on Twitter where you can be notified of my articles on DevTo, FreeCodeCamp and other dev tips and tricks.
Top comments (0)