-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday21.py
65 lines (49 loc) · 1.25 KB
/
day21.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from functools import cache
from itertools import pairwise
data = open(0).read()
inp = data.splitlines()
G = {
"A": (2, 1),
"0": (1, 1),
"1": (0, 2),
"2": (1, 2),
"3": (2, 2),
"4": (0, 3),
"5": (1, 3),
"6": (2, 3),
"7": (0, 4),
"8": (1, 4),
"9": (2, 4),
"^": (1, 1),
"<": (0, 0),
"v": (1, 0),
">": (2, 0),
}
@cache
def move(p, q, r):
(x0, y0), (x1, y1) = p, q
dx, dy = x1 - x0, y1 - y0
if r == 1:
return abs(dx) + abs(dy) + 1
res1, res2 = float("inf"), float("inf")
lr = ">" * dx if dx > 0 else "<" * abs(dx)
ud = "^" * dy if dy > 0 else "v" * abs(dy)
if not (x1 == 0 and y0 == 1):
code = "A" + lr + ud + "A"
res1 = sum(move(G[c], G[d], r - 1) for c, d in pairwise(code))
if not (x0 == 0 and y1 == 1):
code = "A" + ud + lr + "A"
res2 = sum(move(G[c], G[d], r - 1) for c, d in pairwise(code))
return min(res1, res2)
def part1():
return sum(
sum(move(G[c], G[d], 3) for c, d in pairwise("A" + code)) * int(code[:-1])
for code in inp
)
def part2():
return sum(
sum(move(G[c], G[d], 26) for c, d in pairwise("A" + code)) * int(code[:-1])
for code in inp
)
print(part1())
print(part2())