-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_12.py
63 lines (51 loc) · 1.91 KB
/
12_12.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np
def day12_1():
instructions = open("num12.txt").read().split()
cpos = np.array([0, 0])
dir = np.e ** (1j * np.pi)
for (inst, *rest) in instructions:
if inst == "W":
cpos[0] += int("".join(rest))
elif inst == "E":
cpos[0] -= int("".join(rest))
elif inst == "N":
cpos[1] += int("".join(rest))
elif inst == "S":
cpos[1] -= int("".join(rest))
elif inst == "F":
amt = int("".join(rest))
cpos += [int(dir.real) * amt, int(dir.imag) * amt]
elif inst == "L":
dir /= np.e ** (1j * int("".join(rest)) / 180 * np.pi)
elif inst == "R":
dir *= np.e ** (1j * int("".join(rest)) / 180 * np.pi)
return abs(int(cpos[0])) + abs(int(cpos[1]))
import math
def day12_2():
instructions = [x for x in open("num12.txt").read().split() if x]
ship = np.array([0., 0.])
wayp = np.array([-10., 1.])
for (inst, *rest) in instructions:
if inst == "W":
wayp[0] += int("".join(rest))
elif inst == "E":
wayp[0] -= int("".join(rest))
elif inst == "N":
wayp[1] += int("".join(rest))
elif inst == "S":
wayp[1] -= int("".join(rest))
elif inst == "F":
amt = int("".join(rest))
ship += amt * wayp
elif inst == "L":
ang = -int("".join(rest)) * np.pi / 180
dp = np.linalg.norm(wayp) * np.e ** (1j * (ang + math.atan2(wayp[1], wayp[0])))
wayp[:] = round(dp.real), round(dp.imag)
elif inst == "R":
ang = int("".join(rest)) * np.pi / 180
dp = np.linalg.norm(wayp) * np.e ** (1j * (ang + math.atan2(wayp[1], wayp[0])))
wayp[:] = round(dp.real), round(dp.imag)
return abs(int(ship[0])) + abs(int(ship[1]))
if __name__ == '__main__':
print(day12_1())
print(day12_2())