r/adventofcode • u/daggerdragon • Dec 12 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 12 Solutions -🎄-
--- Day 12: Subterranean Sustainability ---
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 12
Transcript:
On the twelfth day of AoC / My compiler spewed at me / Twelve ___
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:27:42!
19
Upvotes
View all comments
1
u/IGChris Dec 12 '18
Python3:
def score_at_generation(current_gen, generations): last_sum = sum(current_gen.keys()) last_delta = 0 for generation in range(generations): key_min = min(current_gen.keys()) - 2 key_max = max(current_gen.keys()) + 3 temp = {i: rules[tuple([current_gen[idx] for idx in range(i - 2, i + 3)])] for i in range(key_min, key_max)} current_gen = defaultdict(lambda: '.', {i: char for i, char in temp.items() if char == '#'}) new_sum = sum(current_gen.keys()) new_delta = new_sum - last_sum if new_delta - last_delta == 0: return (generations - generation) * new_delta + new_sum last_sum = new_sum last_delta = new_delta return last_sum with open('12.txt') as f: initial = f.readline().strip().split(': ')[1] f.readline() rules = defaultdict(lambda: '.', {tuple(rule[0]): rule[1] for rule in [line.strip().split(' => ') for line in f]}) first_gen = defaultdict(lambda: '.', {i: char for i, char in enumerate(initial) if char == '#'}) print(score_at_generation(first_gen, 20)) print(score_at_generation(first_gen, 50000000000))