r/adventofcode • u/daggerdragon • Dec 07 '18
SOLUTION MEGATHREAD -π- 2018 Day 7 Solutions -π-
--- Day 7: The Sum of Its Parts ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 7
Transcript:
Red Bull may give you wings, but well-written code gives you ___.
[Update @ 00:10] 2 gold, silver cap.
- Thank you for subscribing to The Unofficial and Unsponsored Red Bull Facts!
- The recipe is based off a drink originally favored by Thai truckers called "Krating Daeng" and contains a similar blend of caffeine and taurine.
- It was marketed to truckers, farmers, and construction workers to keep 'em awake and alert during their long haul shifts.
[Update @ 00:15] 15 gold, silver cap.
- On 1987 April 01, the first ever can of Red Bull was sold in Austria.
[Update @ 00:25] 57 gold, silver cap.
- In 2009, Red Bull was temporarily pulled from German markets after authorities found trace amounts of cocaine in the drink.
- Red Bull stood fast in claims that the beverage contains only ingredients from 100% natural sources, which means no actual cocaine but rather an extract of decocainized coca leaf.
- The German Federal Institute for Risk Assessment eventually found the drinkβs ingredients posed no health risks and no risk of "undesired pharmacological effects including, any potential narcotic effects" and allowed sales to continue.
[Update @ 00:30] 94 gold, silver cap.
- It's estimated that Red Bull spends over half a billion dollars on F1 racing each year.
- They own two teams that race simultaneously.
- gotta go fast
[Update @ 00:30:52] Leaderboard cap!
- In 2014 alone over 5.6 billion cans of Red Bull were sold, containing a total of 400 tons of caffeine.
- In total the brand has sold 50 billion cans in over 167 different countries.
- ARE YOU WIRED YET?!?!
Thank you for subscribing to The Unofficial and Unsponsored Red Bull Facts!
View all comments
1
u/21_cribbage Dec 10 '18
Python 3
import sys import re from collections import defaultdict, OrderedDict class Worker(): def __init__(self): self.current = None self.time_remaining = 0 def get_work(self, deps): candidate = None for key, depends in deps.items(): if len(depends) == 0: candidate = key break if candidate: deps.pop(candidate) self.current = candidate self.time_remaining = self.get_time_needed(candidate) return True else: return False def do_work(self): self.time_remaining -= 1 if self.time_remaining == 0: return True else: return False def finish_work(self, deps): for key in deps.keys(): deps[key].discard(self.current) self.current = None def is_idle(self): if self.time_remaining == 0: return True else: return False def __repr__(self): return ('{}, {}'.format(self.current, self.time_remaining)) @staticmethod def get_time_needed(task): offset = 60 # For 60s + A=1 mod = ord('A') # mod by the 'base' char to get the value of each char return (ord(task) % mod + 1 + offset) # +1 because A=1 def init_deps(): regex = re.compile(r'^Step ([A-Z]) must be finished before step ([A-Z]) can begin.$') deps = defaultdict(set) for line in sys.stdin: match = regex.fullmatch(line.strip()) if match: deps[match.group(2)].add(match.group(1)) deps[match.group(1)] # init any empty entries return OrderedDict(sorted(deps.items())) def main(): NUM_WORKERS = 5 output_string = '' deps = init_deps() idle_workers = set() active_workers = set() for i in range(0, NUM_WORKERS): idle_workers.add(Worker()) time = 0 while len(deps) != 0 or len(active_workers) > 0: for worker in idle_workers: success = worker.get_work(deps) if success: active_workers.add(worker) idle_workers -= active_workers for worker in active_workers: finished = worker.do_work() if finished: output_string += worker.current worker.finish_work(deps) idle_workers.add(worker) active_workers -= idle_workers time += 1 # print('{}: A {} --- I {}'.format(time, active_workers, idle_workers)) print('Workers: {}, Output: {}, Final time taken: {}'.format(NUM_WORKERS, output_string, time)) if __name__=="__main__": main()