-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.py
56 lines (42 loc) · 1.34 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
46
47
48
49
50
51
52
53
54
55
56
LEFT = ['E', 'N', 'W', 'S']
LEFT += LEFT
RIGHT = list(reversed(LEFT))
def step(coordinates, direction, distance):
if direction == 'N':
coordinates[0] += distance
if direction == 'S':
coordinates[0] -= distance
if direction == 'E':
coordinates[1] += distance
if direction == 'W':
coordinates[1] -= distance
return coordinates
def navigate(instructions):
coordinates = [0, 0]
facing = 'E'
for i in instructions:
if i[0] in 'NSEW':
coordinates = step(coordinates, i[0], i[1])
elif i[0] == 'L':
degree = i[1] // 90
facing = LEFT[LEFT.index(facing) + degree]
elif i[0] == 'R':
degree = i[1] // 90
facing = RIGHT[RIGHT.index(facing) + degree]
elif i[0] == 'F':
coordinates = step(coordinates, facing, i[1])
else:
raise ValueError("Unknown instruction {}".format(i[0]))
return coordinates
def part1():
with open("input12.txt") as file:
instructions = [(line[0], int(line[1:])) for line in file]
result = navigate(instructions)
print(result)
return result
part1()
def test_navigate():
instructions = [('F', 10), ('N', 3), ('F', 7), ('R', 90), ('F', 11)]
result = navigate(instructions)
assert result[0] == -8
assert result[1] == 17