1
1
import functools
2
+ from collections import Counter , defaultdict
2
3
3
4
from . import SeparateRunner
4
5
@@ -114,6 +115,18 @@ def decode(code: str, pad: dict[str, tuple[int, int]]) -> str:
114
115
return result
115
116
116
117
118
+ def count_steps (path : str , count : int ) -> dict [str , int ]:
119
+ cur = "A"
120
+ counts = defaultdict (int )
121
+
122
+ for c in path :
123
+ step = shortest_dirpad (cur , c )
124
+ cur = c
125
+ counts [step ] += count
126
+
127
+ return counts
128
+
129
+
117
130
class DayRunner (SeparateRunner ):
118
131
@classmethod
119
132
def part1 (cls , input : str ) -> int :
@@ -128,5 +141,19 @@ def part1(cls, input: str) -> int:
128
141
return result
129
142
130
143
@classmethod
131
- def part2 (cls , input : str ) -> int :
132
- pass
144
+ def part2 (cls , input : str , robots = 25 ) -> int :
145
+ result = 0
146
+ for code in input .strip ().split ("\n " ):
147
+ numpad = encode_shortest_numpad (code )
148
+ keypresses = Counter ([numpad ])
149
+
150
+ for _ in range (robots + 1 ):
151
+ new_presses = Counter ()
152
+ for subroute , count in keypresses .items ():
153
+ new_presses .update (count_steps (subroute , count ))
154
+
155
+ keypresses = new_presses
156
+
157
+ result += int (code [:- 1 ]) * keypresses .total ()
158
+
159
+ return result
0 commit comments