1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import { ExtractNumbers, LoadInput } from "../common.ts";
- const tests = [
- "Time: 7 15 30",
- "Distance: 9 40 200",
- ];
- const input = await LoadInput(6);
- const theRace = ParseRaces(input);
- FindWinningTimes(theRace);
- console.log("The amount of possible race wins is:", theRace.WinningTimesQuantity);
- function ParseRaces(inputLines: string[]): Race {
-
- const times = ExtractNumbers(inputLines.shift() || "");
-
- const distances = ExtractNumbers(inputLines.shift() || "");
-
- return {
- Duration: Number(times.join("")),
- RecordDistance: Number(distances.join("")),
- WinningTimesQuantity: -1,
- };
- }
- function FindWinningTimes(race: Race) {
-
- let [lowerBounds, upperBounds] = FindQuadraticRoots(-1, race.Duration, (race.RecordDistance * -1));
-
-
- lowerBounds = Math.ceil(lowerBounds + 0.01);
- upperBounds = Math.floor(upperBounds - 0.01);
- race.WinningTimesQuantity = upperBounds - lowerBounds + 1;
- }
- function FindQuadraticRoots(a: number, b: number, c: number): number[] {
- if (a == 0) { return []; }
-
- const discriminant = Math.sqrt(Math.pow(b, 2) - (4 * a * c));
-
- return [
- ((b * -1) + discriminant) / (2 * a),
- ((b * -1) - discriminant) / (2 * a),
- ];
- }
- type Race = {
-
- Duration: number,
-
- RecordDistance: number,
-
- WinningTimesQuantity: number
- };
|