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.
42 lines
748 B
JavaScript
42 lines
748 B
JavaScript
// TODO: optimization needed
|
|
|
|
// const input = "0,3,6";
|
|
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;
|
|
let lastLog = new Date();
|
|
while (turn <= 30000000) {
|
|
if (typeof memory[lastNum] === "undefined") {
|
|
num = 0;
|
|
} else {
|
|
num = turn - 1 - memory[lastNum];
|
|
}
|
|
|
|
const now = new Date();
|
|
if (now - lastLog > 5000) {
|
|
console.log(`Turn ${turn.toLocaleString()}, num: ${num}`);
|
|
lastLog = now;
|
|
}
|
|
|
|
memory[lastNum] = turn - 1;
|
|
lastNum = num;
|
|
turn += 1;
|
|
}
|
|
|
|
console.log("Solution:", num);
|