-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
45 lines (39 loc) · 1.09 KB
/
day12.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
input_filename = __file__.split('.')[0] + ".input"
with open(input_filename) as f:
raw = f.read().strip().split('\n')
EAST = 0
SOUTH = 1
WEST = 2
NORTH = 3
facing = EAST
pos_east = 0
pos_north = 0
for instruction in raw:
action, value = instruction[0], int(instruction[1:])
if action == 'N':
pos_north += value
elif action == 'S':
pos_north -= value
elif action == 'E':
pos_east += value
elif action == 'W':
pos_east -= value
elif action == 'F':
if facing == EAST:
pos_east += value
elif facing == WEST:
pos_east -= value
elif facing == NORTH:
pos_north += value
elif facing == SOUTH:
pos_north -= value
else:
raise ValueError
else:
value = value / 90
if action == 'L':
value = -value
facing = (facing + value) % 4
# print(f"{instruction} - ({pos_north}, {pos_east}) facing {facing}")
print(f"Facing {facing} at ({pos_north}, {pos_east})")
print(f"Distance from start: {abs(pos_east) + abs(pos_north)}")