Advent of Code 2024 - Day 20Race Condition
| Problem statement | Source code | Tags: Geometry
This problem pays tribute to 2017 day 24 (unimplemented).
The total time of a path that uses a cheat from to can be calculated as:
where is the track length and is the Manhattan distance (since walls are ignored during the cheat). The total time without the cheat is:
So the cheat works whenever .
I first run a "pseudo-BFS" to calculate and for all and . Because there's a single path, I didn't use a queue; I just find the next neighbor that hasn't been visited yet. The results are a distances matrix recording for each , and a path list of all points along the path. can be derived by since the path is linear.
For each point along the path, we can either use the cheat or not. When using a cheat, we can jump to any point that is within the radius of 2 (or 20 for part 2) measured in Manhattan distance. I just iterate over all points in this range, verifying that .