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

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))?.apply(x) ?? 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
plot([3_751_501_000, 1e3])
Insert cell
part2 = d3.min(d3.range(3751501_000, 3751501_000+1e3), run)
Insert cell
plot([1_206_608_000, 1e3])
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, Math.ceil(n / 1e4)), {
x: (d) => d,
y: run,
}),
Plot.tip(
d3.range(s, s + n, Math.ceil(n / 1e4)),
Plot.selectMinY({
x: (d) => d,
y: run,
format: { x: format, y: format },
title: { x: true, y: true }
})
)
],
grid: true,
x: { tickFormat: "s", label: "Seed" },
y: { tickFormat: "s", type: "log", label: "Location" },
color: { type: "categorical" },
width
})
Insert cell
format = d3.formatLocale({ thousands: "_", grouping: [3] }).format(",d")
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