-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol12.py
148 lines (107 loc) · 3.22 KB
/
sol12.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import utils
def initialise():
global instructions
instructions = utils.loadInputFile("input_12.txt")
reset()
def reset():
global heading, north, east, waypointNorth, waypointEast
heading = 90 # The ship starts by facing east (90)
north = 0
east = 0
waypointNorth = 1
waypointEast = 10
def rotate(amount):
global heading
heading += amount
if heading >= 360:
heading -= 360
elif heading < 0:
heading += 360
def move(heading, amount):
global north, east
if heading == 0:
north += amount
elif heading == 90:
east += amount
elif heading == 180:
north -= amount
elif heading == 270:
east -= amount
def moveForward(amount):
global heading
move(heading, amount)
def distanceFromStart():
global north, east
return abs(north) + abs(east)
def moveWaypoint(heading, amount):
global waypointNorth, waypointEast
if heading == 0:
waypointNorth += amount
elif heading == 90:
waypointEast += amount
elif heading == 180:
waypointNorth -= amount
elif heading == 270:
waypointEast -= amount
def rotateWaypoint(direction, amount):
global waypointNorth, waypointEast
if direction == "L":
if amount == 90:
waypointNorth, waypointEast = waypointEast, -waypointNorth
elif amount == 270:
waypointNorth, waypointEast = -waypointEast, waypointNorth
if direction == "R":
if amount == 90:
waypointNorth, waypointEast = -waypointEast, waypointNorth
elif amount == 270:
waypointNorth, waypointEast = waypointEast, -waypointNorth
if amount == 180:
waypointNorth = -waypointNorth
waypointEast = -waypointEast
def moveShipToWaypoint(amount):
global north, east, waypointNorth, waypointEast
north += amount * waypointNorth
east += amount * waypointEast
# viewpoint stays relative, so coords unchanged
def part1():
global instructions
for instruction in instructions:
action, value = instruction[:1], int(instruction[1:])
if action == "L":
rotate(-1 * value)
elif action == "R":
rotate(1 * value)
elif action == "F":
moveForward(value)
elif action == "N":
move(0, value)
elif action == "E":
move(90, value)
elif action == "S":
move(180, value)
elif action == "W":
move(270, value)
return distanceFromStart()
def part2():
global instructions
for instruction in instructions:
action, value = instruction[:1], int(instruction[1:])
if action == "L":
rotateWaypoint("L", value)
elif action == "R":
rotateWaypoint("R", value)
elif action == "F":
moveShipToWaypoint(value)
elif action == "N":
moveWaypoint(0, value)
elif action == "E":
moveWaypoint(90, value)
elif action == "S":
moveWaypoint(180, value)
elif action == "W":
moveWaypoint(270, value)
return distanceFromStart()
initialise()
print("Part 1:", part1()) # 1294
reset()
print("Part 2:", part2()) # 20592