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"))
|
lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
|
||||||
|
|
||||||
pos = np.array([0, 0], dtype=np.int32)
|
pos = 0
|
||||||
drctn = np.array([1, 0], dtype=np.int32)
|
drctn = 1
|
||||||
targets = np.array([
|
targets = [1j, 1, -1j, -1]
|
||||||
[0, 1],
|
|
||||||
[1, 0],
|
|
||||||
[0, -1],
|
|
||||||
[-1, 0]
|
|
||||||
], dtype=np.int32)
|
|
||||||
target_str = "NESW"
|
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:
|
for line in lines:
|
||||||
if line[0] in target_str:
|
if line[0] in target_str:
|
||||||
pos += direction(line[0]) * line[1]
|
pos += targets[target_str.index(line[0])] * line[1]
|
||||||
elif line[0] in "LR":
|
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:
|
else:
|
||||||
pos += drctn * line[1]
|
pos += drctn * line[1]
|
||||||
|
|
||||||
print(sum(np.abs(pos)))
|
print(int(abs(pos.real) + abs(pos.imag)))
|
||||||
|
|||||||
@@ -1,34 +1,17 @@
|
|||||||
import numpy as np
|
|
||||||
|
|
||||||
lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
|
lines = ((x[0], int(x[1:].strip())) for x in open("input.txt"))
|
||||||
|
|
||||||
pos = np.array([0, 0], dtype=np.int32)
|
pos = 0
|
||||||
drctn = np.array([10, 1], dtype=np.int32)
|
drctn = 10+1j
|
||||||
targets = np.array([
|
targets = [1j, 1, -1j, -1]
|
||||||
[0, 1],
|
|
||||||
[1, 0],
|
|
||||||
[0, -1],
|
|
||||||
[-1, 0]
|
|
||||||
], dtype=np.int32)
|
|
||||||
target_str = "NESW"
|
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:
|
for line in lines:
|
||||||
if line[0] in target_str:
|
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":
|
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:
|
else:
|
||||||
pos += drctn * line[1]
|
pos += drctn * line[1]
|
||||||
|
|
||||||
print(sum(np.abs(pos)))
|
print(int(abs(pos.real) + abs(pos.imag)))
|
||||||
|
|||||||
Reference in New Issue
Block a user