#!/usr/bin/python3 import fileinput rope = [] for i in range(0, 10): rope.append([0, 0]) visit = {(0, 0)} # Head index, tail index, direction def move(hi, ti, dir): global rope hx, hy = rope[hi] tx, ty = rope[ti] # Move the head (if this is in fact the head) if dir == 'R': hx += 1 elif dir == 'L': hx -= 1 elif dir == 'D': hy += 1 elif dir == 'U': hy -= 1 # Now move the tail if necessary if (abs(tx - hx) <= 1) and (abs(ty - hy) <= 1): pass elif tx == hx: if ty < hy: ty += 1 else: ty -= 1 elif ty == hy: if tx < hx: tx += 1 else: tx -= 1 else: # Tail must move diagonally if tx < hx: tx += 1 else: tx -= 1 if ty < hy: ty += 1 else: ty -= 1 # Update rope rope[hi] = [hx, hy] rope[ti] = [tx, ty] for line in fileinput.input(): dir, dist = line.rstrip().split(' ') for i in range(0, int(dist)): # Head moves in the specified direction. # The rest of the rope just follows along. move(0, 1, dir) for j in range(1, len(rope) - 1): move(j, j+1, "") visit.add(tuple(rope[-1])) print(len(visit))