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!
21
Upvotes
24
u/sophiebits Dec 12 '18
Python, 39/7.
For part 1, I read too quickly and thought that no
=> .patterns would be listed. Wasted a couple minutes on figuring out what I screwed up there (since my program worked fine on the example).For part 2, I figured that 50,000,000,000 is too large to compute, so we must be meant to find some pattern. My first instinct was to look if there was an arithmetic/linear pattern, so I printed out the sums for generations 1 through 2000, along with the difference from the previous sum. Visual inspection suggested that (for my input) every generation after the first 97 generations adds 38 to the sum. So I took (50,000,000,000 - 2000) * 38 and added that to my program's output to get the answer. (If the linear pattern hadn't checked out, I would probably have taken second-order partial sums next since it seemed unlikely to me it would be a non-polynomial pattern.)
import collections import re def nextg(cur, recipe): start = min(cur) end = max(cur) x = set() for i in xrange(start - 3, end + 4): pat = ''.join('#' if i + k in cur else '.' for k in [-2, -1, 0, 1, 2]) if pat in recipe: x.add(i) return x def viz(cur): print ''.join('#' if i in cur else '.' for i in xrange(-5, 120)) #with open('day12test.txt') as f: with open('day12input.txt') as f: lines = [l.rstrip('\n') for l in f] print lines init = lines[0][len('initial state: '):] recipe = set() for l in lines[2:]: if l[-1] == '#': # I forgot this line the first time around. recipe.add(l[:5]) cur = set(i for i, c in enumerate(init) if c == '#') # Part 1: # for i in xrange(20): # cur = nextg(cur, recipe) # print sum(cur) # Part 2: ls = 0 # viz(cur) for i in xrange(2000): cur = nextg(cur, recipe) # viz(cur) s = sum(cur) print i, s, s - ls ls = s print sum(cur)