Public
Edited
Dec 14
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
return input.split("\n").map((d) => {
const [x, y, vx, vy] = d.match(/-?\d+/g).map(Number);
return { x, y, vx, vy };
});
}
Insert cell
Insert cell
grid = ({ nRows: 103, nCols: 101 })
Insert cell
Insert cell
function move(robots) {
robots.forEach((r) => {
r.x = (grid.nCols + r.x + r.vx) % grid.nCols;
r.y = (grid.nRows + r.y + r.vy) % grid.nRows;
});
}
Insert cell
Insert cell
function score(robots) {
const [midRow, midCol] = [(grid.nRows - 1) / 2, (grid.nCols - 1) / 2];
let [tl, tr, bl, br] = [0, 0, 0, 0];
robots.forEach((r) => {
tl += r.x < midCol && r.y < midRow ? 1 : 0;
tr += r.x > midCol && r.y < midRow ? 1 : 0;
bl += r.x < midCol && r.y > midRow ? 1 : 0;
br += r.x > midCol && r.y > midRow ? 1 : 0;
});
return tl * tr * bl * br;
}
Insert cell
function part1(input) {
const robots = parse(puzzleInput);
d3.range(0, 100).forEach((_) => move(robots));
return score(robots);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cluster = ({
x: robotVariance.filter((d) => d.xVar < 500)[0].i,
y: robotVariance.filter((d) => d.yVar < 500)[0].i
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const cycle = grid.nRows * grid.nCols;
return (
(cycle + AOC.chineseRemainderTheorem([101, 103], [cluster.x, cluster.y])) %
cycle
);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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