Public
Edited
Mar 3, 2023
2 forks
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
droplets = new d3.InternSet(
data
.trim()
.split("\n")
.map((l) => l.split(",").map(Number)),
String
)
Insert cell
function* neighbors([x, y, z]) {
for (const [dx, dy, dz] of [
[0, 0, 1],
[0, 0, -1],
[0, 1, 0],
[0, -1, 0],
[1, 0, 0],
[-1, 0, 0]
])
yield [x + dx, y + dy, z + dz];
}
Insert cell
part1 = {
let sum = 0;
for (const d of droplets)
for (const n of neighbors(d))
if (!droplets.has(n)) sum++;
return sum;
}
Insert cell
extent = [0, 1, 2].map((d) => d3.extent(droplets, (p) => p[d]))
Insert cell
inExtent = ([x, y, z]) =>
extent[0][0]-1 <= x && x <= extent[0][1]+1 &&
extent[1][0]-1 <= y && y <= extent[1][1]+1 &&
extent[2][0]-1 <= z && z <= extent[2][1]+1
Insert cell
function* flood() {
const seen = new d3.InternSet([], String);
const q = [extent.map((d) => d[0] - 1)];
while (q.length) {
const p = q.shift();
if (seen.has(p)) continue;
seen.add(p);
for (const n of neighbors(p)) {
if (!inExtent(n)) continue;
if (droplets.has(n)) yield p;
else q.push(n);
}
}
}
Insert cell
flooded = new d3.InternSet(flood(), String)
Insert cell
part2 = d3.reduce(flood(), (s, d, i) => i) + 1
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