Skip to content

Commit 5febf79

Browse files
committed
add 2024/21-25
1 parent 6e59d79 commit 5febf79

26 files changed

+11079
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ a.c
44
a.out
55
a.o
66
*vis*.p[gp]m
7+
*vis*.dot
78
*vis*.svg
89
*vis*.png
910
.vis*

2024/21/README.md

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# 2024, Day 21: Keypad Conundrum
2+
3+
As you teleport onto Santa's [Reindeer-class starship](../../2019/25), 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.
4+
5+
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.
6+
7+
## Part 1
8+
9+
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:
10+
11+
+---+---+---+
12+
| 7 | 8 | 9 |
13+
+---+---+---+
14+
| 4 | 5 | 6 |
15+
+---+---+---+
16+
| 1 | 2 | 3 |
17+
+---+---+---+
18+
| 0 | A |
19+
+---+---+
20+
21+
Unfortunately, the area outside the door is currently _depressurized_ and nobody can go near the door. A robot needs to be sent instead.
22+
23+
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_.
24+
25+
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:
26+
27+
+---+---+
28+
| ^ | A |
29+
+---+---+---+
30+
| < | v | > |
31+
+---+---+---+
32+
33+
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.
34+
35+
For example, to make the robot type `029A` on the numeric keypad, one sequence of inputs on the directional keypad you could use is:
36+
37+
* `<` to move the arm from `A` (its initial position) to `0`.
38+
* `A` to push the `0` button.
39+
* `^A` to move the arm to the `2` button and push it.
40+
* `>^^A` to move the arm to the `9` button and push it.
41+
* `vvvA` to move the arm to the `A` button and push it.
42+
43+
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`.
44+
45+
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.
46+
47+
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).
48+
49+
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`.
50+
51+
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.
52+
53+
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`.
54+
55+
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.
56+
57+
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:
58+
59+
<vA<AA>>^AvAA<^A>A<v<A>>^AvA^A<vA>^A<v<A>^A>AAvA^A<v<A>A>^AAAvA<^A>A
60+
v<<A>>^A<A>AvA<^AA>A<vAAA>^A
61+
<A^A>^^AvvvA
62+
029A
63+
64+
In summary, there are the following keypads:
65+
66+
* One directional keypad that _you_ are using.
67+
* Two directional keypads that _robots_ are using.
68+
* One numeric keypad (on a door) that a _robot_ is using.
69+
70+
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.
71+
72+
To unlock the door, _five_ codes will need to be typed on its numeric keypad. For example:
73+
74+
029A
75+
980A
76+
179A
77+
456A
78+
379A
79+
80+
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:
81+
82+
029A: <vA<AA>>^AvAA<^A>A<v<A>>^AvA^A<vA>^A<v<A>^A>AAvA^A<v<A>A>^AAAvA<^A>A
83+
980A: <v<A>>^AAAvA^A<vA<AA>>^AvAA<^A>A<v<A>A>^AAAvA<^A>A<vA>^A<A>A
84+
179A: <v<A>>^A<vA<A>>^AAvAA<^A>A<v<A>>^AAvA^A<vA>^AA<A>A<v<A>A>^AAAvA<^A>A
85+
456A: <v<A>>^AA<vA<A>>^AAvAA<^A>A<vA>^A<A>A<vA>^A<A>A<v<A>A>^AAvA<^A>A
86+
379A: <v<A>>^AvA^A<vA<AA>>^AAvA<^A>AAvA^A<vA>^AA<A>A<v<A>A>^AAAvA<^A>A
87+
88+
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.
89+
90+
The _complexity_ of a single code (like `029A`) is equal to the result of multiplying these two values:
91+
92+
* 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 be `68`.
93+
* The _numeric part of the code_ (ignoring leading zeroes); for `029A`, this would be `29`.
94+
95+
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`_.
96+
97+
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?_
98+
99+
Your puzzle answer was `105458`.
100+
101+
## Part 2
102+
103+
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!
104+
105+
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.
106+
107+
This time, many more robots are involved. In summary, there are the following keypads:
108+
109+
* One directional keypad that _you_ are using.
110+
* _25_ directional keypads that _robots_ are using.
111+
* One numeric keypad (on a door) that a _robot_ is using.
112+
113+
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.
114+
115+
The door codes are the same this time around; only the number of robots and directional keypads has changed.
116+
117+
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?_
118+
119+
Your puzzle answer was `129551515895690`.
120+
121+
## Solution Notes
122+
123+
This puzzle has a lot of pitfalls that make it extra hard to solve (I took several hours and one full rewrite), but at its core, it's just a recursive search for the path through the keypad that enters a certain sequence with minimal input length on the _next_ keypad. Memoization is required for part 2, lest the search tree becomes too deep to be feasible without it, but that's in fact the only required change once the rest of the algorithm is sound. It's only about 50 lines of non-golf Python, but unfortunately it doesn't golf too well, coming in at about 500 bytes.
124+
125+
* Part 1, Python: 478 bytes, <100 ms
126+
* Part 2, Python: 510 bytes, <100 ms

2024/21/aoc2024_21_nogolf.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
"AoC 2024/21"
3+
import functools
4+
5+
def generate_keymap(keys):
6+
return {key:(x,y) for y,row in enumerate(keys.split()) for x,key in enumerate(row)}
7+
keymaps = [
8+
generate_keymap("789 456 123 x0A"),
9+
generate_keymap("x^A <v>")
10+
]
11+
12+
@functools.cache
13+
def search(sequence, pad_no, max_pads):
14+
if pad_no > max_pads:
15+
return len(sequence)
16+
keymap = keymaps[pad_no > 0]
17+
px, py = keymap['A']
18+
ix, iy = keymap['x']
19+
result = 0
20+
for key in sequence:
21+
tx, ty = keymap[key]
22+
queue = [(px, py, "")]
23+
paths = []
24+
while queue:
25+
cx, cy, path = queue.pop()
26+
if (cx, cy) == (tx, ty):
27+
paths.append(path + 'A')
28+
continue
29+
if (cx, cy) == (ix, iy):
30+
continue
31+
if tx < cx: queue.append((cx-1, cy, path + '<'))
32+
if tx > cx: queue.append((cx+1, cy, path + '>'))
33+
if ty < cy: queue.append((cx, cy-1, path + '^'))
34+
if ty > cy: queue.append((cx, cy+1, path + 'v'))
35+
result += min(search(path, pad_no + 1, max_pads) for path in paths)
36+
px, py = tx, ty
37+
return result
38+
39+
def run(max_pads, verbose=False):
40+
result = 0
41+
for line in open("input.txt"):
42+
sequence = line.strip()
43+
value = int(sequence[:-1])
44+
length = search(sequence, 0, max_pads)
45+
score = length * value
46+
if verbose:
47+
print(sequence + ":", length, "*", value, "=", score)
48+
result += score
49+
print(result)
50+
51+
if __name__ == "__main__":
52+
run(2)
53+
run(25)

2024/21/aoc2024_21_part1.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
E=enumerate
2+
M=lambda k:{c:x+y*1j for y,r in E(k.split())for x,c in E(r)};M=[M("789 456 123 x0A"),M("x^A <v>")]
3+
def D(s,n):
4+
if n>2:return len(s)
5+
m=M[n>0];c,i,r=m['A'],m['x'],0
6+
for k in s:
7+
u=m[k];q,z=[(c,'')],[]
8+
while q:
9+
x,p=q.pop()
10+
if x==u:z+=[p+'A']
11+
elif x-i:q+=[(x+a,p+d)for a,d in((-1,'<'),(1,'>'),(-1j,'^'),(1j,'v'))if((x-u)*a.conjugate()).real<0]
12+
r+=min(D(p,n+1)for p in z);c=u
13+
return r
14+
print(sum(D(s:=l.strip(),0)*int(s[:-1])for l in open("input.txt")))

2024/21/aoc2024_21_part2.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
E=enumerate
2+
M=lambda k:{c:x+y*1j for y,r in E(k.split())for x,c in E(r)};M=[M("789 456 123 x0A"),M("x^A <v>")]
3+
import functools as F
4+
@F.cache
5+
def D(s,n):
6+
if n>25:return len(s)
7+
m=M[n>0];c,i,r=m['A'],m['x'],0
8+
for k in s:
9+
u=m[k];q,z=[(c,'')],[]
10+
while q:
11+
x,p=q.pop()
12+
if x==u:z+=[p+'A']
13+
elif x-i:q+=[(x+a,p+d)for a,d in((-1,'<'),(1,'>'),(-1j,'^'),(1j,'v'))if((x-u)*a.conjugate()).real<0]
14+
r+=min(D(p,n+1)for p in z);c=u
15+
return r
16+
print(sum(D(s:=l.strip(),0)*int(s[:-1])for l in open("input.txt")))

2024/21/input.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
140A
2+
170A
3+
169A
4+
803A
5+
129A

0 commit comments

Comments
 (0)