#!/usr/bin/env python import math MOVES = { 'U': ( 0, -1), 'D': ( 0, +1), 'L': (-1, 0), 'R': (+1, 0), } TAIL_POSITIONS = set([]) HEAD = (0, 0) TAIL = (0, 0) def point_add(a, b): return tuple(map(lambda i, j: i + j, a, b)) def move(delta): global TAIL_POSITIONS, HEAD, TAIL last_head = HEAD HEAD = point_add(HEAD, delta) if ed := math.dist(HEAD, TAIL) < 2.0: _ # do nothing elif ed == 2.0: TAIL = point_add(TAIL, delta) else: TAIL = last_head TAIL_POSITIONS.add(TAIL) with open('input') as f: for line in f: direction, count = line.rstrip().split(" ") for _ in range(int(count)): move(MOVES[direction]) print(len(TAIL_POSITIONS))