Public
Edited
Nov 11, 2022
1 star
Insert cell
Insert cell
function vanEck(history, spoken, n) {
const numInitialItems = history.reduce((x) => x + 1, 0);
for (let turn = numInitialItems; turn < n - 1; turn++) {
if (spoken in history) {
const last = history[spoken];
history[spoken] = turn;
spoken = turn - last;
} else {
history[spoken] = turn;
spoken = 0;
}
}
return spoken;
}
Insert cell
Insert cell
function initHistory(input) {
const initSequence = AOC.cloneArray(input);
const history = [];
const current = initSequence.pop();
initSequence.forEach((n, i) => (history[n] = i));
return [history, current];
}
Insert cell
function part1(input) {
return vanEck(...initHistory(input), 2020);
}
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
return vanEck(...initHistory(input), 30000000);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more