|
| 1 | +Link: <https://adventofcode.com/2024/day/20> <br> |
| 2 | +Author: Eric Wastl ([@ericwastl](https://twitter.com/ericwastl)) (2024) |
| 3 | + |
| 4 | +--- |
| 5 | + |
| 6 | +## --- Day 20: Race Condition --- |
| 7 | + |
| 8 | +The Historians are quite pixelated again. This time, a massive, black building looms over you - you're [right outside](/2017/day/24) the CPU! |
| 9 | + |
| 10 | +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! |
| 11 | + |
| 12 | +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](https://en.wikipedia.org/wiki/Lock_(computer_science))! |
| 13 | + |
| 14 | +They hand you a **map of the racetrack** (your puzzle input). For example: |
| 15 | + |
| 16 | +``` |
| 17 | +############### |
| 18 | +#...#...#.....# |
| 19 | +#.#.#.#.#.###.# |
| 20 | +#S#...#.#.#...# |
| 21 | +#######.#.#.### |
| 22 | +#######.#.#...# |
| 23 | +#######.#.###.# |
| 24 | +###..E#...#...# |
| 25 | +###.#######.### |
| 26 | +#...###...#...# |
| 27 | +#.#####.#.###.# |
| 28 | +#.#...#.#.#...# |
| 29 | +#.#.#.#.#.#.### |
| 30 | +#...#...#...### |
| 31 | +############### |
| 32 | +``` |
| 33 | + |
| 34 | +The map consists of track (`.`) - including the **start** (`S`) and **end** (`E`) positions (both of which also count as track) - and **walls** (`#`). |
| 35 | + |
| 36 | +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. |
| 37 | + |
| 38 | +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**. |
| 39 | + |
| 40 | +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](https://en.wikipedia.org/wiki/Segmentation_fault) and get disqualified. |
| 41 | + |
| 42 | +So, a program could complete the course in 72 picoseconds (saving **12 picoseconds**) by cheating for the two moves marked `1` and `2`: |
| 43 | + |
| 44 | +``` |
| 45 | +############### |
| 46 | +#...#...12....# |
| 47 | +#.#.#.#.#.###.# |
| 48 | +#S#...#.#.#...# |
| 49 | +#######.#.#.### |
| 50 | +#######.#.#...# |
| 51 | +#######.#.###.# |
| 52 | +###..E#...#...# |
| 53 | +###.#######.### |
| 54 | +#...###...#...# |
| 55 | +#.#####.#.###.# |
| 56 | +#.#...#.#.#...# |
| 57 | +#.#.#.#.#.#.### |
| 58 | +#...#...#...### |
| 59 | +############### |
| 60 | +``` |
| 61 | + |
| 62 | +Or, a program could complete the course in 64 picoseconds (saving **20 picoseconds**) by cheating for the two moves marked `1` and `2`: |
| 63 | + |
| 64 | +``` |
| 65 | +############### |
| 66 | +#...#...#.....# |
| 67 | +#.#.#.#.#.###.# |
| 68 | +#S#...#.#.#...# |
| 69 | +#######.#.#.### |
| 70 | +#######.#.#...# |
| 71 | +#######.#.###.# |
| 72 | +###..E#...12..# |
| 73 | +###.#######.### |
| 74 | +#...###...#...# |
| 75 | +#.#####.#.###.# |
| 76 | +#.#...#.#.#...# |
| 77 | +#.#.#.#.#.#.### |
| 78 | +#...#...#...### |
| 79 | +############### |
| 80 | +``` |
| 81 | + |
| 82 | +This cheat saves **38 picoseconds**: |
| 83 | + |
| 84 | +``` |
| 85 | +############### |
| 86 | +#...#...#.....# |
| 87 | +#.#.#.#.#.###.# |
| 88 | +#S#...#.#.#...# |
| 89 | +#######.#.#.### |
| 90 | +#######.#.#...# |
| 91 | +#######.#.###.# |
| 92 | +###..E#...#...# |
| 93 | +###.####1##.### |
| 94 | +#...###.2.#...# |
| 95 | +#.#####.#.###.# |
| 96 | +#.#...#.#.#...# |
| 97 | +#.#.#.#.#.#.### |
| 98 | +#...#...#...### |
| 99 | +############### |
| 100 | +``` |
| 101 | + |
| 102 | +This cheat saves **64 picoseconds** and takes the program directly to the end: |
| 103 | + |
| 104 | +``` |
| 105 | +############### |
| 106 | +#...#...#.....# |
| 107 | +#.#.#.#.#.###.# |
| 108 | +#S#...#.#.#...# |
| 109 | +#######.#.#.### |
| 110 | +#######.#.#...# |
| 111 | +#######.#.###.# |
| 112 | +###..21...#...# |
| 113 | +###.#######.### |
| 114 | +#...###...#...# |
| 115 | +#.#####.#.###.# |
| 116 | +#.#...#.#.#...# |
| 117 | +#.#.#.#.#.#.### |
| 118 | +#...#...#...### |
| 119 | +############### |
| 120 | +``` |
| 121 | + |
| 122 | +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. |
| 123 | + |
| 124 | +In this example, the total number of cheats (grouped by the amount of time they save) are as follows: |
| 125 | + |
| 126 | +- There are 14 cheats that save 2 picoseconds. |
| 127 | +- There are 14 cheats that save 4 picoseconds. |
| 128 | +- There are 2 cheats that save 6 picoseconds. |
| 129 | +- There are 4 cheats that save 8 picoseconds. |
| 130 | +- There are 2 cheats that save 10 picoseconds. |
| 131 | +- There are 3 cheats that save 12 picoseconds. |
| 132 | +- There is one cheat that saves 20 picoseconds. |
| 133 | +- There is one cheat that saves 36 picoseconds. |
| 134 | +- There is one cheat that saves 38 picoseconds. |
| 135 | +- There is one cheat that saves 40 picoseconds. |
| 136 | +- There is one cheat that saves 64 picoseconds. |
| 137 | + |
| 138 | +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?** |
0 commit comments