-
Notifications
You must be signed in to change notification settings - Fork 0
/
12th.py
113 lines (102 loc) · 2.78 KB
/
12th.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import numpy as np
file = open('12-input.txt', 'r')
compass = ["N", "E", "S", "W"]
dir = 1
pos = np.array([0.,0.])
while True:
line = file.readline()
if not line:
break
action = line[0]
nr = int(line[1:])
# print (line, action, nr)
if action == "F":
action = compass[int(dir)] # Part 1
#print ("F: ", dir)
if action == "L":
dir = (dir - nr/90) % 4
#print ("L: ", dir)
elif action == "R":
dir = (dir + nr/90) % 4
#print ("R: ",dir)
elif action == "N":
pos[0] += nr
#print ("N", pos)
elif action == "S":
pos[0] -= nr
#print ("S", pos)
elif action == "E":
pos[1] += nr
#print ("E", pos)
elif action == "W":
pos[1] -= nr
#print ("W", pos)
file.close()
print("Part 1")
print("final diraction: ", compass[int(dir)])
print("final position: ")
if pos[0] < 0:
print (abs(pos[0]), " units south")
elif pos[0] > 0:
print (pos[0], " units north")
if pos[1] < 0:
print (abs(pos[1]), " units west")
elif pos[1] > 0:
print (pos[1], " units east")
print("manhatten position: ", abs(pos[0]) + abs(pos[1]))
# Part 2
file = open('12-input.txt', 'r')
pos = np.array([0.,0.])
waypoint = np.array([1.,10.])
rotL = np.array([[0.,1.],[-1.,0.]])
rotR = np.array([[0.,-1.], [1.,0.]])
while True:
line = file.readline()
if not line:
break
action = line[0]
nr = int(line[1:])
# print (line, action, nr)
if action == "F":
pos += waypoint * nr # Part 2
#print (line, pos)
elif action == "L":
if nr == 90:
waypoint = np.dot(rotL, waypoint)
elif nr == 180:
waypoint = np.dot(np.dot(rotL,rotL), waypoint)
elif nr == 270:
waypoint = np.dot(np.dot(np.dot(rotL,rotL),rotL), waypoint)
#print (line, waypoint)
elif action == "R":
if nr == 90:
waypoint = np.dot(rotR, waypoint)
elif nr == 180:
waypoint = np.dot(np.dot(rotR,rotR), waypoint)
elif nr == 270:
waypoint = np.dot(np.dot(np.dot(rotR,rotR),rotR), waypoint)
#print (line, waypoint)
elif action == "N":
waypoint[0] += nr
#print (line, waypoint)
elif action == "S":
waypoint[0] -= nr
#print (line, waypoint)
elif action == "E":
waypoint[1] += nr
#print (line, waypoint)
elif action == "W":
waypoint[1] -= nr
#print (line, waypoint)
file.close()
print ("part 2")
print("final position: ")
if pos[0] < 0:
print (abs(pos[0]), " units south")
elif pos[0] > 0:
print (pos[0], " units north")
if pos[1] < 0:
print (abs(pos[1]), " units west")
elif pos[1] > 0:
print (pos[1], " units east")
print("manhatten position: ", abs(pos[0]) + abs(pos[1]))