From 8cb5086749c9aacaf620a7ab23fb98ce9e2ae90b Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Thu, 8 Dec 2022 21:24:52 -0800 Subject: [PATCH] day 9 python part 2 --- day09/day09.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/day09/day09.py b/day09/day09.py index bfeb529..7fa57b1 100644 --- a/day09/day09.py +++ b/day09/day09.py @@ -31,6 +31,25 @@ def follow(): 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 = 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 sgn(x): if x==0: return 0 if x > 0: return 1 @@ -44,7 +63,22 @@ for line in open("input"): H = go(H, dir) T = follow() visited.add(T) - print(H,T) + #print(H,T) print(len(visited)) +R = [(0,0)]*10 +visited = set([R[-1]]) +for line in open("input"): + dir, count = line.split() + count = int(count) + 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))