Published
Edited
Dec 10, 2018
Insert cell
Insert cell
Insert cell
Insert cell
Deque.prototype.rotate = function rotate (n) {
if (n >= 0) {
for (let i = 0; i < n; i++) {
this.push(this.shift());
}
} else {
for (let i = 0; i > n; i--) {
this.unshift(this.pop());
}
}
}
Insert cell
function solve (marbles, players) {
let scores = {};
let circle = new Deque([0]);

for (let m = 1; m <= marbles; m++) {
if (m % 23) {
circle.rotate(1);
circle.push(m);
} else {
circle.rotate(-7);
scores[m % players] = (scores[m % players] || 0) + m + circle.pop();
circle.rotate(1);
}
}

return Object.entries(scores).reduce(
(max, next) => max > next[1] ? max : next[1], 0);
}
Insert cell
Insert cell
Insert cell
part1Score = solve(numMarbles, numPlayers)
Insert cell
Insert cell
part2Score = solve(numMarbles * 100, numPlayers)
Insert cell
Insert cell
Insert cell
Deque = require('denque@1.4.0/index.js').catch(() => window.Denque)
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