Created
December 11, 2020 06:25
-
-
Save Friss/40a0a4af839e2c7fc64e6ec43eae490d 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
const fs = require('fs').promises; | |
(async () => { | |
console.log(`Reading input from ${__dirname}/${process.argv[2]}.txt`); | |
const inputData = await fs.readFile(`${__dirname}/${process.argv[2]}.txt`); | |
const inputs = inputData | |
.toString() | |
.split('\n') | |
.filter((n) => n) | |
.map((n) => parseInt(n, 10)) | |
.sort((a, b) => a - b); | |
const max = inputs[inputs.length - 1] + 3; | |
inputs.unshift(0); | |
inputs.push(max); | |
const inputSet = new Set(inputs); | |
let jolts = 0; | |
let oneUps = 0; | |
let twoUps = 0; | |
let threeUps = 0; | |
for (let index = 0; index < inputs.length + 1; index++) { | |
for (let j = 1; j < 4; j++) { | |
if (inputSet.has(jolts + j)) { | |
jolts += j; | |
if (j === 1) oneUps++; | |
if (j === 2) twoUps++; | |
if (j === 3) threeUps++; | |
break; | |
} | |
} | |
} | |
console.log('partOne', oneUps * threeUps); | |
console.log('----'); | |
const cache = new Map(); | |
const permutations = (val) => { | |
if (cache.has(val)) { | |
return cache.get(val); | |
} | |
const callBack = (lastValue) => { | |
if (lastValue === max) { | |
return 1; | |
} | |
let count = 0; | |
for (let index = 1; index < 4; index++) { | |
if (inputSet.has(lastValue + index)) { | |
count += permutations(lastValue + index); | |
} | |
} | |
return count; | |
}; | |
const output = callBack(val); | |
cache.set(val, output); | |
return output; | |
}; | |
console.log('part2', permutations(0)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment