Skip to content

Commit 687f5da

Browse files
committed
2024/16
1 parent 92d1ddd commit 687f5da

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

2024/Day16/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ It's time again for the [Reindeer Olympics](/2015/day/14)! This year, the big ev
44
You and The Historians arrive to search for the Chief right as the event is about to start. It wouldn't hurt to watch a little, right?
55

66
_Visit the website for the full story and [full puzzle](https://adventofcode.com/2024/day/16) description._
7+
8+
I spent hell a lot of time on this one. I’m not sure why, because I had a good understanding of what to do for both parts. `Part 1` went reasonably well: I quickly implemented a priority queue-based approach to find the shortest path from the `start` state to the `goal`.
9+
10+
For `Part 2`, I initially tried a few dead ends, but I found the right direction after about half an hour. The idea is to split the problem into two halves. First, we compute the optimal distances from every tile and direction to the goal node. This can be found in the `DistancesTo` function below. It is similar to how I implemented Part 1, using a priority queue, but I needed to walk backwards instead of forward.
11+
12+
Once I have the distances, I can start an other round, now working forward from the start position and using a flood-fill-like algorithm to discover the optimal positions. This is easy to do with the distance map as a guide. I can maintain the remaining score along the path, and I just need to check if the distance from a potential next state equals to the score I still have to use. This logic can be found in the `FindBestSpots` function.

2024/Day16/Solution.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ int FindBestSpots(Map map) {
3636
q.Enqueue(start, dist[start]);
3737
var bestSpots = new HashSet<State> { start };
3838

39-
while (q.TryDequeue(out var state, out var totalScore)) {
39+
while (q.TryDequeue(out var state, out var remainingScore)) {
4040
foreach (var (next, score) in Steps(map, state, forward: true)) {
4141
if (bestSpots.Contains(next)) {
4242
continue;
4343
}
44-
45-
if (dist[next] + score == totalScore) {
44+
var nextScore = remainingScore - score;
45+
if (dist[next] == nextScore) {
4646
bestSpots.Add(next);
47-
q.Enqueue(next, dist[next]);
47+
q.Enqueue(next, nextScore);
4848
}
4949
}
5050
}

0 commit comments

Comments
 (0)