Day 12 (complex numbers)
This commit is contained in:
@@ -1,34 +1,17 @@
|
||||
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)
|
||||
pos = 0
|
||||
drctn = 10+1j
|
||||
targets = [1j, 1, -1j, -1]
|
||||
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]
|
||||
drctn += targets[target_str.index(line[0])] * line[1]
|
||||
elif line[0] in "LR":
|
||||
drctn = rotate(drctn, -line[1] if line[0] == "R" else line[1])
|
||||
times = (-line[1] if line[0] == "R" else line[1]) / 90
|
||||
drctn = drctn * 1j ** times
|
||||
else:
|
||||
pos += drctn * line[1]
|
||||
|
||||
print(sum(np.abs(pos)))
|
||||
print(int(abs(pos.real) + abs(pos.imag)))
|
||||
|
||||
Reference in New Issue
Block a user