You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.2 KiB
TypeScript

type Race = {
time: number;
distance: number;
};
const races: Race[] = [];
const timeFrom = new Date().getTime();
// const input = `Time: 47 70 75 66
// Distance: 282 1079 1147 1062`.split("\n");
const input = `Time: 47707566
Distance: 282107911471062`.split("\n");
// parse input
const [t, ...rtimes] = input[0].split(" ");
const times = rtimes.filter((o) => o !== "").map((o) => Number(o));
const [d, ...rdist] = input[1].split(" ");
const dist = rdist.filter((o) => o !== "").map((o) => Number(o));
let mul = 1;
for (let i = 0; i < times.length; i++) {
races.push({ time: times[i], distance: dist[i] });
}
console.log(races);
function getDistance(race: Race, holdTime: number): number {
return (race.time - holdTime) * holdTime;
}
for (const race of races) {
let from = 1;
let to = 1;
for (let i = 0; i < race.time; i++) {
if (getDistance(race, i) > race.distance) {
from = i;
break;
}
}
for (let i = race.time; i >= 0; i--) {
if (getDistance(race, i) > race.distance) {
to = i;
break;
}
}
mul *= to - from + 1;
}
console.log(mul);
const timeTotal = new Date().getTime() - timeFrom;
console.log(`Time took: ${timeTotal}ms`);