Public
Edited
Dec 11, 2023
1 fork
2 stars
Insert cell
Insert cell
space = input.split("\n").map((d) => [...d])
Insert cell
Plot.plot({
height: 600,
width: 600,
inset: 4,
marks: [
Plot.ruleY(expandY, { stroke: "orange" }),
Plot.ruleX(expandX, { stroke: "orange" }),
Plot.text(space.flat(), {
fontSize: 6,
x: (d, i) => i % space[0].length,
y: (d, i) => (i / space[0].length) | 0
})
]
})
Insert cell
expandX = d3
.range(space[0].length)
.filter((x) => space.every((line) => line[x] === "."))
Insert cell
expandY = d3
.range(space.length)
.filter((y) => space[y].every((d) => d === "."))
Insert cell
expandedManhattan1 = (a, b, expand, v) =>
Math.abs(b - a) + v * expand.filter((x) => (x - a) * (x - b) < 0).length
Insert cell
expandedManhattan = (v = 1) =>
([x1, y1], [x2, y2]) =>
expandedManhattan1(x1, x2, expandX, v) + expandedManhattan1(y1, y2, expandY, v)
Insert cell
galaxies = space
.flatMap((line, y) => line.map((d, x) => (d === "#" ? [[x, y]] : [])))
.flat()
Insert cell
{
const dist = expandedManhattan(1);
let total = 0;
for (let i = 0; i < galaxies.length; ++i) {
for (let j = 0; j < i; ++j) {
total += dist(galaxies[i], galaxies[j]);
}
}
return total;
}
Insert cell
Insert cell
{
const dist = expandedManhattan(1e6 - 1);
let total = 0;
for (let i = 0; i < galaxies.length; ++i) {
for (let j = 0; j < i; ++j) {
total += dist(galaxies[i], galaxies[j]);
}
}
return total;
}
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