Public
Edited
Dec 5, 2023
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
AOC.resultTable(meta)
Insert cell
run = (x) => parsed(data).maps.reduce((y, fn) => fn(y), x)
Insert cell
part1 = (s) => d3.min(parsed(s).seeds, run)
Insert cell
Insert cell
part2 = d3.min(d3.range(3_177_317_000, 3_177_317_000 + 1e3), run)
Insert cell
parsed = (s) => {
const [seeds, ...rest] = s;

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
parsed(data)
Insert cell
data = parse(which === "test1" ? test1 : input)
Insert cell
parse = (s) => s.trim().split("\n\n")
Insert cell
Insert cell
Insert cell
inputs = ({
test: test1,
real: input
})
Insert cell
meta = AOC.meta({
day: 5,
year: 23,
title: "If You Give A Seed A Fertilizer",
parse,
inputs,
parts: [part1, part2],
expected: { test: [35, 46], real: [424490994, 15290096] },
href: "https://observablehq.com/@a1ip/aoc-2023-day-5"
})
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