Day 20: Race Condition

2024-12-20

The Historians are quite pixelated again. This time, a massive, black building looms over you - you're right outside the CPU!

While The Historians get to work, a nearby program sees that you're idle and challenges you to a race. Apparently, you've arrived just in time for the frequently-held race condition festival!

The race takes place on a particularly long and twisting code path; programs compete to see who can finish in the fewest picoseconds. The winner even gets their very own mutex!

They hand you a map of the racetrack (your puzzle input). For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
############### #...#...#.....# #.#.#.#.#.###.# #S#...#.#.#...# #######.#.#.### #######.#.#...# #######.#.###.# ###..E#...#...# ###.#######.### #...###...#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############

The map consists of track (.) - including the start (S) and end (E) positions (both of which also count as track) - and walls (#).

When a program runs through the racetrack, it starts at the start position. Then, it is allowed to move up, down, left, or right; each such move takes 1 picosecond. The goal is to reach the end position as quickly as possible. In this example racetrack, the fastest time is 84 picoseconds.

Because there is only a single path from the start to the end and the programs all go the same speed, the races used to be pretty boring. To make things more interesting, they introduced a new rule to the races: programs are allowed to cheat.

The rules for cheating are very strict. Exactly once during a race, a program may disable collision for up to 2 picoseconds. This allows the program to pass through walls as if they were regular track. At the end of the cheat, the program must be back on normal track again; otherwise, it will receive a segmentation fault and get disqualified.

So, a program could complete the course in 72 picoseconds (saving 12 picoseconds) by cheating for the two moves marked 1 and 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
############### #...#...12....# #.#.#.#.#.###.# #S#...#.#.#...# #######.#.#.### #######.#.#...# #######.#.###.# ###..E#...#...# ###.#######.### #...###...#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############

Or, a program could complete the course in 64 picoseconds (saving 20 picoseconds) by cheating for the two moves marked 1 and 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
############### #...#...#.....# #.#.#.#.#.###.# #S#...#.#.#...# #######.#.#.### #######.#.#...# #######.#.###.# ###..E#...12..# ###.#######.### #...###...#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############

This cheat saves 38 picoseconds:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
############### #...#...#.....# #.#.#.#.#.###.# #S#...#.#.#...# #######.#.#.### #######.#.#...# #######.#.###.# ###..E#...#...# ###.####1##.### #...###.2.#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############

This cheat saves 64 picoseconds and takes the program directly to the end:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
############### #...#...#.....# #.#.#.#.#.###.# #S#...#.#.#...# #######.#.#.### #######.#.#...# #######.#.###.# ###..21...#...# ###.#######.### #...###...#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############

Each cheat has a distinct start position (the position where the cheat is activated, just before the first move that is allowed to go through walls) and end position; cheats are uniquely identified by their start position and end position.

In this example, the total number of cheats (grouped by the amount of time they save) are as follows:

  • There are 14 cheats that save 2 picoseconds.
  • There are 14 cheats that save 4 picoseconds.
  • There are 2 cheats that save 6 picoseconds.
  • There are 4 cheats that save 8 picoseconds.
  • There are 2 cheats that save 10 picoseconds.
  • There are 3 cheats that save 12 picoseconds.
  • There is one cheat that saves 20 picoseconds.
  • There is one cheat that saves 36 picoseconds.
  • There is one cheat that saves 38 picoseconds.
  • There is one cheat that saves 40 picoseconds.
  • There is one cheat that saves 64 picoseconds.

You aren't sure what the conditions of the racetrack will be like, so to give yourself as many options as possible, you'll need a list of the best cheats. How many cheats would save you at least 100 picoseconds?

Progress: 0 / Calculating...
Base Score: Calculating...
Maps Under: 0
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
174
175
176
177
interface Position { y: number; x: number; } type StrPosition = `${string},${string}`; interface NextPosition extends Position { prevScore: number; } class Map { start: Position; end: Position; matrix: Array<Array<string>>; finalScore: number | null; seenPositions: Set<StrPosition>; positionsToCheck: Array<NextPosition>; constructor(cheatStart: Position, cheatEnd: Position) { this.matrix = JSON.parse(JSON.stringify(values)); this.start = { y: -1, x: -1 }; this.end = { y: -1, x: -1 }; this.finalScore = null; this.matrix.forEach((row, y) => { row.forEach((character, x) => { if (character === "S") { this.start = { y, x }; } else if (character === "E") { this.end = { y, x }; } else if (y === cheatStart.y && x === cheatStart.x) { this.matrix[y]![x] = "."; } else if (y === cheatEnd.y && x === cheatEnd.x) { if (this.at({ y, x }) !== ".") { this.finalScore = Infinity; } } }); }); this.seenPositions = new Set(); // Start with the start position this.positionsToCheck = [ { // Up y: this.start.y - 1, x: this.start.x, prevScore: 1, }, { // Down y: this.start.y + 1, x: this.start.x, prevScore: 1, }, { // Left y: this.start.y, x: this.start.x - 1, prevScore: 1, }, { // Right y: this.start.y, x: this.start.x + 1, prevScore: 1, }, ]; } at({ y, x }: Position) { return this.matrix[y]?.[x]; } addPositionToCheck(position: NextPosition) { // Skip walls if (this.at(position) === "#") return; const { y, x } = position; const strPosition: StrPosition = `${y},${x}`; if (!this.seenPositions.has(strPosition)) { this.seenPositions.add(strPosition); 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 or map edge, early exit if (tile === undefined) return; if (tile === "#") return; if (tile === "E") { // Hit the end this.finalScore = position.prevScore; return; } // We've got a valid tile to move to // Push into our positions to check // Up this.addPositionToCheck({ y: position.y - 1, x: position.x, prevScore: position.prevScore + 1, }); // Down this.addPositionToCheck({ y: position.y + 1, x: position.x, prevScore: position.prevScore + 1, }); // Left this.addPositionToCheck({ y: position.y, x: position.x - 1, prevScore: position.prevScore + 1, }); // Right this.addPositionToCheck({ y: position.y, x: position.x + 1, prevScore: position.prevScore + 1, }); // Resort the positions by lowest score this.positionsToCheck.sort((a, b) => a.prevScore - b.prevScore); } cheats() { const positions: Array<[Position, Position]> = []; this.matrix.forEach((row, y) => { row.forEach((_, x) => { // x-cheat if (this.at({ y, x: x + 1 }) !== undefined) { positions.push([ { y, x }, { y, x: x + 1 }, ]); } // y-cheat if (this.at({ y: y + 1, x }) !== undefined) { positions.push([ { y, x }, { y: y + 1, x }, ]); } }); }); return positions; } }

To calculate the number of cheats, we iterate over the cheat start/end positions and recalculate the entire map using the cheat.

1
2
3
4
5
6
7
8
9
10
11
for (const [cheatStart, cheatEnd] of cheats) { const map = new Map(cheatStart, cheatEnd); while (map.finalScore === null) { map.step(); } if (map.finalScore <= baseTime - 100) { runningTotal += 1; } }

The programs seem perplexed by your list of cheats. Apparently, the two-picosecond cheating rule was deprecated several milliseconds ago! The latest version of the cheating rule permits a single cheat that instead lasts at most 20 picoseconds.

Now, in addition to all the cheats that were possible in just two picoseconds, many more cheats are possible. This six-picosecond cheat saves 76 picoseconds:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
############### #...#...#.....# #.#.#.#.#.###.# #S#...#.#.#...# #1#####.#.#.### #2#####.#.#...# #3#####.#.###.# #456.E#...#...# ###.#######.### #...###...#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############

Because this cheat has the same start and end positions as the one above, it's the same cheat, even though the path taken during the cheat is different:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
############### #...#...#.....# #.#.#.#.#.###.# #S12..#.#.#...# ###3###.#.#.### ###4###.#.#...# ###5###.#.###.# ###6.E#...#...# ###.#######.### #...###...#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############

Cheats don't need to use all 20 picoseconds; cheats can last any amount of time up to and including 20 picoseconds (but can still only end when the program is on normal track). Any cheat time not used is lost; it can't be saved for another cheat later. If cheat mode is active when the end position is reached, cheat mode ends automatically.

You'll still need a list of the best cheats, but now there are even more to choose between. Here are the quantities of cheats in this example that save 50 picoseconds or more:

  • There are 32 cheats that save 50 picoseconds.
  • There are 31 cheats that save 52 picoseconds.
  • There are 29 cheats that save 54 picoseconds.
  • There are 39 cheats that save 56 picoseconds.
  • There are 25 cheats that save 58 picoseconds.
  • There are 23 cheats that save 60 picoseconds.
  • There are 20 cheats that save 62 picoseconds.
  • There are 19 cheats that save 64 picoseconds.
  • There are 12 cheats that save 66 picoseconds.
  • There are 14 cheats that save 68 picoseconds.
  • There are 12 cheats that save 70 picoseconds.
  • There are 22 cheats that save 72 picoseconds.
  • There are 4 cheats that save 74 picoseconds.
  • There are 3 cheats that save 76 picoseconds.

Find the best cheats using the updated cheating rules. How many cheats would save you at least 100 picoseconds?

Base Score: Calculating...
Maps Under: 0
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
// Get all positions ordered from start to end const positions = map.orderedPositions(); let counter = 0; for (let startIndex = 0; startIndex < positions.length - 1; startIndex += 1) { for (let endIndex = startIndex + 1; endIndex < positions.length; endIndex += 1) { // Calculate normal distance between start and end const distance = endIndex - startIndex; // Calculate cheated distance (manhattan distance) const start = positions[startIndex]!; const end = positions[endIndex]!; const yDiff = Math.abs(start.y - end.y); const xDiff = Math.abs(start.x - end.x); const cheatDistance = yDiff + xDiff; if (cheatDistance <= 20) { const savings = distance - cheatDistance; if (savings >= 100) { counter += 1; } } } }
Part 1 TimePart 1 RankPart 2 TimePart 2 Rank
01:07:333,57903:14:354,400

In retrospect, this one wasn't particularly hard. I wasted a TON of time on Part 2 because I was updated React state inside of my loop, which absolutely tanked my performance. I waited about 45 minutes for the thing to complete before giving up and removing the React state updates. Some quick back-of-the-napkin match says it would've taken ~4 hours to complete if I hadn't done that. Now, it takes only a second or two ¯\_(ツ)_/¯