Published
Edited
Dec 6, 2021
2 stars
Insert cell
Insert cell
Insert cell
grid = Object.entries(part2[0])
.map(([p, count]) => {
const [x, y] = p.split(",").map(Number);
return { x, y, count };
})
.sort((a, b) => a.count - b.count)
Insert cell
part2 = {
const grid = {};
for (const [p0, p1] of data)
for (const p of line(p0, p1))
grid[p] = (grid[p] || 0) + 1;
return [grid, Object.values(grid).filter((d) => d > 1).length]
}
Insert cell
part1 = {
const grid = {};
for (const [p0, p1] of data)
if (p0[0] === p1[0] || p0[1] === p1[1])
for (const p of line(p0, p1)) grid[p] = (grid[p] || 0) + 1;
return [grid, Object.values(grid).filter((d) => d > 1).length];
}
Insert cell
function* line([x0, y0], [x1, y1]) {
let [x, y] = [x0, y0];
while (x !== x1 || y !== y1) {
yield [x, y];
x += Math.sign(x1 - x);
y += Math.sign(y1 - y);
}
yield [x, y];
}
Insert cell
data = (which === "test" ? test : input)
.trim()
.split("\n")
.map((line) => line.split(" -> ").map((c) => c.split(",").map(Number)))
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