This is my node.js solution to https://adventofcode.com/2018/day/12
Last active
February 12, 2019 15:43
-
-
Save trescube/ff709e34c75c53736e420f031baec647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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