adventofcode-2020/12/part1

44 lines
899 B
Python
Executable File

#!/usr/bin/python3
x = 0 # -W, +E
y = 0 # -S, +N
dir = 0 # E=0, CCW
def dir2v(dir):
if dir == 0:
return (1, 0)
elif dir == 90:
return (0, 1)
elif dir == 180:
return (-1, 0)
elif dir == 270:
return (0, -1)
else:
raise RuntimeError(dir)
with open("input") as fh:
for ln in fh:
op = ln[0]
arg = int(ln[1:])
if op == "F":
v = dir2v(dir)
x += v[0] * arg
y += v[1] * arg
elif op == "L":
dir = (dir + arg) % 360
elif op == "R":
dir = (dir - arg) % 360
elif op == "E":
x += arg
elif op == "W":
x -= arg
elif op == "N":
y += arg
elif op == "S":
y -= arg
else:
raise RuntimeError(op)
print(op, arg, x, y, dir)
print(abs(x) + abs(y))