-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-1-fail.py
More file actions
112 lines (97 loc) · 2.58 KB
/
07-1-fail.py
File metadata and controls
112 lines (97 loc) · 2.58 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import re
import operator
from collections import Counter
from sortedcontainers import SortedDict
from sortedcontainers import SortedSet
def clear_rgraph(node):
for n, deps in rgraph.items():
if node in deps:
deps.remove(node)
def clear_graph(node):
for n, next in graph.items():
if node in next:
next.remove(node)
def find_first():
for node in active_nodes:
if node in done:
continue
for rn, deps in rgraph.items():
if len(deps) == 1 and node in deps:
return node
return None
def find_next():
for node in active_nodes:
if node in done:
continue
if node not in rgraph or len(rgraph[node]) == 0:
return node;
return None
pat = re.compile(r'^Step ([A-Z]) must be finished before step ([A-Z]) can begin.$')
graph = {}
rgraph = {}
nodes = SortedSet()
with open('07-1.txt') as f:
for line in f:
m = pat.match(line.strip())
node = m.group(1)
dep = m.group(2)
nodes.add(node)
if node not in graph:
graph[node] = SortedSet()
graph[node].add(dep)
if dep not in rgraph:
rgraph[dep] = SortedSet()
rgraph[dep].add(node)
print('nodes', nodes, 'len', len(nodes))
print('graph', graph)
print('rgraph', rgraph)
steps = []
first_node = None
active_nodes = SortedSet()
for node in nodes:
if node not in rgraph:
print('no deps!', node)
for rn, deps in rgraph.items():
#if len(deps) == 1 and node in deps:
active_nodes.add(node)
print('active', active_nodes)
done = set()
first_node = find_first()
print('first', first_node)
for n in graph[first_node]:
active_nodes.add(n)
steps.append(first_node)
done.add(first_node)
prev_node = first_node
clear_rgraph(first_node)
while True:
print('graph', graph)
print('rgraph', rgraph)
print('done', done)
print('active', active_nodes)
"""
possibles = SortedSet()
for an in active_nodes:
if an in graph:
for n in graph[an]:
if len(rgraph[n]) == 0:
possibles.add(n)
print('new possibles', possibles)
if len(possibles) == 0:
break;
"""
next_node = find_next()
if next_node is None:
break
print('doing step', next_node)
clear_graph(next_node)
clear_rgraph(next_node)
steps.append(next_node)
done.add(next_node)
if next_node not in graph:
break
for n in graph[next_node]:
active_nodes.add(n)
prev_node = next_node
print(''.join(steps))