Skip to content

Commit 16b2456

Browse files
committed
Add day 21
1 parent 7c8523e commit 16b2456

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

2024/21/21.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,28 @@ def path_to(start, end, numpad):
1616
dy = ey-sy
1717
spacex, spacey = pad[" "]
1818
ri = up = ""
19+
yy = ("^" if dy < 0 else "v") * abs(dy)
20+
xx = ("<" if dx < 0 else ">") * abs(dx)
1921
if not (sx == spacex and sy+dy == spacey):
20-
up += ("^" if dy < 0 else "v") * abs(dy)
21-
up += ("<" if dx < 0 else ">") * abs(dx)
22-
22+
s = up = yy + xx
2323
if not (sx+dx == spacex and sy == spacey):
24-
ri += ("<" if dx < 0 else ">") * abs(dx)
25-
ri += ("^" if dy < 0 else "v") * abs(dy)
24+
s = ri = xx + yy
2625

2726
if up and ri:
2827
s = up if random() < 0.5 else ri
29-
else:
30-
s = up or ri
3128
return s + "A"
3229

33-
3430
@cache
35-
def length(P, char, i):
36-
if i == 0: return 1
37-
s = 0
38-
prev = 'A'
39-
for c in path_to(P, char, i==G):
40-
s += length(prev, c, i-1)
41-
prev = c
31+
def length(code, robot, s=0):
32+
if robot == 0: return len(code)
33+
for i, c in enumerate(code):
34+
s += length(path_to(code[i-1], c, robot==G), robot-1)
4235
return s
4336

4437
def solve(code):
4538
path_to.cache_clear()
4639
length.cache_clear()
47-
prev = 'A'
48-
s = 0
49-
for c in code:
50-
s += length(prev, c, G)
51-
prev = c
52-
return int(code[:-1]) * s
40+
return int(code[:-1]) * length(code, G)
5341

5442
def simulate(code):
5543
return min(solve(code) for _ in range(1000))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy