As you teleport onto Santa's Reindeer-class starship, The Historians begin to panic: someone from their search party is missing. A quick life-form scan by the ship's computer reveals that when the missing Historian teleported, he arrived in another part of the ship.
The door to that area is locked, but the computer can't open it; it can only be opened by physically typing the door codes (your puzzle input) on the numeric keypad on the door.
The numeric keypad has four rows of buttons: 789
, 456
, 123
, and finally an empty gap followed by 0A
. Visually, they are arranged like this:
123456789+---+---+---+
| 7 | 8 | 9 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 0 | A |
+---+---+
Unfortunately, the area outside the door is currently depressurized and nobody can go near the door. A robot needs to be sent instead.
The robot has no problem navigating the ship and finding the numeric keypad, but it's not designed for button pushing: it can't be told to push a specific button directly. Instead, it has a robotic arm that can be controlled remotely via a directional keypad.
The directional keypad has two rows of buttons: a gap / ^
(up) / A
(activate) on the first row and <
(left) / v
(down) / >
(right) on the second row. Visually, they are arranged like this:
12345 +---+---+
| ^ | A |
+---+---+---+
| < | v | > |
+---+---+---+
When the robot arrives at the numeric keypad, its robotic arm is pointed at the A
button in the bottom right corner. After that, this directional keypad remote control must be used to maneuver the robotic arm: the up / down / left / right buttons cause it to move its arm one button in that direction, and the A
button causes the robot to briefly move forward, pressing the button being aimed at by the robotic arm.
For example, to make the robot type 029A
on the numeric keypad, one sequence of inputs on the directional keypad you could use is:
<
to move the arm fromA
(its initial position) to0
.A
to push the0
button.^A
to move the arm to the2
button and push it.>^^A
to move the arm to the9
button and push it.vvvA
to move the arm to theA
button and push it.
In total, there are three shortest possible sequences of button presses on this directional keypad that would cause the robot to type 029A
: <A^A>^^AvvvA
, <A^A^>^AvvvA
, and <A^A^^>AvvvA
.
Unfortunately, the area containing this directional keypad remote control is currently experiencing high levels of radiation and nobody can go near it. A robot needs to be sent instead.
When the robot arrives at the directional keypad, its robot arm is pointed at the A
button in the upper right corner. After that, a second, different directional keypad remote control is used to control this robot (in the same way as the first robot, except that this one is typing on a directional keypad instead of a numeric keypad).
There are multiple shortest possible sequences of directional keypad button presses that would cause this robot to tell the first robot to type 029A
on the door. One such sequence is v<<A>>^A<A>AvA<^AA>A<vAAA>^A
.
Unfortunately, the area containing this second directional keypad remote control is currently -40
degrees! Another robot will need to be sent to type on that directional keypad, too.
There are many shortest possible sequences of directional keypad button presses that would cause this robot to tell the second robot to tell the first robot to eventually type 029A
on the door. One such sequence is <vA<AA>>^AvAA<^A>A<v<A>>^AvA^A<vA>^A<v<A>^A>AAvA^A<v<A>A>^AAAvA<^A>A
.
Unfortunately, the area containing this third directional keypad remote control is currently full of Historians, so no robots can find a clear path there. Instead, you will have to type this sequence yourself.
Were you to choose this sequence of button presses, here are all of the buttons that would be pressed on your directional keypad, the two robots' directional keypads, and the numeric keypad:
1234<vA<AA>>^AvAA<^A>A<v<A>>^AvA^A<vA>^A<v<A>^A>AAvA^A<v<A>A>^AAAvA<^A>A
v<<A>>^A<A>AvA<^AA>A<vAAA>^A
<A^A>^^AvvvA
029A
In summary, there are the following keypads:
- One directional keypad that you are using.
- Two directional keypads that robots are using.
- One numeric keypad (on a door) that a robot is using.
It is important to remember that these robots are not designed for button pushing. In particular, if a robot arm is ever aimed at a gap where no button is present on the keypad, even for an instant, the robot will panic unrecoverably. So, don't do that. All robots will initially aim at the keypad's A
key, wherever it is.
To unlock the door, five codes will need to be typed on its numeric keypad. For example:
12345029A
980A
179A
456A
379A
For each of these, here is a shortest sequence of button presses you could type to cause the desired code to be typed on the numeric keypad:
12345029A: <vA<AA>>^AvAA<^A>A<v<A>>^AvA^A<vA>^A<v<A>^A>AAvA^A<v<A>A>^AAAvA<^A>A
980A: <v<A>>^AAAvA^A<vA<AA>>^AvAA<^A>A<v<A>A>^AAAvA<^A>A<vA>^A<A>A
179A: <v<A>>^A<vA<A>>^AAvAA<^A>A<v<A>>^AAvA^A<vA>^AA<A>A<v<A>A>^AAAvA<^A>A
456A: <v<A>>^AA<vA<A>>^AAvAA<^A>A<vA>^A<A>A<vA>^A<A>A<v<A>A>^AAvA<^A>A
379A: <v<A>>^AvA^A<vA<AA>>^AAvA<^A>AAvA^A<vA>^AA<A>A<v<A>A>^AAAvA<^A>A
The Historians are getting nervous; the ship computer doesn't remember whether the missing Historian is trapped in the area containing a giant electromagnet or molten lava. You'll need to make sure that for each of the five codes, you find the shortest sequence of button presses necessary.
The complexity of a single code (like 029A
) is equal to the result of multiplying these two values:
- The length of the shortest sequence of button presses you need to type on your directional keypad in order to cause the code to be typed on the numeric keypad; for
029A
, this would be68
. - The numeric part of the code (ignoring leading zeroes); for
029A
, this would be29
.
In the above example, complexity of the five codes can be found by calculating 68 * 29
, 60 * 980
, 68 * 179
, 64 * 456
, and 64 * 379
. Adding these together produces 126384
.
Find the fewest number of button presses you'll need to perform in order to cause the robot in front of the door to type each code. What is the sum of the complexities of the five codes on your list?
First, build up a map of {previous key}
to {current key}
. Explicit types help here to ensure a combination isn't forgotten.
If we don't change keys, there are no key presses needed (for example, AA: ""
).
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154const numKeys = ["A", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] as const;
const dirKeys = ["A", "^", "<", "v", ">"] as const;
type NumKey = (typeof numKeys)[number];
type DirKey = (typeof dirKeys)[number];
const keyToNext: Record<`${NumKey}${NumKey}` | `${DirKey}${DirKey}`, string> = {
AA: "",
A0: "<A",
A1: "^<<A",
A2: "<^A",
A3: "^A",
A4: "^^<<A",
A5: "<^^A",
A6: "^^A",
A7: "^^^<<A",
A8: "<^^^A",
A9: "^^^A",
"00": "",
"0A": ">A",
"01": "^<A",
"02": "^A",
"03": "^>A",
"04": "^^<A",
"05": "^^A",
"06": "^^>A",
"07": "^^^<A",
"08": "^^^A",
"09": "^^^>A",
"11": "",
"1A": ">>vA",
"10": ">vA",
"12": ">A",
"13": ">>A",
"14": "^A",
"15": "^>A",
"16": "^>>A",
"17": "^^A",
"18": "^^>A",
"19": "^^>>A",
"22": "",
"2A": "v>A",
"20": "vA",
"21": "<A",
"23": ">A",
"24": "<^A",
"25": "^A",
"26": "^>A",
"27": "<^^A",
"28": "^^A",
"29": "^^>A",
"33": "",
"3A": "vA",
"30": "<vA",
"31": "<<A",
"32": "<A",
"34": "<<^A",
"35": "<^A",
"36": "^A",
"37": "<<^^A",
"38": "<^^A",
"39": "^^A",
"44": "",
"4A": ">>vvA",
"40": ">vvA",
"41": "vA",
"42": "v>A",
"43": "v>>A",
"45": ">A",
"46": ">>A",
"47": "^A",
"48": "^>A",
"49": "^>>A",
"55": "",
"5A": "vv>A",
"50": "vvA",
"51": "<vA",
"52": "vA",
"53": "v>A",
"54": "<A",
"56": ">A",
"57": "<^A",
"58": "^A",
"59": "^>A",
"66": "",
"6A": "vvA",
"60": "<vvA",
"61": "<<vA",
"62": "<vA",
"63": "vA",
"64": "<<A",
"65": "<A",
"67": "<<^A",
"68": "<^A",
"69": "^A",
"77": "",
"7A": ">>vvvA",
"70": ">vvvA",
"71": "vvA",
"72": "vv>A",
"73": "vv>>A",
"74": "vA",
"75": "v>A",
"76": "v>>A",
"78": ">A",
"79": ">>A",
"88": "",
"8A": ">vvvA",
"80": "vvvA",
"81": "<vvA",
"82": "vvA",
"83": "vv>A",
"84": "<vA",
"85": "vA",
"86": "v>A",
"87": "<A",
"89": ">A",
"99": "",
"9A": "vvvA",
"90": "<vvvA",
"91": "<<vvA",
"92": "<vvA",
"93": "vvA",
"94": "<<vA",
"95": "<vA",
"96": "vA",
"97": "<<A",
"98": "<A",
"A^": "<A",
"A<": "v<<A",
Av: "<vA",
"A>": "vA",
"^^": "",
"^A": ">A",
"^<": "v<A",
"^v": "vA",
"^>": "v>A",
"<<": "",
"<A": ">>^A",
"<^": ">^A",
"<v": ">A",
"<>": ">>A",
vv: "",
vA: "^>A",
"v^": "^A",
"v<": "<A",
"v>": ">A",
">>": "",
">A": "^A",
">^": "<^A",
"><": "<<A",
">v": "<A",
} as const;
A few helper functions for updating the "layers" of key presses. The calculateParentKeystrokes
function creates a new counter for the next "layer".
12345678910111213141516171819202122232425262728293031323334function nextKey(key: string) {
if (key.charAt(0) === key.charAt(1)) {
return "A";
}
// @ts-ignore
return keyToNext[key]!;
}
function updateCounter(counter: Counter, keys: string, count: number) {
for (let i = 0; i < keys.length - 1; i += 1) {
const previous = keys[i]!;
const current = keys[i + 1]!;
const movement = `${previous}${current}`;
counter.set(movement, counter.get(movement) + count);
}
}
function calculateParentKeystrokes(counter: Counter) {
const parentCounter = new Counter();
for (const key of counter.keys()) {
const next = nextKey(key);
const count = counter.get(key);
// All robots start on A
updateCounter(parentCounter, `A${next}`, count);
}
return parentCounter;
}
And finally, we process all values in the input.
1234567891011121314151617181920212223242526// Process values
for await (const value of values) {
// Numpad always starts at A
const instructions = `A${value}`;
let counter = new Counter();
for (let i = 0; i < instructions.length - 1; i += 1) {
const previous = instructions[i]!;
const current = instructions[i + 1]!;
const movement = nextKey(`${previous}${current}`);
// Every movement requires pressing A
updateCounter(counter, `A${movement}`, 1);
}
// Iterate over levels
for (let level = 0; level < 2; level += 1) {
counter = calculateParentKeystrokes(counter);
}
const numeric = z.coerce.number().parse(value.replaceAll("A", ""));
runningTotal += sum(counter.values()) * numeric;
}
Just as the missing Historian is released, The Historians realize that a second member of their search party has also been missing this entire time!
A quick life-form scan reveals the Historian is also trapped in a locked area of the ship. Due to a variety of hazards, robots are once again dispatched, forming another chain of remote control keypads managing robotic-arm-wielding robots.
This time, many more robots are involved. In summary, there are the following keypads:
- One directional keypad that you are using.
- 25 directional keypads that robots are using.
- One numeric keypad (on a door) that a robot is using.
The keypads form a chain, just like before: your directional keypad controls a robot which is typing on a directional keypad which controls a robot which is typing on a directional keypad... and so on, ending with the robot which is typing on the numeric keypad.
The door codes are the same this time around; only the number of robots and directional keypads has changed.
Find the fewest number of button presses you'll need to perform in order to cause the robot in front of the door to type each code. What is the sum of the complexities of the five codes on your list?
This uses the exact same code as Part 1, but checks 25 levels instead of 2.
Part 1 Time | Part 1 Rank | Part 2 Time | Part 2 Rank |
---|---|---|---|
18:41:00 | 11,297 | 18:48:16 | 7,521 |
I didn't really start on this until the following day. I looked at the problem when it came out, guessed that Part 2 would have N many more layers, and decided to sleep on it. In total, this probably took ~3 hours or so of actual work.
I'm not 100% satisfied with my solution. Changing some of the directions in my mapping gives me a different answer, even though it feels like it shouldn't. But ¯\_(ツ)_/¯ just happy to be through this one.