day 7 refactor
This commit is contained in:
parent
7348f78a09
commit
2b24356087
|
@ -0,0 +1 @@
|
||||||
|
__pycache__
|
48
7/a.py
48
7/a.py
|
@ -1,12 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
from time import sleep
|
|
||||||
from pprint import pprint
|
|
||||||
from collections import namedtuple, defaultdict
|
|
||||||
|
|
||||||
|
|
||||||
# Node = NamedTuple("Node", "id blocks")
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -17,58 +11,24 @@ def main():
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
_, stepid, _, _, _, _, _, blocks, _, _ = line.split()
|
_, stepid, _, _, _, _, _, blocks, _, _ = line.split()
|
||||||
relations[blocks].update([stepid])
|
relations[blocks].update([stepid])
|
||||||
# relations[stepid].append(blocks)
|
|
||||||
steps.update([blocks, stepid])
|
steps.update([blocks, stepid])
|
||||||
|
|
||||||
# relations = dict(relations)
|
|
||||||
|
|
||||||
# pprint(steps)
|
|
||||||
# pprint(dict(relations))
|
|
||||||
|
|
||||||
# while steps:
|
|
||||||
# sleep(1)
|
|
||||||
# print()
|
|
||||||
# print(relations)
|
|
||||||
# print(steps)
|
|
||||||
# for step in list(steps):
|
|
||||||
# if step not in relations or not relations[step]:
|
|
||||||
# print("do ", step)
|
|
||||||
# steps.remove(step)
|
|
||||||
# for otherstep, blockers in relations.items():
|
|
||||||
# if step in blockers:
|
|
||||||
# blockers.remove(step)
|
|
||||||
# break
|
|
||||||
# break
|
|
||||||
# if step in relations:
|
|
||||||
# relations[step].remove(step)
|
|
||||||
|
|
||||||
remaining = sorted(list(steps))
|
remaining = sorted(list(steps))
|
||||||
complete = set()
|
complete = set()
|
||||||
|
|
||||||
while remaining:
|
while remaining:
|
||||||
# print()
|
|
||||||
# print(remaining)
|
|
||||||
# print(dict(relations))
|
|
||||||
# Find unblocked step
|
|
||||||
for step in remaining[:]:
|
for step in remaining[:]:
|
||||||
|
# find unblocked step
|
||||||
if not relations[step]:
|
if not relations[step]:
|
||||||
# print(step, "is unblocked")
|
|
||||||
print(step, end="")
|
print(step, end="")
|
||||||
complete.update(step)
|
complete.update(step)
|
||||||
|
# remove the completed step from the blockers of other steps
|
||||||
for k, v in relations.items():
|
for k, v in relations.items():
|
||||||
if step in v:
|
if step in v:
|
||||||
v.remove(step)
|
v.remove(step)
|
||||||
remaining.remove(step)
|
remaining.remove(step)
|
||||||
break
|
break
|
||||||
|
print()
|
||||||
|
|
||||||
# def donext():
|
|
||||||
# for step in remaining:
|
|
||||||
# pass
|
|
||||||
# pass
|
|
||||||
|
|
||||||
# donext()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
38
7/b.py
38
7/b.py
|
@ -1,11 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
|
||||||
from time import sleep
|
|
||||||
from pprint import pprint
|
|
||||||
from collections import namedtuple, defaultdict
|
|
||||||
|
|
||||||
|
|
||||||
class Job(object):
|
class Job(object):
|
||||||
def __init__(self, id, reqs, duration):
|
def __init__(self, id, reqs, duration):
|
||||||
self.id = id
|
self.id = id
|
||||||
|
@ -27,6 +22,8 @@ class Worker(object):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
num_workers = 5
|
num_workers = 5
|
||||||
|
workers = [Worker() for i in range(0, num_workers)]
|
||||||
|
|
||||||
jobs = {}
|
jobs = {}
|
||||||
|
|
||||||
def makejob(jobid):
|
def makejob(jobid):
|
||||||
|
@ -36,40 +33,31 @@ def main():
|
||||||
with open("input.txt") as f:
|
with open("input.txt") as f:
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
_, stepid, _, _, _, _, _, blocks, _, _ = line.split()
|
_, stepid, _, _, _, _, _, blocks, _, _ = line.split()
|
||||||
|
|
||||||
makejob(stepid)
|
makejob(stepid)
|
||||||
makejob(blocks)
|
makejob(blocks)
|
||||||
|
|
||||||
jobs[blocks].reqs.update([stepid])
|
jobs[blocks].reqs.update([stepid])
|
||||||
|
|
||||||
|
duration = 0 # the answer
|
||||||
workers = [Worker() for i in range(0, num_workers)]
|
|
||||||
empty = set()
|
|
||||||
|
|
||||||
duration = 0
|
|
||||||
completed = set()
|
completed = set()
|
||||||
while True:
|
while True:
|
||||||
print()
|
|
||||||
# pprint(jobs)
|
|
||||||
# print(workers)
|
|
||||||
# do worker accounting
|
# do worker accounting
|
||||||
remainings = [worker.busy for worker in workers if worker.task is not None]
|
remainings = [worker.busy for worker in workers if worker.task is not None]
|
||||||
|
|
||||||
|
# find the next worker(s) to complete
|
||||||
if remainings:
|
if remainings:
|
||||||
next_done = min(remainings)
|
next_done = min(remainings)
|
||||||
# print("next task done in", next_done, "s")
|
|
||||||
duration += next_done
|
|
||||||
|
|
||||||
|
# fast-forward time to that point
|
||||||
|
duration += next_done
|
||||||
for worker in workers:
|
for worker in workers:
|
||||||
if worker.task:
|
if worker.task:
|
||||||
worker.busy -= next_done
|
worker.busy -= next_done
|
||||||
if worker.busy == 0:
|
if worker.busy == 0:
|
||||||
print("finished", worker.task)
|
print("finished task:", worker.task)
|
||||||
completed.update([worker.task.id])
|
completed.update([worker.task.id])
|
||||||
worker.task = None
|
worker.task = None
|
||||||
|
|
||||||
print("total = ", duration)
|
if not jobs and all([worker.task is None for worker in workers]):
|
||||||
|
|
||||||
if not jobs:
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# find available tasks
|
# find available tasks
|
||||||
|
@ -77,9 +65,8 @@ def main():
|
||||||
|
|
||||||
# sort by weight
|
# sort by weight
|
||||||
available.sort(key=lambda x: x.duration)
|
available.sort(key=lambda x: x.duration)
|
||||||
# print("can do:", available)
|
|
||||||
# return
|
|
||||||
|
|
||||||
|
# assign tasks to available workers
|
||||||
for worker in workers:
|
for worker in workers:
|
||||||
if worker.task is None:
|
if worker.task is None:
|
||||||
if not available:
|
if not available:
|
||||||
|
@ -87,9 +74,8 @@ def main():
|
||||||
task = available.pop()
|
task = available.pop()
|
||||||
worker.task = task
|
worker.task = task
|
||||||
worker.busy = task.duration
|
worker.busy = task.duration
|
||||||
# print("assigned {} to {}")
|
print("assigned", task, "to", worker)
|
||||||
print("assigned:", worker)
|
del jobs[task.id] # remove job from work queue
|
||||||
del jobs[task.id]
|
|
||||||
|
|
||||||
print(duration)
|
print(duration)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue