--- Day 20: Race Condition ---
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:
############### #...#...#.....# #.#.#.#.#.###.# #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
and2
:############### #...#...12....# #.#.#.#.#.###.# #S#...#.#.#...# #######.#.#.### #######.#.#...# #######.#.###.# ###..E#...#...# ###.#######.### #...###...#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############
Or, a program could complete the course in 64 picoseconds (saving 20 picoseconds) by cheating for the two moves marked
1
and2
:############### #...#...#.....# #.#.#.#.#.###.# #S#...#.#.#...# #######.#.#.### #######.#.#...# #######.#.###.# ###..E#...12..# ###.#######.### #...###...#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############
This cheat saves 38 picoseconds:
############### #...#...#.....# #.#.#.#.#.###.# #S#...#.#.#...# #######.#.#.### #######.#.#...# #######.#.###.# ###..E#...#...# ###.####1##.### #...###.2.#...# #.#####.#.###.# #.#...#.#.#...# #.#.#.#.#.#.### #...#...#...### ###############
This cheat saves 64 picoseconds and takes the program directly to the end:
############### #...#...#.....# #.#.#.#.#.###.# #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?
First, read the grid, and find the size, and start and end point.
n: count g: read0 `20.txt; show g "###############" "#...#...#.....#" "#.#.#.#.#.###.#" "#S#...#.#.#...#" "#######.#.#.###" "#######.#.#...#" "#######.#.###.#" "###..E#...#...#" "###.#######.###" "#...###...#...#" "#.#####.#.###.#" "#.#...#.#.#...#" "#.#.#.#.#.#.###" "#...#...#...###" "###############"
show (s;e): n vs' raze[g]?"SE" 3 1 7 5
First of all, we're told there is only 1 path, so we can find it easily.
Define a list of directions.
d: (0 1;0 -1;1 0;-1 0)
Now, we traverse the path. Given a value, and the previous point, we find the next position in the path by adding all directions, and filtering out walls and the previous value.
p where "#"<>0N!g ./: 0N!p: d+\:s (3 2;3 0;4 1;2 1) "###." 2 1
We can see the process above.
All that's left is to repeat this until we meet the end, and collect intermediate values.
We also mark the path as unique to speed up lookups.
show path: `u#last each {not e~last x} ({(y; first p where (not p~\:x)&"#"<>g ./:p:d+\:y)}.)\ (();s) 3 1 2 1 1 1 1 2 1 3 2 3 3 3 3 4 3 5 2 5 1 5 1 6 1 7 2 7 3 7 4 7 5 7 6 7 7 7 7 8 7 9 6 9 ..
To find "cheats", we look at positions that are a distance of 2 away. The amount of saved time can be calculated from the difference in the new track position and the previous one.
First of all, define distances 2 away.
show d2: (distinct raze d+/:\:d) except enlist 0 0 0 2 1 1 -1 1 0 -2 1 -1 -1 -1 2 0 -2 0
Now, we add these to each value in the path, and look up its position. The mod is so anything not in the path becomes 0.
mod[path?d2+\:/:path;count path] 6 0 0 0 0 0 0 2 5 0 3 0 0 0 0 0 4 0 0 0 0 0 0 0 0 5 0 0 1 0 0 0 10 0 0 2 0 0 6 0 9 7 0 1 0 3 0 0 8 0 0 0 0 0 0 4 0 0 9 0 0 5 0 0 14 0 0 6 0 0 0 10 13 0 11 5 7 0 0 0 12 0 0 4 0 0 8 0 0 13 0 0 9 0 0 0 26 0 0 10 0 0 14 0 25 0 0 9 0 11 15 0 24 0 0 8 0 0 16 12 23 0 0 0 0 0 17 13 22 0 0 0 0 0 18 14 21 19 0 0 0 0 0 15 20 0 0 84 0 0 60 16 0 0 21 0 0 17 59 0 42 0 0 18 0 0 58 22 0 0 0 17 19 0 0 23 ..
This gives us a table of savings. We then subtract the cost, namely the index of the first position, plus 2 to account for the "cheat" cost.
show tab: mod[path?d2+\:/:path;count path]-2+til count path 4 -2 -2 -2 -2 -2 -2 0 2 -3 0 -3 -3 -3 -3 -3 0 -4 -4 -4 -4 -4 -4 -4 -5 0 -5 -5 -4 -5 -5 -5 4 -6 -6 -4 -6 -6 0 -6 2 0 -7 -6 -7 -4 -7 -7 0 -8 -8 -8 -8 -8 -8 -4 -9 -9 0 -9 -9 -4 -9 -9 4 -10 -10 -4 -10 -10 -10 0 2 -11 0 -6 -4 -11 -11 -11 0 -12 -12 -8 -12 -12 -4 -12 -13 0 -13 -13 -4 -13 -13 -13 12 -14 -14 -4 -14 -14 0 -14 10 -15 -15 -6 -15 -4 0 -15 8 -16 -16 -8 -16 -16 0 -4 6 -17 -17 -17 -17 -17 0 -4 4 -18 -18 -18 -18 -18 0 -4 2 0 -19 -19 -19 -19 -19 -4 0 -20 -20 64 -20 -20 40 -4 -21 -21 0 -21 -21 -4 38 -21 20 -22 -22 -4 -22 -22 36 0 -23 -23 -23 -6 -4 -23 -23 0 ..
We can use 0|
to map any negatives to 0, and count the frequencies of each occurrence.
show freqs: count each group asc raze 0|tab
Our answer is the total number of values in the table greater than or equal to 100.
sum sum 99<tab
--- Part Two ---
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:
############### #...#...#.....# #.#.#.#.#.###.# #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:
############### #...#...#.....# #.#.#.#.#.###.# #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.
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?
Now we can cheat for a distance up to 20.
We can adjust part 1 by calculating new offsets, and their costs.
First of all, let's calculate all positions within a manhattan distance of 20. We can do this by starting with pairs of points where the coordinates are within [-20,20]
.
{x cross x} 0N!til[41]-20 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 .. -20 -20 -20 -19 -20 -18 -20 -17 -20 -16 -20 -15 -20 -14 -20 -13 -20 -12 -20 -11 -20 -10 -20 -9 -20 -8 -20 -7 -20 -6 -20 -5 -20 -4 -20 -3 -20 -2 -20 -1 -20 0 -20 1 ..
Now, we calculate the manhattan distance, sort by it, and filter by it being less than or equal to 20. This gives us a list of points, and distances.
show (o;c): {(sum D<21)#'(x;D)@\:iasc D:sum each abs x} {x cross x} til[41]-20 0 0 -1 0 0 -1 0 1 1 0 -2 0 -1 -1 -1 1 0 -2 0 2.. 0 1 1 1 1 2 2 2 2 2 ..
Now, we can reuse the method from part 1.
show tab: 0|mod[path?o+\:/:path;count path]-c+/:til count path 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 2 0 4 0 0 0 0 0 0 0 0 0 0 0 0 .. 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 .. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 38 0 0 0 0 0 62 20 0 0 38 36 0 0 0 0 0 0 0 0 .. 0 0 0 0 0 0 0 0 0 20 0 0 36 0 0 0 0 0 0 18 0 20 36 0 34 0 0 0 0 12 0 0 .. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12 0 0 0 18 0 0 34 0 0 0 0 10 0 12 ..
-10#count each group asc raze tab 58| 25 60| 23 62| 20 64| 19 66| 12 68| 14 70| 12 72| 22 74| 4 76| 3
sum sum 99<tab 0i
For the full solution, we can also reuse this calculation for part 1.
n: count g: read0 `20.txt
(s;e): n vs' raze[g]?"SE"
d: (0 1;0 -1;1 0;-1 0)
path: `u#last each {not e~last x} ({(y; first p where (not p~\:x)&"#"<>g ./:p:d+\:y)}.)\ (();s)
d2: (distinct raze d+/:\:d) except enlist 0 0
sum sum 99<mod[path?d2+\:/:path;count path]-2+til count path
(o;c): {(sum D<21)#'(x;D)@\:iasc D:sum each abs x} {x cross x} til[41]-20
sum sum 99<mod[path?o+\:/:path;count path]-c+/:til count path
To speed things up, rather than using 2d coordinates, which are represented as pairs, we can represent them as single integers, by encoding.
The following is much faster for part 2:
N:n+50; o: N sv'o; p: N sv'path
sum sum 99<mod[p?p+/:o;count p]-c+\:til[count p]