Files
python-aoc-2020/day12/part2.py
2020-12-12 11:24:11 +01:00

35 lines
847 B
Python

import numpy as np
lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
pos = np.array([0, 0], dtype=np.int32)
drctn = np.array([10, 1], dtype=np.int32)
targets = np.array([
[0, 1],
[1, 0],
[0, -1],
[-1, 0]
], dtype=np.int32)
target_str = "NESW"
def direction(target):
return targets[target_str.index(target)]
def rotate(current, amount):
angle = np.arctan2(current[1], current[0]) + np.pi / 180 * amount
amount = np.linalg.norm(current)
return np.array([np.rint(np.cos(angle) * amount), np.rint(np.sin(angle) * amount)], dtype=np.int32)
for line in lines:
if line[0] in target_str:
drctn += direction(line[0]) * line[1]
elif line[0] in "LR":
drctn = rotate(drctn, -line[1] if line[0] == "R" else line[1])
else:
pos += drctn * line[1]
print(sum(np.abs(pos)))