day 9 python simplify
parent
8cb5086749
commit
117d3d58d5
108
day09/day09.py
108
day09/day09.py
|
@ -1,84 +1,42 @@
|
||||||
H = (0,0)
|
|
||||||
T = (0,0)
|
|
||||||
def go(p, dir):
|
def go(p, dir):
|
||||||
x,y = p
|
x,y = p
|
||||||
if dir == "U":
|
if dir == "U": y += 1
|
||||||
y += 1
|
elif dir == "D": y -= 1
|
||||||
if dir == "D":
|
elif dir == "L": x -= 1
|
||||||
y -= 1
|
elif dir == "R": x += 1
|
||||||
if dir == "L":
|
|
||||||
x -= 1
|
|
||||||
if dir == "R":
|
|
||||||
x += 1
|
|
||||||
return x,y
|
return x,y
|
||||||
|
|
||||||
def follow():
|
def follow(h, t):
|
||||||
if H == T:
|
if abs(h[0] - t[0]) > 1 or abs(h[1] - t[1]) > 1:
|
||||||
return T
|
|
||||||
if abs(H[0] - T[0]) <= 1 and abs(H[1] - T[1]) <= 1:
|
|
||||||
return T
|
|
||||||
if H[1] == T[1]:
|
|
||||||
x,y = T
|
|
||||||
x = H[0] - sgn(H[0] - T[0])
|
|
||||||
return x,y
|
|
||||||
if H[0] == T[0]:
|
|
||||||
x,y = T
|
|
||||||
y = H[1] - sgn(H[1]-T[1])
|
|
||||||
return x,y
|
|
||||||
# diagonal
|
|
||||||
x,y = T
|
|
||||||
x += sgn(H[0] - T[0])
|
|
||||||
y += sgn(H[1] - T[1])
|
|
||||||
return x,y
|
|
||||||
|
|
||||||
def follow2(h, t):
|
|
||||||
if h == t:
|
|
||||||
return t
|
|
||||||
if abs(h[0] - t[0]) <= 1 and abs(h[1] - t[1]) <= 1:
|
|
||||||
return t
|
|
||||||
if h[1] == t[1]:
|
|
||||||
x,y = t
|
x,y = t
|
||||||
x = h[0] - sgn(h[0] - t[0])
|
x += sgn(h[0] - t[0])
|
||||||
return x,y
|
y += sgn(h[1] - t[1])
|
||||||
if h[0] == t[0]:
|
t = x,y
|
||||||
x,y = t
|
return t
|
||||||
y = h[1] - sgn(h[1]-t[1])
|
|
||||||
return x,y
|
|
||||||
# diagonal
|
|
||||||
x,y = t
|
|
||||||
x += sgn(h[0] - t[0])
|
|
||||||
y += sgn(h[1] - t[1])
|
|
||||||
return x,y
|
|
||||||
|
|
||||||
def sgn(x):
|
def sgn(x):
|
||||||
if x==0: return 0
|
return (x > 0) - (x < 0)
|
||||||
if x > 0: return 1
|
|
||||||
return -1
|
|
||||||
|
|
||||||
visited = set([T])
|
def solve(n):
|
||||||
for line in open("input"):
|
R = [(0,0)]*n
|
||||||
dir, count = line.split()
|
visited = set([R[-1]])
|
||||||
count = int(count)
|
for line in open("input"):
|
||||||
for _ in range(count):
|
dir, count = line.split()
|
||||||
H = go(H, dir)
|
for _ in range(int(count)):
|
||||||
T = follow()
|
R[0] = go(R[0], dir)
|
||||||
visited.add(T)
|
for i in range(1, len(R)):
|
||||||
#print(H,T)
|
t = follow(R[i-1], R[i])
|
||||||
print(len(visited))
|
if t == R[i]:
|
||||||
|
# optimization: if a knot doesn't move,
|
||||||
|
# none of the knots after it will either
|
||||||
|
break
|
||||||
|
R[i] = t
|
||||||
|
visited.add(R[-1])
|
||||||
|
#print(R)
|
||||||
|
return len(visited)
|
||||||
|
|
||||||
R = [(0,0)]*10
|
A1 = solve(2)
|
||||||
visited = set([R[-1]])
|
print(A1)
|
||||||
for line in open("input"):
|
|
||||||
dir, count = line.split()
|
A2 = solve(10)
|
||||||
count = int(count)
|
print(A2)
|
||||||
for _ in range(count):
|
|
||||||
R[0] = go(R[0], dir)
|
|
||||||
for i in range(1,len(R)):
|
|
||||||
t = follow2(R[i-1], R[i])
|
|
||||||
if t == R[i]:
|
|
||||||
break
|
|
||||||
R[i] = t
|
|
||||||
visited.add(R[-1])
|
|
||||||
print(R)
|
|
||||||
print(len(visited))
|
|
||||||
|
|
Loading…
Reference in New Issue