-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
46 lines (34 loc) · 1.2 KB
/
solution.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
from collections import defaultdict
def run(workers):
requirements = defaultdict(set)
missing_steps = set()
finish_order = []
waittimes = [0] * workers
works_on = [None] * workers
with open('input') as f:
for line in f.read().splitlines():
parts = line.split(" ")
requirements[parts[-3]].add(parts[1])
missing_steps |= set([parts[1], parts[-3]])
duration = 0
while len(missing_steps) > 0:
for i in range(workers):
if waittimes[i] == 0:
job_finished = works_on[i]
if job_finished is not None:
finish_order.append(job_finished)
missing_steps = missing_steps - set([job_finished])
requirements = {step : reqs-set([job_finished]) for step,reqs in requirements.items()}
works_on[i] = None
possible = sorted([step for step in missing_steps if len(requirements[step]) == 0 and step not in works_on])
if len(possible) > 0:
works_on[i] = possible[0]
waittimes[i] = 60 + ord(works_on[i]) - ord('A') + 1
waittimes = [max(0, x-1) for x in waittimes]
if len(missing_steps) > 0:
duration += 1
return (duration, "".join(finish_order))
_, job_order = run(1)
print("Part 1\t", job_order)
duration, _ = run(5)
print("Part 2\t", duration)