Public
Edited
Dec 8, 2023
Insert cell
Insert cell
parsed = {
const [instructions, network] = input.trim().split("\n\n");
const nodes = new Map(
network.split("\n").map((l) => {
const [, name, L, R] = l.match(/(\w+) = \((\w+), (\w+)\)/);
return [name, { L, R }];
})
);
return { instructions, nodes };
}
Insert cell
Insert cell
sample2 = `
LLR

AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)
`
Insert cell
function* steps(n = "AAA", end = (d) => d === "ZZZ") {
while (true) {
for (const i of parsed.instructions) {
yield n;
if (end(n)) return;
n = parsed.nodes.get(n)[i];
}
}
}
Insert cell
count = (iter) => d3.reduce(iter, (count) => count + 1, 0)
Insert cell
part1 = count(steps("AAA")) - 1
Insert cell
Insert cell
starts = d3.filter(parsed.nodes.keys(), d => d.endsWith("A"))
Insert cell
ends = starts.map((s) => count(steps(s, (d) => d.endsWith("Z"))) - 1)
Insert cell
lcm = (await import("https://esm.run/lcm")).default
Insert cell
part2 = ends.reduce(lcm)
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