AoC# 2024 - Day 10: Hoof It
It’s Advent of Code time 🎄
Advent of Code is an Advent calendar of small programming puzzles for a variety of skill levels that can be solved in any programming language you like.
Day 10 looked like a maze-type solving problem: we need some code to walk through it…
Part 1 ⭐️
After a brief refresher of maze solving and then tree traversing algorithms, I realised the puzzle input is a tree: we have a starting node, and any number of routes or dead-ends to an ending node.
I thought a breadth-first search should do the trick, and having built a list of all possible trails it was straightforward to get the answer:
// trailheads is a list of all complete (0-9) trails from a single starting point
result += trailheads.DistinctBy(t => t.End).Count();
Part 2 ⭐️
It’s really satisfying when you just need a tweak to Part 1 to get the answer to Part 2:
// trailheads is the same list of all complete (0-9) trails from a single starting point
result += trailheads
.AggregateBy(t => t.End, 0, (sum, t) => sum + 1)
.Sum(agg => agg.Value);
Lovely 🥹