-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09-Rope Bridge.py
44 lines (41 loc) · 1.02 KB
/
day09-Rope Bridge.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
lines = open('input').readlines()
lines = [_.strip() for _ in lines]
directions = {
'R': (0, 1),
'L': (0, -1),
'U': (-1, 0),
'D': (1, 0),
}
strategy = {
(2, -2): (1, -1),
(2, -1): (1, -1),
(2, 0): (1, 0),
(2, 1): (1, 1),
(2, 2): (1, 1),
(-2, -2): (-1, -1),
(-2, 0): (-1, 0),
(-2, -1): (-1, -1),
(-2, 1): (-1, 1),
(-2, 2): (-1, 1),
(0, 2): (0, 1),
(0, -2): (0, -1),
(1, 2): (1, 1),
(-1, 2): (-1, 1),
(1, -2): (1, -1),
(-1, -2): (-1, -1),
}
H = (0, 0)
tails = [H for _ in range(10)]
visited = {H}
for line in lines:
d, steps = line.split(' ')
for i in range(int(steps)):
a, b = directions[d]
H = (H[0] + a, H[1] + b)
tails[-1] = H
for j in reversed(range(9)):
dist = (tails[j + 1][0] - tails[j][0], tails[j + 1][1] - tails[j][1])
if dist in strategy:
tails[j] = (tails[j][0] + strategy[dist][0], tails[j][1] + strategy[dist][1])
visited.add(tails[0])
print(len(visited))