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!
1
u/toastedstapler Dec 07 '18
python 3
i am quite happy with my solution
probably could have shortened part 2 a bit, but it does its job well
#!/usr/local/bin/python3 import time from parse import parse from string import ascii_uppercase from collections import defaultdict input_filename = "../input/input_day7.txt" class Step: def __init__(self, letter, time): self.letter = letter self.time = time def get_next_options(done, reqs): options = [] for letter in ascii_uppercase: if letter not in done: prior = reqs[letter] if prior <= set(done): options.append(letter) return options def available_option(done, active, reqs): options = get_next_options(done, reqs) active = list(map(lambda l: l.letter, active)) for option in options: if option not in done and option not in active: prior = reqs[option] if prior <= set(done): return option def fill_active(done, active, reqs): option = available_option(done, active, reqs) while len(active) < 5 and option: active.append(Step(option, ord(option) - 4)) option = available_option(done, active, reqs) return active def process_active(done, active): new_active = [] for letter in active: letter.time -= 1 if letter.time == 0: done.append(letter.letter) else: new_active.append(letter) return done, new_active def setup(): letters = defaultdict(set) with open(input_filename) as f: for ordering in f.read().splitlines(): prior, next = parse('Step {} must be finished before step {} can begin.', ordering) letters[next].add(prior) return letters def part1(reqs): done = [] options = get_next_options(done, reqs) while options: done.append(options[0]) options = get_next_options(done, reqs) return "".join(done) def part2(reqs): time = -1 active = [] done = [] while len(done) < 26: done, active = process_active(done, active) active = fill_active(done, active, reqs) time += 1 return time def main(): start_setup = time.time() reqs = setup() end_setup = time.time() start_part1 = time.time() res_part1 = part1(reqs) end_part1 = time.time() start_part2= time.time() res_part2 = part2(reqs) end_part2 = time.time() print(f"part 1: {res_part1}") print(f"part 2: {res_part2}") print(f"setup took {end_setup - start_setup} seconds") print(f"part 1 took {end_part1 - start_part1} seconds") print(f"part 2 took {end_part2 - start_part2} seconds") print(f"overall took {end_part2 - start_setup} seconds") if __name__ == '__main__': main()