generated from alvesvaren/AoC-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.py
65 lines (58 loc) · 1.44 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import aoc
import re
data = aoc.get_input(12).splitlines()
n, e, r = 0, 0, 0
e2, n2 = 0, 0
we, wn = 10, 1
for instruction in data:
direction, value = re.match(r"([NSFWELR])(\d+)", instruction).groups()
value = int(value)
orig_direction = direction
if direction == "F":
r = r % 4
if r == 1:
direction = "N"
elif r == 2:
direction = "W"
elif r == 3:
direction = "S"
elif r == 0:
direction = "E"
e2 += we * value
n2 += wn * value
if direction == "R":
r -= value//90
elif direction == "L":
r += value//90
elif direction == "N":
n += value
elif direction == "S":
n -= value
elif direction == "E":
e += value
elif direction == "W":
e -= value
if orig_direction == "R":
if value == 90:
we, wn = wn, -we
elif value == 270:
we, wn = -wn, we
else:
we, wn = -we, -wn
elif orig_direction == "L":
if value == 90:
we, wn = -wn, we
elif value == 270:
we, wn = wn, -we
else:
we, wn = -we, -wn
elif orig_direction == "N":
wn += value
elif orig_direction == "S":
wn -= value
elif orig_direction == "E":
we += value
elif orig_direction == "W":
we -= value
print("Part 1:", abs(e)+abs(n))
print("Part 2:", abs(e2)+abs(n2))