adventofcode-2020/12/part2

66 lines
1.5 KiB
Plaintext
Raw Normal View History

2020-12-13 21:32:46 +01:00
#!/usr/bin/python3
class V:
def __init__(self, x, y):
self.x = x
self.y = y
def __mul__(self, n):
return V(self.x * n, self.y * n)
def __add__(self, other):
return V(self.x + other.x, self.y + other.y)
def rotate(self, deg):
deg = deg % 360
if deg == 0:
return self
elif deg == 90:
return V(-self.y, self.x)
elif deg == 180:
return V(-self.x, -self.y)
elif deg == 270:
return V(self.y, -self.x)
else:
raise RuntimeError(deg)
def __abs__(self):
return abs(self.x) + abs(self.y)
def __repr__(self):
return f"V({self.x}, {self.y})"
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)
pos = V(0, 0)
v = V(10, 1)
with open("input") as fh:
for ln in fh:
op = ln[0]
arg = int(ln[1:])
if op == "F":
pos += v * arg
elif op == "L":
v = v.rotate(arg)
elif op == "R":
v = v.rotate(-arg)
elif op == "E":
v += V(arg, 0)
elif op == "W":
v += V(-arg, 0)
elif op == "N":
v += V(0, arg)
elif op == "S":
v += V(0, -arg)
else:
raise RuntimeError(op)
print(op, arg, pos, v)
print(abs(pos))