lines = open("input.txt", "r").read().splitlines() class Knots: def __init__(self): self.x = 0 self.y = 0 self.pos = [] def visited(self): return len(set(self.pos)) k = [] for i in range(10): k.append(Knots()) k[i].pos.append((0,0)) def move(h, t): if abs(h.x - t.x) <= 1 and abs(h.y - t.y) <= 1: pass elif abs(h.x - t.x) > 1 and abs(h.y - t.y) == 0: if h.x > t.x: t.x += 1 else: t.x -= 1 elif abs(h.x - t.x) == 0 and abs(h.y - t.y) > 1: if h.y > t.y: t.y += 1 else: t.y -= 1 else: if h.x > t.x and h.y > t.y : t.x += 1 t.y += 1 elif h.x > t.x and h.y < t.y: t.x += 1 t.y -= 1 elif h.x < t.x and h.y < t.y: t.x -= 1 t.y -= 1 else: t.x -= 1 t.y += 1 t.pos.append((t.x,t.y)) for line in lines: cmd = line.split(" ") if cmd[0] == "R": for _ in range(int(cmd[1])): k[0].x = k[0].x + 1 for i in range(1, 10): move(k[i-1], k[i]) if cmd[0] == "L": for _ in range(int(cmd[1])): k[0].x = k[0].x - 1 for i in range(1, 10): move(k[i-1], k[i]) if cmd[0] == "U": for _ in range(int(cmd[1])): k[0].y = k[0].y + 1 for i in range(1, 10): move(k[i-1], k[i]) if cmd[0] == "D": for _ in range(int(cmd[1])): k[0].y = k[0].y - 1 for i in range(1, 10): move(k[i-1], k[i]) print(k[-1].visited())