Public
Edited
Dec 5, 2023
Insert cell
Insert cell
parsed = {
const [seeds, ...rest] = input.trim().split("\n\n");

const identity = { apply: (x) => x };

const maps = rest.map((lines) => {
const [name, ...ranges] = lines.split("\n");
const fns = ranges.map(r => {
const [d, s, n] = r.split(" ").map(Number);
return { applies: (x) => s <= x && x < s + n, apply: (x) => d + x - s }
});
return (x) => (fns.find(fn => fn.applies(x)) ?? identity).apply(x);
})
return { seeds: seeds.split(" ").slice(1).map(Number), maps };
}
Insert cell
run = (x) => parsed.maps.reduce((y, fn) => fn(y), x)
Insert cell
part1 = d3.min(parsed.seeds, run)
Insert cell
seeds = [...d3.group(parsed.seeds, (d, i) => Math.floor(i / 2)).values()]
Insert cell
Insert cell
Insert cell
Insert cell
seeds.map(([s, n], i) =>
d3.min(d3.range(s, s + n, n / 1e4), (d) => run(Math.floor(d)))
)
Insert cell
plot([3_394_006_000, 1e3])
Insert cell
d3.min(d3.range(3_394_006_000, 3_394_006_000 + 1e3), run) // close, but not as small as the other!
Insert cell
plot([2_410_596_000, 1e3])
Insert cell
d3.min(d3.range(2_410_596_000, 2_410_596_000 + 1e3), run) // close, but not as small as the other!
Insert cell
plot([538_564_000, 1e3])
Insert cell
d3.min(d3.range(538_564_000, 538_564_000 + 1e3), run) // close, but not as small as the other!
Insert cell
plot([3_177_317_000, 1e3])
Insert cell
part2 = d3.min(d3.range(3_177_317_000, 3_177_317_000 + 1e3), run)
Insert cell
d3.min(d3.range(1206_608_000, 1206_608_000+1e3), run) // close, but not as small as the other!
Insert cell
plot = ([s, n]) => Plot.plot({
marks: [
Plot.line(d3.range(s, s + n, n / 1e4), {
x: (d) => Math.floor(d),
y: (d) => run(Math.floor(d)),
tip: "x"
})
],
x: { tickFormat: "s" },
y: { tickFormat: "s", type: "log" },
color: { type: "categorical" },
width
})
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