Public
Edited
Dec 6, 2023
Insert cell
Insert cell
inputs0 = `Time: 7 15 30
Distance: 9 40 200`
Insert cell
inputs = `Time: 46 80 78 66
Distance: 214 1177 1402 1024`
Insert cell
Insert cell
races = {
let races = [];
inputs
.split("\n")[0]
.split(":")
.pop()
.replace(/[ ]+/g, " ")
.trim()
.split(" ")
.forEach((t, idx) => (races[idx] = { time: t }));
inputs
.split("\n")[1]
.split(":")
.pop()
.replace(/[ ]+/g, " ")
.trim()
.split(" ")
.forEach((d, idx) => (races[idx].dist = d));
return races;
}
Insert cell
wins = races.map((race) => {
let p = -race.time;
let q = race.dist;
let k1 = Math.floor(-p / 2 + Math.sqrt((p * p) / 4 - q) - 0.0000001);
let k2 = Math.ceil(-p / 2 - Math.sqrt((p * p) / 4 - q) + 0.0000001);
return { ...race, wins: k1 - k2 + 1 };
})
Insert cell
result1 = wins.reduce((prod, win) => prod * win.wins, 1)
Insert cell
Insert cell
races2 = {
let races = [];
inputs
.split("\n")[0]
.split(":")
.pop()
.replace(/[ ]+/g, "")
.trim()
.split(" ")
.forEach((t, idx) => (races[idx] = { time: t }));
inputs
.split("\n")[1]
.split(":")
.pop()
.replace(/[ ]+/g, "")
.trim()
.split(" ")
.forEach((d, idx) => (races[idx].dist = d));
return races;
}
Insert cell
wins2 = races2.map((race) => {
let p = -race.time;
let q = race.dist;
let k1 = Math.floor(-p / 2 + Math.sqrt((p * p) / 4 - q) - 0.0000001);
let k2 = Math.ceil(-p / 2 - Math.sqrt((p * p) / 4 - q) + 0.0000001);
return { ...race, wins: k1 - k2 + 1 };
})
Insert cell
result2 = wins2[0].wins
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