import fs from "fs"; const input = fs.readFileSync("./input.txt").toString(); const [times, distances] = input .split("\n") .slice(0, 2) .map((s) => s.match(/\d+/g)?.map(Number) || []); function numberOfWaysToWin(time: number, record: number) { // Solution to quadratic: // x(time-x) > record // on the domain [0, time] const sqrt = Math.sqrt(time * time - 4 * record); const [min, max] = [-1, +1].map((sign) => 0.5 * (time + sqrt * sign)); const wholeMin = Math.floor(min) + 1; const wholeMax = Math.ceil(max); return wholeMax - wholeMin; } const result = times .map((time, index) => { const distance = distances[index]; return numberOfWaysToWin(time, distance); }) .reduce((acc, v) => acc * v, 1); console.log(result);