Another day, another familiar sensation.
This time, you’re inside a computer again.
Some algorithm is corrupting bytes in the memory where you are, one byte at a time.
That’s today’s input, an ordered list of byte locations the algorithm is going to corrupt.
When a position becomes corrupt, you can no longer be at that location.
An example input looks like this:
Each line is a location, in a col_idx,row_idx form.
Wait a minute, this is another 2D-grid problem!
Parsing
Alright, you might already know the drill, time for a Point.
Then turn the input into a list of them.
Part 1
You need to get out of this place while you still can.
The grid you are in is 71 by 71 in size (the problem mentions 70, but those are 0-indexed numbers!).
You start at row: 0, col: 0
The goal is at row: 70, col: 70
The question asks what the minimum amount of steps is to reach the goal after 1024 positions have become corrupted.
Each step you can move in the 4 directions if that location isn’t corrupted.
This is a breadth-first-search again, so I used some of the helpers from previous days.
Helpers
Code
Breadth-first-search, where each item in the queue is a (point, score) pair, and the score increases by 1 each step.
Point neighbours are only considered if they are not corrupted.
Before starting the search, I create a set of the first 1024 points in the input that become corrupted.
Part 2
The historians want to know how slow they can go before a byte is corrupted that makes it impossible to reach the end from the start.
I refactored my code from part1 a bit so the search logic is its own function that either returns a cost, or nothing.
If it returns nothing, there is no possible path from start to finish.
The helper takes in a set of locations that are corrupted.
I then used that helper and performed a binary search on the list of input positions.
At some position, crossing from start to end becomes impossible.