Public
Edited
Dec 26, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
blizzards = {
const { lines, width, height } = parsed;
const directions = { ">": [1, 0], "<": [-1, 0], "^": [0, -1], v: [0, 1] };
const blizzards = [];
lines.forEach((l, y) => {
[...l].forEach((c, x) => {
if (c !== "#" && c !== ".") {
const [dx, dy] = directions[c];
blizzards.push((t) => [
(((x - 1 + dx * t) % width) + width) % width,
(((y - 1 + dy * t) % height) + height) % height
]);
}
});
});
return (t) =>
new d3.InternSet(
blizzards.map((f) => f(t)),
String
);
}
Insert cell
within = ([x, y]) => 0 <= x && x < parsed.width && 0 <= y && y < parsed.height
Insert cell
function* neighbors([x, y]) {
for (let p of [
[x - 1, y],
[x + 1, y],
[x, y - 1],
[x, y + 1],
[x, y]
])
if ((p[0] === x && p[1] === y) || within(p)) yield p;
}
Insert cell
function* step(path) {
if (path.length > 18) {
yield path;
} else {
const next = blizzards(path.length + 1);
for (const n of neighbors(path[path.length - 1]))
if (!next.has(n)) yield* step(path.concat([n]));
}
}
Insert cell
// paths = [...step([[0, -1]])]
Insert cell
furthest = d3
.map(step([[0, -1]]), (p) => [
p,
d3.max(p, (d) => d[0]) + d3.max(p, (d) => d[1])
])
.sort((a, b) => b[1] - a[1])[0][0]
Insert cell
successful = paths.find(p => new d3.InternSet(p, String).has(target))
Insert cell
target = [parsed.width - 1, parsed.height - 1]
Insert cell
Insert cell
Insert cell
parsed = {
const lines = (which === "sample" ? sample : input).trim().split("\n");
return { lines, height: lines.length - 2, width: lines[0].length - 2 };
}
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