Public
Edited
Dec 2
Paused
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
return input.split("\n").map((x) => [Number(x)]);
}
Insert cell
Insert cell
function move(fromIndex, toIndex, xs) {
const remove = (i) => xs.splice(i, 1);
const insert = (x, i) => xs.splice(i, 0, x);
const x = xs[fromIndex % xs.length];
remove(fromIndex % xs.length);
insert(x, toIndex % xs.length);
}
Insert cell
Insert cell
function mix(xs, numMixes = 1) {
const mixed = xs.map(AOC.identity);
for (let i = 0; i < numMixes; i++) {
xs.forEach((x) => {
const xPos = mixed.indexOf(x);
move(xPos, xPos + x[0], mixed);
});
}
return mixed;
}
Insert cell
Insert cell
function coords(xs) {
const indexOfZero = xs.indexOf(0);
return (
xs[(indexOfZero + 1000) % xs.length] +
xs[(indexOfZero + 2000) % xs.length] +
xs[(indexOfZero + 3000) % xs.length]
);
}
Insert cell
function part1(input) {
return coords(mix(parse(input)).flat());
}
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
let xs = parse(input).map(([x]) => [x * 811589153]);
return coords(mix(xs, 10).flat());
}
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