-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday20.py
executable file
·87 lines (64 loc) · 1.41 KB
/
day20.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
from utils import advent
import sys
import networkx as nx
import matplotlib.pyplot as plt
from collections import deque
def log(s, *a):
sys.stderr.write(s.format(*a))
sys.stderr.flush()
def reclog(depth):
def fn(s, *a):
log(' |'*depth + ' ' + s, *a)
return fn
advent.setup(2018, 20)
fin = advent.get_input()
# print(*fin)
##################################################
def dump_graph(G):
lol = nx.spring_layout(G)
nx.draw(G, lol, with_labels=True)
# edge_labels = nx.get_edge_attributes(G, 'path')
# nx.draw_networkx_edge_labels(G, lol, edge_labels)
nx.draw_networkx_edge_labels(G, lol)
plt.show()
def build_graph(s):
g = nx.Graph()
q = deque()
head = (0, 0)
g.add_node(head)
i = 0
l = len(s)
for c in s:
i += 1
log('Building graph: {}/{}\r', i, l)
if c == '(':
q.append(head)
elif c == '|':
head = q[-1]
elif c == ')':
q.pop()
else:
newhead = (head[0] + deltas[c][0], head[1] + deltas[c][1])
g.add_edge(head, newhead)
head = newhead
log('\n')
return g
deltas = {
'N': ( 0, 1),
'S': ( 0, -1),
'E': ( 1, 0),
'W': (-1, 0)
}
directions = fin.read().strip()[1:-1]
graph = build_graph(directions)
paths = nx.single_source_dijkstra_path_length(graph, (0, 0))
ans = max(paths.values())
# dump_graph(graph)
advent.submit_answer(1, ans)
tot = 0
for p in paths.values():
if p >= 1000:
tot += 1
ans2 = tot
advent.submit_answer(2, ans2)