-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht20.py
More file actions
44 lines (31 loc) · 759 Bytes
/
t20.py
File metadata and controls
44 lines (31 loc) · 759 Bytes
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
#!python
import networkx as nx
import sys
from collections import deque
assert len(sys.argv) == 2
inp = open(sys.argv[1]).read().strip()[1:-1]
x = y = 0
gr = nx.Graph()
move = {'N':(0,1),'S':(0,-1),'E':(1,0),'W':(-1,0)}
pos = (0,0)
gr.add_node(pos)
q = deque()
for char in inp:
if char == '(':
q.append(pos)
elif char == '|':
pos = q[-1]
elif char == ')':
q.pop()
else:
newpos = (pos[0] + move[char][0], pos[1] + move[char][1])
gr.add_edge(pos, newpos)
pos = newpos
paths = nx.single_source_dijkstra_path_length(gr, (0, 0))
ans = max(paths.values())
print(f'Part1: {ans}')
over_1000 = 0
for p in paths.values():
if p >= 1000:
over_1000 += 1
print(f'Part2: {over_1000}')