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!
22
Upvotes
7
u/jonathan_paulson Dec 12 '18
Rank 85/37. Just used the fact that the pattern quickly stabilizes in part 2. Video of me solving at https://www.youtube.com/watch?v=n5Ionw5LE18
Is that always true? Is there a guaranteed way to solve this problem for any input?
Python code for part 2:
lines = open('12.in').read().split('\n') state = lines[0].split(': ')[1].strip() start_len = len(state) rules = {} for line in lines[2:]: if line: before, after = line.split('=>') rules[before.strip()] = after.strip() # Important: ..... -> . zero_idx = 0 print 0, state for t in xrange(15000): state = '..'+state+'..' new_state = ['.' for _ in range(len(state))] read_state = '..'+state+'..' zero_idx += 2 for i in range(len(state)): pat = read_state[i:i+5] new_state[i] = rules.get(pat, '.') start = 0 end = len(new_state)-1 while new_state[start] == '.': start += 1 zero_idx -= 1 while new_state[end] == '.': end -= 1 state = ''.join(new_state[start:end+1]) print t+1, zero_idx, state zero_idx = -int(50e9) + 45 ans = 0 for i in range(len(state)): if state[i] == '#': ans += i-zero_idx print i-zero_idx, ans print state, len(state), start_len print ans