#!/usr/bin/python "Inspired by an AoC puzzle: the objective is to move a turtle." from __future__ import division, print_function import sys c = [(0, 1), (1, 0), (0, -1), (-1, 0)] # compass d = 1 # direction x, y = 0, 0 # location s = lambda (x0, y0), (x1, y1): (x0+x1, y0+y1) # sum vectors m = lambda s, (x0, y0): (s*x0, s*y0) # multiply vector b = 'NESW' for line in sys.stdin: if not line.strip(): continue k, a = line[0], int(line[1:]) # command, arg if k == 'F': x, y = s((x, y), m(a, c[d])) elif k == 'R': d = (d + a // 90) % 4 elif k == 'L': d = (d - a // 90) % 4 elif k in b: x, y = s((x, y), m(a, c[b.index(k)])) else: print("did not grok", line) print("at", x, y) print(abs(x) + abs(y))