Published
Edited
Dec 19, 2018
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
part1 = {
let numRecipes = Number(INPUT);
let scores = [3, 7];
let elf1 = 0;
let elf2 = 1;

while (scores.length < numRecipes + 10) {
elf1 = (elf1 + 1 + scores[elf1]) % scores.length;
elf2 = (elf2 + 1 + scores[elf2]) % scores.length;

// each recipe score is 0-9, so their sum is 0-18
// if the sum is > 9, push a 1 and then the unit digit
let newScores = scores[elf1] + scores[elf2];
if (newScores > 9) {
scores.push(1, newScores % 10);
} else {
scores.push(newScores);
}
}

return Number(scores.slice(numRecipes, numRecipes + 10).join(''));
}
Insert cell
Insert cell
function arrayEndsWith (array, subarray) {
for (let i = 1; i <= subarray.length; i++) {
if (array[array.length - i] !== subarray[subarray.length - i]) {
return false;
}
}
return true;
}
Insert cell
part2 = {
let target = INPUT.split('').map(Number);
let scores = [3, 7];
let elf1 = 0;
let elf2 = 1;
while (true) {
elf1 = (elf1 + 1 + scores[elf1]) % scores.length;
elf2 = (elf2 + 1 + scores[elf2]) % scores.length;

let newScores = scores[elf1] + scores[elf2];
if (newScores > 9) {
newScores = [1, newScores % 10];
} else {
newScores = [newScores];
}

for (let digit of newScores) {
scores.push(digit);
if (arrayEndsWith(scores, target)) {
return scores.length - target.length;
}
}
}
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more