Skip to content

Instantly share code, notes, and snippets.

@trescube
Last active February 12, 2019 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trescube/ff709e34c75c53736e420f031baec647 to your computer and use it in GitHub Desktop.
Save trescube/ff709e34c75c53736e420f031baec647 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const numberOfGenerations = 20
const initialGenerationOfPots = '#..#.#..##......###...###'.split('')
const rules = [
'...##',
'..#..',
'.#...',
'.#.#.',
'.#.##',
'.##..',
'.####',
'#.#.#',
'#.###',
'##.#.',
'##.##',
'###..',
'###.#',
'####.'
]
const FULL = '#'
const EMPTY = '.'
function bitmap(acc, char, idx) {
return acc | ((char === FULL) ? 2**idx : 0)
}
const bitmappedRules = rules.map(rule => rule.split('').reverse().reduce(bitmap, 0))
const finalGenerationOfPots = Array(numberOfGenerations).fill(0).reduce((currentGenerationOfPots, _, idx) => {
console.log(`generation ${idx}: ${currentGenerationOfPots.join('')}`)
const bitmappedPots = []
currentGenerationOfPots.concat(Array.from('....')).reduce((currentSlice, pot) => {
currentSlice.push(pot)
bitmappedPots.push(currentSlice.slice(0, 5).reverse().reduce(bitmap, 0))
return currentSlice.slice(1, 5)
}, Array.from('....'))
return bitmappedPots.map(bitmappedPot => bitmappedRules.includes(bitmappedPot) ? FULL : EMPTY)
}, initialGenerationOfPots)
const count = finalGenerationOfPots.reduce((count, currentPot, idx) => {
return (currentPot === FULL) ? count + idx - numberOfGenerations*2 : count
}, 0)
console.log(`count: ${count}`)
{
"name": "aoc-day-12",
"version": "0.0.0",
"bin": "./aoc-day-12.js"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment