Public
Edited
Dec 1, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function toVect(instr) {
switch (instr) {
case "n":
return [0, 1, 1];
case "ne":
return [1, 1, 0];
case "se":
return [1, 0, -1];
case "s":
return [0, -1, -1];
case "sw":
return [-1, -1, 0];
case "nw":
return [-1, 0, 1];
}
}
Insert cell
function move([p0, q0, r0], [p1, q1, r1]) {
return [p0 + p1, q0 + q1, r0 + r1];
}
Insert cell
function distFromOrigin([p, q, r]) {
return (Math.abs(p) + Math.abs(q) + Math.abs(r)) / 2;
}
Insert cell
Insert cell
function part1(input) {
const path = input.split(",").map(toVect);
return distFromOrigin(path.reduce((pos, mvt) => move(pos, mvt), [0, 0, 0]));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const path = input.split(",").map(toVect);
return Math.max(
...AOC.scanl(path, (pos, mvt) => move(pos, mvt), [0, 0, 0]).map(
distFromOrigin
)
);
}
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