-
Notifications
You must be signed in to change notification settings - Fork 0
/
201820.py
42 lines (35 loc) · 926 Bytes
/
201820.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
from collections import defaultdict
import math
s = []
with open("201820.txt", "r") as f:
path = f.read().strip()
visited = {(0, 0, 0)} # (x, y, distance)
(x, y, d) = (0, 0, 0)
for c in path[1:-1]:
if c == "N":
y -= 1
d += 1
visited.add((x, y, d))
elif c == "S":
y += 1
d += 1
visited.add((x, y, d))
elif c == "E":
x += 1
d += 1
visited.add((x, y, d))
elif c == "W":
x -= 1
d += 1
visited.add((x, y, d))
elif c == "(":
s.append((x, y, d))
elif c == "|":
(x, y, d) = s[-1]
elif c == ")":
(x, y, d) = s.pop()
min_for_coords = defaultdict(lambda: math.inf)
for x, y, d in visited:
min_for_coords[(x, y)] = min(min_for_coords[(x, y)], d)
print(f"Part one: {max(min_for_coords.values())}")
print(f"Part two: {len([d for d in min_for_coords.values() if d >= 1000])}")