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)) t= Knots() h = Knots() 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)) t.pos.append((0,0)) for line in lines: cmd = line.split(" ") if cmd[0] == "R": for i in range(int(cmd[1])): h.x = h.x + 1 move(h, t) if cmd[0] == "L": for i in range(int(cmd[1])): h.x = h.x - 1 move(h, t) if cmd[0] == "U": for i in range(int(cmd[1])): h.y = h.y + 1 move(h, t) if cmd[0] == "D": for i in range(int(cmd[1])): h.y = h.y - 1 move(h, t) print(t.visited())