-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day07Task2.py
61 lines (45 loc) · 1.84 KB
/
Day07Task2.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
class worker:
def __init__(self):
self.current_task = ''
self.finish_time = 0
with open("/Users/DominikKloepfer/Documents/workspace/Personal_Projects/Advent of Code 2018/Day 7/day7_input1.txt") as input:
conditions = [line.strip().split(' ') for line in input]
steps = {} # each key has value [[predecessors], [successors]]
for condition in conditions:
if not condition[1] in steps:
steps[condition[1]] = [[], [condition[7]]]
else:
steps[condition[1]][1].append(condition[7])
if not condition[7] in steps:
steps[condition[7]] = [[condition[1]], []]
else:
steps[condition[7]][0].append(condition[1])
workers = [worker() for w in range(0, 5)]
proper_time = -1
steps_to_do = len(steps)
while steps_to_do > 0:
next_finish = 100000
for worker in workers:
if worker.finish_time > proper_time and worker.finish_time < next_finish:
next_finish = worker.finish_time
proper_time = next_finish
for worker in workers:
if worker.finish_time == proper_time and worker.current_task != '':
for successor in steps[worker.current_task][1]:
steps[successor][0].remove(worker.current_task)
steps_to_do -= 1
i = 0
while i < len(steps):
step = sorted(steps.keys())[i]
if steps[step][0] == [] and not all(worker.finish_time > proper_time for worker in workers):
print(step, proper_time + 60 + i +1)
# assign step
for worker in workers:
if worker.finish_time <= proper_time:
worker.current_task = step
worker.finish_time = proper_time + 60 + i+1
break
steps[step][0] = ['Done']
i = -1 # returns to beginning of steps
i += 1
print(proper_time)