Public
Edited
Dec 14, 2022
1 fork
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { digest } from "@visnup/emoji-hash"
Insert cell
part2 = {
const start = String([500, 0]);
const floor = d3
.range(width[0] - 1000, width[1] + 1000)
.map((x) => [x, height]);
const rest = new d3.InternSet([...rock, ...floor], String);
let i = 0, final;
do {
rest.add((final = pour([500, 0], rest)));
if (i++ % 1000 === 0) yield { final, rest, i };
} while (final && String(final) !== start);
yield { final, rest, i };
}
Insert cell
part1 = {
play;
const rest = new d3.InternSet(rock, String);
let i = 0, final;
do {
rest.add((final = pour([500, 0], rest)));
if (i++ % 10 === 0) yield { final, rest, i };
} while (final);
yield { final, rest, i: i - 1 };
}
Insert cell
pour = howPour === "randomly" ? pourRandom : pourLeft
Insert cell
function pourLeft(p, filled) {
while (p[1] < height) {
const [x, y] = p;
const n = [
[x, y + 1],
[x - 1, y + 1],
[x + 1, y + 1]
].find((n) => !filled.has(n));
if (n) p = n;
else return p;
}
}
Insert cell
function pourRandom(p, filled) {
while (p[1] < height) {
const [x, y] = p;
const n = [
[x, y + 1],
...d3.shuffle([
[x - 1, y + 1],
[x + 1, y + 1]
])
].find((n) => !filled.has(n));
if (n) p = n;
else return p;
}
}
Insert cell
rock = new d3.InternSet(
paths.flatMap((p) =>
d3.pairs(p).flatMap(([[x0, y0], [x1, y1]]) => {
const dx = Math.sign(x1 - x0), dy = Math.sign(y1 - y0);
return x0 !== x1
? d3.range(x0, x1 + dx, dx).map((x) => [x, y0])
: d3.range(y0, y1 + dy, dy).map((y) => [x0, y]);
})
),
String
)
Insert cell
height = d3.max(rock, d => d[1]) + 2
Insert cell
width = d3.extent(rock, d => d[0])
Insert cell
Plot.plot({
marks: paths.map(Plot.line),
y: { reverse: true }
})
Insert cell
paths = data
.trim()
.split("\n")
.map((l) => l.split(" -> ").map((p) => p.split(",").map(Number)))
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