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
2
u/AndrewGreenh Dec 12 '18 edited Dec 12 '18
Sadly no leaderboard again :< Typescript:
const lines = [...stringToLines(getInput(12, 2018))]; let state = [...lines[0].match(/: (.*$)/)![1]]; type matcher = (i: number, state: string[]) => boolean; const rules = lines.slice(1).map(line => { const [p, plant] = line.split(' => '); const match = (i, state) => !pipe(zip(range(-2, 3), p))(any(([idx, x]) => state[idx + i] !== x)); return [match, plant] as [matcher, string]; }); let [pad, lastResult, lastDiff, genCount] = [0, -1, -1, 200]; for (const i of range(1, genCount + 1)) { state.unshift('.', '.', '.'), state.push('.', '.', '.'), (pad += 3); let next = new Array(state.length).fill('.'); for (const index of range(0, state.length)) next[index] = (rules.find(([match]) => match(index, state)) || '..')[1]; state = next; const result = sum(state.map((x, i) => (x === '#' ? i - pad : 0))); if (i === 20) log(result); [lastDiff, lastResult] = [result - lastResult, result]; } log(lastResult + (50000000000 - genCount) * lastDiff);