Created
December 18, 2018 09:16
-
-
Save jemmyw/293f40bcb38ff867740b4877a05a85f0 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
let recipeScores: number[], i1: number, i2: number, serialized: string; | |
function reset() { | |
recipeScores = [3, 7]; | |
i1 = 0; | |
i2 = 1; | |
serialized = '37'; | |
} | |
function updateScores() { | |
const sum = recipeScores[i1] + recipeScores[i2]; | |
const sumStr = String(sum); | |
sumStr | |
.split('') | |
.map(Number) | |
.forEach(newRecipeScore => { | |
recipeScores.push(newRecipeScore); | |
serialized += String(newRecipeScore); | |
}); | |
i1 += 1 + recipeScores[i1]; | |
i1 = i1 % recipeScores.length; | |
i2 += 1 + recipeScores[i2]; | |
i2 = i2 % recipeScores.length; | |
} | |
reset(); | |
const after = 890691; | |
while (recipeScores.length < after + 10 + 1) { | |
updateScores(); | |
} | |
console.log(recipeScores.slice(after, after + 10).join('')); | |
// Part 2 | |
const search = '890691'; | |
console.log(serialized.indexOf(search)); | |
reset(); | |
while (!serialized.includes(search)) { | |
// Above check is expensive, do 1,000,000 updates before checking each time | |
for(let i = 0; i < 1000000; ++i) { | |
updateScores(); | |
} | |
console.log(recipeScores.length, serialized.indexOf(search)); | |
} | |
console.log(serialized.indexOf(search)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment