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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more