-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_part_2.py
28 lines (25 loc) · 997 Bytes
/
12_part_2.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
from pathlib import Path
with (Path(__file__).parent / "data" / "12.txt").open() as fin:
waypoint = [1, 10]
ship = [0, 0]
for instruction in map(str.strip, fin):
action, i = instruction[0], int(instruction[1:])
if action == "N":
waypoint[0] += i
elif action == "S":
waypoint[0] -= i
elif action == "E":
waypoint[1] += i
elif action == "W":
waypoint[1] -= i
elif action == "F":
ship[0] += i * waypoint[0]
ship[1] += i * waypoint[1]
elif i == 180:
waypoint[0] = -waypoint[0]
waypoint[1] = -waypoint[1]
elif (action == "R" and i == 90) or (action == "L" and i == 270):
waypoint[0], waypoint[1] = -waypoint[1], waypoint[0]
elif (action == "R" and i == 270) or (action == "L" and i == 90):
waypoint[0], waypoint[1] = waypoint[1], -waypoint[0]
print(f"manhattan distance: {sum(map(abs, ship))}")