#!/usr/bin/python3 import fileinput hx = 0 hy = 0 tx = 0 ty = 0 visit = {(tx, ty)} for line in fileinput.input(): dir, dist = line.rstrip().split(' ') for i in range(0, int(dist)): # Move 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 tail position tracking visit.add((tx, ty)) print(len(visit))