From 2b24356087b80636afcf46600df3092fd4447d4f Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 6 Dec 2018 22:06:56 -0800 Subject: [PATCH] day 7 refactor --- .gitignore | 1 + 1/{1.py => a.py} | 0 1/{1b.py => b.py} | 0 2/{2.py => a.py} | 0 2/{2b.py => b.py} | 0 7/a.py | 48 ++++------------------------------------------- 7/b.py | 38 ++++++++++++------------------------- 7 files changed, 17 insertions(+), 70 deletions(-) create mode 100644 .gitignore rename 1/{1.py => a.py} (100%) rename 1/{1b.py => b.py} (100%) rename 2/{2.py => a.py} (100%) rename 2/{2b.py => b.py} (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/1/1.py b/1/a.py similarity index 100% rename from 1/1.py rename to 1/a.py diff --git a/1/1b.py b/1/b.py similarity index 100% rename from 1/1b.py rename to 1/b.py diff --git a/2/2.py b/2/a.py similarity index 100% rename from 2/2.py rename to 2/a.py diff --git a/2/2b.py b/2/b.py similarity index 100% rename from 2/2b.py rename to 2/b.py diff --git a/7/a.py b/7/a.py index 9ee7d48..9fe2556 100755 --- a/7/a.py +++ b/7/a.py @@ -1,12 +1,6 @@ #!/usr/bin/env python3 - -from time import sleep -from pprint import pprint -from collections import namedtuple, defaultdict - - -# Node = NamedTuple("Node", "id blocks") +from collections import defaultdict def main(): @@ -17,58 +11,24 @@ def main(): for line in f.readlines(): _, stepid, _, _, _, _, _, blocks, _, _ = line.split() relations[blocks].update([stepid]) - # relations[stepid].append(blocks) 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)) complete = set() while remaining: - # print() - # print(remaining) - # print(dict(relations)) - # Find unblocked step for step in remaining[:]: + # find unblocked step if not relations[step]: - # print(step, "is unblocked") print(step, end="") complete.update(step) + # remove the completed step from the blockers of other steps for k, v in relations.items(): if step in v: v.remove(step) remaining.remove(step) break - - - # def donext(): - # for step in remaining: - # pass - # pass - - # donext() - + print() if __name__ == '__main__': diff --git a/7/b.py b/7/b.py index 049b7c5..1c13ac3 100755 --- a/7/b.py +++ b/7/b.py @@ -1,11 +1,6 @@ #!/usr/bin/env python3 -from time import sleep -from pprint import pprint -from collections import namedtuple, defaultdict - - class Job(object): def __init__(self, id, reqs, duration): self.id = id @@ -27,6 +22,8 @@ class Worker(object): def main(): num_workers = 5 + workers = [Worker() for i in range(0, num_workers)] + jobs = {} def makejob(jobid): @@ -36,40 +33,31 @@ def main(): with open("input.txt") as f: for line in f.readlines(): _, stepid, _, _, _, _, _, blocks, _, _ = line.split() - makejob(stepid) makejob(blocks) - jobs[blocks].reqs.update([stepid]) - - workers = [Worker() for i in range(0, num_workers)] - empty = set() - - duration = 0 + duration = 0 # the answer completed = set() while True: - print() - # pprint(jobs) - # print(workers) # do worker accounting remainings = [worker.busy for worker in workers if worker.task is not None] + + # find the next worker(s) to complete if 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: if worker.task: worker.busy -= next_done if worker.busy == 0: - print("finished", worker.task) + print("finished task:", worker.task) completed.update([worker.task.id]) worker.task = None - print("total = ", duration) - - if not jobs: + if not jobs and all([worker.task is None for worker in workers]): break # find available tasks @@ -77,9 +65,8 @@ def main(): # sort by weight available.sort(key=lambda x: x.duration) - # print("can do:", available) - # return + # assign tasks to available workers for worker in workers: if worker.task is None: if not available: @@ -87,9 +74,8 @@ def main(): task = available.pop() worker.task = task worker.busy = task.duration - # print("assigned {} to {}") - print("assigned:", worker) - del jobs[task.id] + print("assigned", task, "to", worker) + del jobs[task.id] # remove job from work queue print(duration)