Public
Edited
Dec 6, 2023
1 fork
2 stars
Insert cell
Insert cell
input = `Time: 62 73 75 65
Distance: 644 1023 1240 1023`
Insert cell
races = [[62, 644], [73, 1023], [75, 1240], [65, 1023]]
Insert cell
answer1 = races.map(ways).reduce((a, b) => a * b, 1)
Insert cell
function ways([time, record]) {
const span = Math.sqrt((time / 2) ** 2 - record);
// d3.sum(d3.range(time + 1), (i) => Math.abs(i - time / 2) < span); // first approach, lame
return Math.ceil(time / 2 + span) - Math.floor(time / 2 - span) - 1; // better
}
Insert cell
ways([7, 9])
Insert cell
ways([15, 40])
Insert cell
ways([30, 200])
Insert cell
Insert cell
Insert cell
race2 = [
+races.map((d) => String(d[0])).join(""),
+races.map((d) => String(d[1])).join("")
]
Insert cell
ways(race2)
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