aoc2022/d09/part2.py

30 lines
755 B
Python

import sys
rope = [(0,0) for _ in range(10)]
P = [{(0,0)} for _ in range(10) ]
D = {"R":(1,0),"L":(-1,0),"U":(0,1),"D":(0,-1)}
for l in sys.stdin.read().splitlines():
M, q = l.split(" ")
q = int(q)
for k in range(q):
(x,y) = rope[0]
(dx,dy) = D[M]
rope[0] = (x+dx, y+dy)
for i in range(1,10):
(Hx,Hy) = rope[i-1]
(Tx,Ty) = rope[i]
dh = Hx-Tx # tension horizontale
dv = Hy-Ty # tension vertivale
if (abs(dh)>1):
Tx += 1 if dh > 0 else -1
if (abs(dv)>0):
Ty += 1 if dv > 0 else -1
elif (abs(dv)>1):
Ty += 1 if dv > 0 else -1
if (abs(dh)>0):
Tx += 1 if dh > 0 else -1
P[i].add((Tx,Ty))
rope[i] = (Tx,Ty)
print(*(len(p) for p in P))