You and The Historians look a lot more pixelated than you remember. You're inside a computer at the North Pole!
Just as you're about to check out your surroundings, a program runs up to you. "This region of memory isn't safe! The User misunderstood what a pushdown automaton is and their algorithm is pushing whole bytes down on top of us! Run!"
The algorithm is fast - it's going to cause a byte to fall into your memory space once every nanosecond! Fortunately, you're faster, and by quickly scanning the algorithm, you create a list of which bytes will fall (your puzzle input) in the order they'll land in your memory space.
Your memory space is a two-dimensional grid with coordinates that range from 0
to 70
both horizontally and vertically. However, for the sake of example, suppose you're on a smaller grid with coordinates that range from 0
to 6
and the following list of incoming byte positions:
123456789101112131415161718192021222324255,4
4,2
4,5
3,0
2,1
6,3
2,4
1,5
0,6
3,3
2,6
5,1
1,2
5,5
2,5
6,5
1,4
0,4
6,4
1,1
6,1
1,0
0,5
1,6
2,0
Each byte position is given as an X,Y
coordinate, where X
is the distance from the left edge of your memory space and Y
is the distance from the top edge of your memory space.
You and The Historians are currently in the top left corner of the memory space (at 0,0
) and need to reach the exit in the bottom right corner (at 70,70
in your memory space, but at 6,6
in this example). You'll need to simulate the falling bytes to plan out where it will be safe to run; for now, simulate just the first few bytes falling into your memory space.
As bytes fall into your memory space, they make that coordinate corrupted. Corrupted memory coordinates cannot be entered by you or The Historians, so you'll need to plan your route carefully. You also cannot leave the boundaries of the memory space; your only hope is to reach the exit.
In the above example, if you were to draw the memory space after the first 12
bytes have fallen (using .
for safe and #
for corrupted), it would look like this:
1234567...#...
..#..#.
....#..
...#..#
..#..#.
.#..#..
#.#....
You can take steps up, down, left, or right. After just 12 bytes have corrupted locations in your memory space, the shortest path from the top left corner to the exit would take 22
steps. Here (marked with O
) is one such path:
1234567OO.#OOO
.O#OO#O
.OOO#OO
...#OO#
..#OO#.
.#.O#..
#.#OOOO
Simulate the first kilobyte (1024
bytes) falling onto your memory space. Afterward, what is the minimum number of steps needed to reach the exit?
Create a map using the first 1024
values. Find a path between the start and finish.
I used zod
to turn the input values into structured data:
1234567const parser = z.array(
z
.string()
.transform((value) => value.split(","))
.pipe(z.tuple([z.coerce.number(), z.coerce.number()]))
);
const values = parser.parse(input.split("\n"));
Then created a map with code nearly identical to Advent of Code 2024 Day 16, though this is a bit similar (as we don't need special consideration for turns). If you'd like an overview of the code, check out that explanation.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173interface Position {
y: number;
x: number;
}
type StrPosition = `${string},${string}`;
interface NextPosition extends Position {
score: number;
prevPositions: Set<StrPosition>;
}
interface ScoredPositions {
score: number;
positions: Array<Position>;
}
class Map {
start: Position;
end: Position;
matrix: Array<Array<string>>;
scoredMatrix: Array<Array<ScoredPositions>>;
searching: boolean;
positionsToCheck: Array<NextPosition>;
bestPositions: Set<StrPosition>;
constructor(bytes: number) {
this.start = { y: 0, x: 0 };
this.end = { y: HEIGHT, x: WIDTH };
this.matrix = Array.from(Array(HEIGHT), () => new Array(WIDTH).fill("."));
this.scoredMatrix = this.matrix.map((row) => {
return row.map(() => {
return { score: Infinity, positions: [] };
});
});
this.matrix[this.start.y]![this.start.x] = "S";
this.matrix[this.end.y - 1]![this.end.x - 1] = "E";
for (let i = 0; i < bytes; i += 1) {
const coordinate = values[i];
if (!coordinate) {
throw new Error(`No coordinate at ${i}`);
}
const [x, y] = coordinate;
this.matrix[y]![x] = "#";
}
this.searching = true;
// Start with the start position
this.positionsToCheck = [
{
// Right
y: this.start.y,
x: this.start.x + 1,
score: 0,
prevPositions: new Set(),
},
{
// Down
y: this.start.y + 1,
x: this.start.x,
score: 0,
prevPositions: new Set(),
},
];
this.bestPositions = new Set();
}
at({ y, x }: Position) {
return this.matrix[y]?.[x];
}
addPositionToCheck(position: NextPosition) {
// Skip walls
const tile = this.at(position);
if (tile === undefined || tile === "#") return;
const { y, x, score } = position;
// Add score
const existingScore = this.scoredMatrix[y]![x]!.score;
if (score < existingScore) {
// Prune paths
this.scoredMatrix[y]![x]!.score = score;
this.scoredMatrix[y]![x]!.positions = [];
this.scoredMatrix[y]![x]!.positions.push({ y, x });
this.positionsToCheck.push(position);
}
}
step() {
// Get a position to check
const position = this.positionsToCheck.shift();
if (!position) {
throw new Error("No positions left to check");
}
const tile = this.at(position);
// Hit a wall, early exit
if (tile === "#") return;
if (tile === "E") {
// Hit the end
position.prevPositions.forEach((prevPosition) => {
this.bestPositions.add(prevPosition);
});
this.searching = false;
return;
}
// this.exploreMatrix[position.y]![position.x] = directionCode[position.direction];
const strPosition: StrPosition = `${position.y},${position.x}`;
// Left
this.addPositionToCheck({
y: position.y,
x: position.x - 1,
score: position.score + 1,
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
// Right
this.addPositionToCheck({
y: position.y,
x: position.x + 1,
score: position.score + 1,
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
// Up
this.addPositionToCheck({
y: position.y - 1,
x: position.x,
score: position.score + 1,
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
// Down
this.addPositionToCheck({
y: position.y + 1,
x: position.x,
score: position.score + 1,
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
// Resort the positions by lowest score
this.positionsToCheck.sort((a, b) => a.score - b.score);
}
get score() {
if (this.bestPositions.size === 0) return 0;
// +1 to include end
return this.bestPositions.size + 1;
}
}
The Historians aren't as used to moving around in this pixelated universe as you are. You're afraid they're not going to be fast enough to make it to the exit before the path is completely blocked.
To determine how fast everyone needs to go, you need to determine the first byte that will cut off the path to the exit.
In the above example, after the byte at 1,1
falls, there is still a path to the exit:
1234567O..#OOO
O##OO#O
O#OO#OO
OOO#OO#
###OO##
.##O###
#.#OOOO
However, after adding the very next byte (at 6,1
), there is no longer a path to the exit:
1234567...#...
.##..##
.#..#..
...#..#
###..##
.##.###
#.#....
So, in this example, the coordinates of the first byte that prevents the exit from being reachable are 6,1
.
Simulate more of the bytes that are about to corrupt your memory space. What are the coordinates of the first byte that will prevent the exit from being reachable from your starting position? (Provide the answer as two integers separated by a comma with no other characters.)
The map code is nearly identical to Part 1.
Starting at 1024 bytes
, create a map with the first {bytes}
coordinates. Find a path from the start to the end through the map. If no such path exists, we know the blocking byte. If a path exists, increment bytes
and try again.
Part 1 Time | Part 1 Rank | Part 2 Time | Part 2 Rank |
---|---|---|---|
00:27:30 | 3,197 | 00:45:44 | 3,641 |
This problem was trivial after Day 16. I took it easy and still finished fairly quickly.