-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday07.py
executable file
·79 lines (56 loc) · 1.5 KB
/
day07.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
#!/usr/bin/env python3
import sys
import copy
import heapq
from collections import defaultdict
def lex_toposort(graph, queue):
# Could easily be done using networkx.
# Implemented just for fun and educational purposes.
res = ''
while queue:
cur = heapq.heappop(queue)
res += cur
for n in graph[cur][1]:
graph[n][0] -= 1
if graph[n][0] == 0:
heapq.heappush(queue, n)
return res
def work(graph, queue, duration, max_workers):
total_time = 0
workers = []
done = set()
while workers or queue:
while queue and len(workers) < max_workers:
job = heapq.heappop(queue)
heapq.heappush(workers, [duration[job], job])
t = workers[0][0]
total_time += t
for w in workers:
w[0] -= t
while workers and workers[0][0] == 0:
job = heapq.heappop(workers)[1]
done.add(job)
for n in graph[job][1]:
graph[n][0] -= 1
if graph[n][0] == 0:
heapq.heappush(queue, n)
return total_time
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
graph = defaultdict(lambda: [0, set()])
for line in fin:
s = line.split()
graph[s[1]][1].add(s[7])
graph[s[7]][1].add(s[1])
graph[s[7]][0] += 1
roots = []
for letter, node in graph.items():
if node[0] == 0:
heapq.heappush(roots, letter)
order = lex_toposort(copy.deepcopy(graph), roots[:])
print('Part 1:', order)
durations = {}
for c in graph:
durations[c] = ord(c) - ord('A') + 61
total = work(graph, roots, durations, 5)
print('Part 2:', total)