Day 18: RAM Run

2024-12-18

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
5,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:

1
2
3
4
5
6
7
...#... ..#..#. ....#.. ...#..# ..#..#. .#..#.. #.#....

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:

1
2
3
4
5
6
7
OO.#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?

Progress: 0
Score: 0

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:

1
2
3
4
5
6
7
const 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
interface 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:

1
2
3
4
5
6
7
O..#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:

1
2
3
4
5
6
7
...#... .##..## .#..#.. ...#..# ###..## .##.### #.#....

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.)

Progress: 0
Bytes: 1024
Blocking Coordinate: Still searching...

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 TimePart 1 RankPart 2 TimePart 2 Rank
00:27:303,19700:45:443,641

This problem was trivial after Day 16. I took it easy and still finished fairly quickly.