-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.py
49 lines (38 loc) · 1.54 KB
/
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
import os
inputFile = open(os.path.dirname(__file__) + '/input.txt', 'r')
lines = [line.rstrip('\n') for line in inputFile]
inputFile.close()
def part_one():
directions = [(0, 1), (-1, 0), (0, -1), (1, 0)]
direction_index = 0
pos = (0, 0)
for line in lines:
command = line[0]
value = int(line[1:])
if command == 'L':
direction_index = (direction_index - value // 90 + 4) % 4
elif command == 'R':
direction_index = (direction_index + value // 90) % 4
else:
move_index = {'N': 3, 'S': 1, 'E': 0, 'W': 2, 'F': direction_index}[command]
pos = (pos[0] + value * directions[move_index][0], pos[1] + value * directions[move_index][1])
return abs(pos[0]) + abs(pos[1])
def part_two():
directions = [(0, 1), (-1, 0), (0, -1), (1, 0)]
waypoint = (1, 10)
pos = (0, 0)
for line in lines:
command = line[0]
value = int(line[1:])
if command in ['L', 'R']:
while value:
waypoint = (waypoint[1], -waypoint[0]) if command == 'L' else (-waypoint[1], waypoint[0])
value -= 90
elif command == 'F':
pos = (pos[0] + waypoint[0] * value, pos[1] + waypoint[1] * value)
else:
move_index = {'N': 3, 'S': 1, 'E': 0, 'W': 2}[command]
waypoint = (waypoint[0] + value * directions[move_index][0], waypoint[1] + value * directions[move_index][1])
return abs(pos[0]) + abs(pos[1])
print(f'Part one: {part_one()}')
print(f'Part two: {part_two()}')