Public
Edited
Dec 6, 2023
Insert cell
Insert cell
races = d3.zip(
...input
.trim()
.split("\n")
.map((l) => l.split(/\s+/).slice(1).map(Number))
)
Insert cell
function* options([time, distance]) {
for (let hold = 0; hold <= time; hold++)
yield { hold, speed: hold, distance: hold * (time - hold) };
}
Insert cell
winning = (race) =>
d3.reduce(
options(race),
(count, d) => count + (d.distance > race[1] ? 1 : 0),
0
)
Insert cell
part1 = races.map(winning).reduce((p, d) => p * d)
Insert cell
race = input
.trim()
.split("\n")
.map((l) => l.split(/\s+/).slice(1).join(""))
.map(Number)
Insert cell
part2 = winning(race)
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