-
Notifications
You must be signed in to change notification settings - Fork 0
/
7b.py
77 lines (56 loc) · 1.34 KB
/
7b.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
import numpy as np
import re
file_name = "7a.txt"
stepre = re.compile(r'(?<=tep )\w')
def traverse(d):
worker = []
available = [i for i in d if len(d[i]['g']) == 0]
pathcomplete = []
time = 0
while available or worker:
time += 1
available = sorted(available, reverse=True)
while (len(available) > 0) and len(worker) <= 5 :
n = available.pop()
print("starting ", n, time)
worker.append( n )
completedworkers = []
for w in worker:
d[w]['to_complete'] = d[w]['to_complete'] - 1
if d[w]['to_complete'] == 0:
completedworkers.append(w)
pathcomplete.append(w)
for w in completedworkers:
worker.remove(w)
for i in d:
if w in d[i]['g']:
d[i]['g'].remove(w)
if len(d[i]['g']) == 0:
available.append(i)
print( "final time", time )
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] = {'g': [], 'to_complete': 60 + ord(r) - 64 }
for r in rows:
step = r[1]
gate = r[0]
if step not in d:
d[step]['g'] = [gate]
else:
d[step]['g'].append(gate)
traverse(d)
return
def main():
with open(file_name, 'r') as f:
x = f.readlines()
print(len(x))
process(x)
main()