-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7a.py
70 lines (46 loc) · 998 Bytes
/
7a.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
import numpy as np
import re
file_name = "7a.txt"
#stepre = re.compile(r'tep \w')
stepre = re.compile(r'(?<=tep )\w')
def traverse(d):
available = [i for i in d if len(d[i]) == 0]
path = []
while available:
available = sorted(available, reverse=True)
n = available.pop()
path.append(n)
for i in d:
if n in d[i]:
d[i].remove(n)
if len(d[i]) == 0:
print( "now available ", i )
available.append(i)
print(path, available)
print( ''.join(path) )
return
def process(x):
rows = []
for r in x:
step = stepre.findall( r )
rows.append(step)
allnodes = [i[0] for i in rows] + [i[1] for i in rows]
allnodesset = list(set([i for i in allnodes]))
d = {}
for r in allnodesset:
d[r] = []
for r in rows:
step = r[1]
gate = r[0]
if step not in d:
d[step] = [gate]
else:
d[step].append(gate)
traverse(d)
return
def main():
with open(file_name, 'r') as f:
x = f.readlines()
print(len(x))
process(x)
main()