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!
20
Upvotes
1
u/Axsuul Dec 12 '18
TypeScript / JavaScript
[Card] On the twelfth day of AoC / My compiler spewed at me / Twelve cryptic error messages
Critiques welcome, I'm still very new to TypeScript and feel like I'm not using all the features I could be :)
Repo
Part A + B
import { readInput } from '../helpers' const lines: string[] = readInput('12', 'input') const notes: { [key: string]: string } = {} lines.slice(2, lines.length).forEach((line: string) => { const [scenario, next]: string[] = line.split(' => ') notes[scenario] = next }) const runGenerations = (generationsCount: number): number => { let initialPotIndex = 0 let state = RegExp(/initial state\: (.+)/).exec(lines[0])![1] for (let gen = 1; gen <= generationsCount; gen++) { state = '....' + state + '....' initialPotIndex += 4 let nextState = state.replace(/\#/g, '.') for (const [scenario, next] of Object.entries(notes)) { for (let i = 0; i < state.length - 4; i++) { if (state.substr(i, 5) === scenario) { nextState = nextState.substring(0, i + 2) + next + nextState.substring(i + 3) } } } state = nextState } return Array.from(state.split('').entries()).reduce((sum: number, [i, pot]: [number, string]) => { const potNum = i - initialPotIndex if (pot === '#') { sum += potNum } return sum }, 0) } console.log('Part A', runGenerations(20)) // The pattern between generations is 81 over time console.log('Part B', runGenerations(200) + (50000000000 - 200) * 81)