Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2024 Day 1
- Advent of Code 2024 Day 2
- Advent of Code 2024 Day 3
- Advent of Code 2024 Day 4
- Advent of Code 2024 Day 5
- Advent of Code 2024 Day 6
- Advent of Code 2024 Day 7
- Advent of Code 2024 Day 8
- Advent of Code 2024 Day 9
- Advent of Code 2024 Day 10
- Advent of Code 2024 Day 11
- Advent of Code 2024 Day 12
- Advent of Code 2024 Day 13
- Advent of Code 2024 Day 14
- Advent of Code 2024 Day 15
- Advent of Code 2024 Day 16
- Advent of Code 2024 Day 17
- Advent of Code 2024 Day 18
- Advent of Code 2024 Day 19
- Advent of Code 2024 Day 20
-
Older post
-
Newer post
Advent of Code 2024 Day 10
Day 10: Hoof It
https://adventofcode.com/2024/day/10
Another day, another familiar location.
Today’s input is a map of the surrounding area.
An example input looks like this:
Each number is the height of that point on the map (from 0 to 9).
So the location at row index 0 and column index 0 has a height of 8.
Parsing
Today is a day again where I could have done without any data structures.
But I used one anyway, because I like typing .row
instead of .0
or .x
to get at the row index of a point.
The structure I parsed the input into is also not the most efficient way to store this map (that would probably be a 1D-list). But it works perfectly and is nice to work with.
I represented the input map as a map where each key is a Point
, and each value is a height.
As in a previous day, I made sure to remember the amount of rows and the amount of columns.
Part 1
The goal is to go hiking and find a good trail.
A trail:
- starts at 0
- ends at 9
- goes up by 1 each step
- only has steps in 4 directions (up, right, down, left)
Each trailhead (a height 0 point that starts a trail) has a score.
It’s the amount of 9
-height points that are reachable from it.
The question asks what the sum of the score for all trailheads is.
So, a pathfinding day! Time to break out the bfs!
Helpers
First, some helpers!
I reused some of the logic for Point
from a previous day.
Collecting the logic about working with Point
s there lets me keep the final solving code nice and tidy.
Part 1 code
Starting at every point of height 0, I did a search and incremented a sum each time I saw a unique height 9 point.
Those points are unique because I keep track of every location to visit during the search in a set, and only push points to visit onto the queue when it has not already been marked to be visited previously.
Part 2
Oops, that was wrong! Multiple trails end in the same 9 height tile, and they should all be counted.
The question asks what the sum of the amount of paths is.
For me, this meant deleting the set that prevent going to spots we already visited. This results in all trails being walked.
Final code
I combined the logic from part1 and part2. Keeping track of all reached nines, and deduplicating them by putting them in a set for part1.