You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
635 B
JavaScript

const input = "9,3,1,0,8,4";
const start = input.split(",");
const memory = {};
let turn = 1;
let lastNum = null;
for (const num of start) {
if (lastNum !== null) {
memory[lastNum] = turn - 1;
}
lastNum = num;
console.log(`Turn ${turn}: ${num}`);
turn += 1;
}
let num;
while (turn <= 2020) {
if (typeof memory[lastNum] === "undefined") {
num = 0;
} else {
num = turn - 1 - memory[lastNum];
}
console.log(
`Turn ${turn}: ${num}${
num !== 0 ? ` (${turn - 1} - ${memory[lastNum]})` : ""
}`
);
memory[lastNum] = turn - 1;
lastNum = num;
turn += 1;
}
console.log("Solution:", num);