with open('input.txt') as fh: instructions = [(l[0], int(l.strip()[1:])) for l in fh] def exec1(instructions): # ENWS direction = 0 east = 0 north = 0 for cmd, value in instructions: if cmd == 'E': east += value elif cmd == 'N': north += value elif cmd == 'W': east -= value elif cmd == 'S': north -= value elif cmd == 'R': direction = (direction - (value // 90)) % 4 elif cmd == 'L': direction = (direction + (value // 90)) % 4 elif cmd == 'F': if direction == 0: east += value elif direction == 1: north += value elif direction == 2: east -= value elif direction == 3: north -= value else: assert False, 'invalid direction' else: assert False, 'invalid cmd' print(east, north, abs(east) + abs(north)) def exec2(instructions): east = 0 north = 0 deast = 10 dnorth = 1 for cmd, value in instructions: if cmd == 'E': deast += value elif cmd == 'N': dnorth += value elif cmd == 'W': deast -= value elif cmd == 'S': dnorth -= value elif cmd == 'F': east += deast * value north += dnorth * value else: if cmd == 'R': value = -value a = (value // 90) % 4 for i in range(a): tmp = deast deast = -dnorth dnorth = tmp print(east, north, abs(east) + abs(north)) exec1(instructions) exec2(instructions)