-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd9.py
53 lines (46 loc) · 1.32 KB
/
d9.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
data = [x.strip('\n') for x in open("i9.txt").readlines()]
# data = [ x.strip('\n') for x in open("t9.txt").readlines() ]
def part(p):
s = (0, 0)
r = [s for i in range(10 if p == 2 else 2)]
v = set()
def m(h, t):
hx, hy = h
tx, ty = t
if ((hx - tx) > 1) or ((hx - tx) == 1 and abs(hy-ty) > 1):
dx = 1
elif ((hx - tx) < -1) or ((hx - tx) == -1 and abs(hy-ty) > 1):
dx = -1
else:
dx = 0
if ((hy - ty) > 1) or ((hy - ty) == 1 and abs(hx-tx) > 1):
dy = 1
elif ((hy - ty) < -1) or ((hy - ty) == -1 and abs(hx-tx) > 1):
dy = -1
else:
dy = 0
return (tx + dx, ty + dy)
for line in data:
d, n = line.split()
for i in range(int(n)):
h = r[0]
if d == 'U':
h = (h[0], h[1]-1)
elif d == 'D':
h = (h[0], h[1]+1)
elif d == 'L':
h = (h[0]-1, h[1])
elif d == 'R':
h = (h[0]+1, h[1])
else:
assert False
r[0] = h
for i in range(len(r)-1):
k = r[i+1]
h = r[i]
t = m(h, k)
r[i+1] = t
v.add(r[-1])
print(len(v))
part(1)
part(2)