Another day, another familiar sensation.
This time, you’re inside a computer again, near the CPU.
There is a race being held, on a … drumroll please … 2D-grid!
An example input looks like this:
#: wall
.: empty
S: start, also empty
E: end, also empty
There is only one shortest route from the start to the end.
You can go in the 4 main directions.
Each step costs 1
Parsing
Let’s get the Point and Tile out again.
Return a Point for start, one for end, and the grid as a map from Points to Tiles.
Again, this is not the most efficient way to store the grid, but one I like a lot.
Part 1
Because there is only one shortest route, all contestant would take it, making the race boring.
Contestants are allowed to cheat once.
For a distance of 2 they can turn off their collision detection, this can be done once per race.
They must be on an empty space again after that second step.
In other words, in a 2D-grid, the manhattan distance between the start and endpoint of their cheat must be 2.
It does not matter which route contestants take during a cheat, a cheat is only identified by its start and end position.
The question asks how many cheats would save at least 100 steps.
Some skeleton/pseudo-code I want to work towards:
In that skeleton, I never use the starting or ending points.
I need to build up a map of minimum costs to reach a location from the start,
then I can use that map to calculate how much would be saved at each step of the loop over pairs above.
Helpers
The Point struct got some familiar methods on it, and a new one to calculate the Manhattan distance.
Next up, building the map of minimum costs to reach a Point when starting from start.
That means that at the end of this helper, only reachable points will be in this map.