From 686c925ec8d1d97f4316406788d80b2dcf52a07f Mon Sep 17 00:00:00 2001 From: wieerwill Date: Wed, 6 Dec 2023 20:09:16 +0100 Subject: [PATCH] solve Day06 in TS --- Day06/ts/solution.ts | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Day06/ts/solution.ts diff --git a/Day06/ts/solution.ts b/Day06/ts/solution.ts new file mode 100644 index 0000000..67f177e --- /dev/null +++ b/Day06/ts/solution.ts @@ -0,0 +1,77 @@ +import fs from 'fs'; +import assert from 'assert'; + +interface RaceData { + time: number; + distance: number; +} + +/** + * Parses the input file to extract the race time and record distance. + * @param {string} filePath - Path to the input file. + * @returns {RaceData} - An object containing time and record distance. + */ +function parseInput(filePath: string): RaceData { + try { + const lines = fs.readFileSync(filePath, 'utf-8').trim().split('\n'); + const time = parseInt(lines[0].replace(/\D/g, ''), 10); + const distance = parseInt(lines[1].replace(/\D/g, ''), 10); + console.log(`Parsed input from ${filePath} - Time: ${time}, Distance: ${distance}`); + return { time, distance }; + } catch (error) { + throw new Error(`Error parsing input file: ${(error as Error).message}`); + } +} + +/** + * Calculates the number of ways to beat the record for the race. + * @param {number} time - Total time for the race. + * @param {number} record - Record distance to beat. + * @returns {number} - Number of ways to win the race. + */ +function calculateWinningWays(time: number, record: number): number { + console.log(`Calculating winning ways for Time: ${time}, Record: ${record}`); + let minHoldTime = 0; + while (minHoldTime * (time - minHoldTime) <= record && minHoldTime < time) { + minHoldTime++; + } + let maxHoldTime = time - 1; + while (maxHoldTime * (time - maxHoldTime) <= record && maxHoldTime >= 0) { + maxHoldTime--; + } + const winningWays = Math.max(0, maxHoldTime - minHoldTime + 1); + console.log(`Winning ways calculated: ${winningWays} (Min: ${minHoldTime}, Max: ${maxHoldTime})`); + return winningWays; +} + +/** + * Runs a test with the given file and compares the result to the expected result. + * @param {string} filePath - Path to the test file. + * @param {number} expectedResult - Expected result for the test. + */ +function runTest(filePath: string, expectedResult: number): void { + console.log(`Running test with file: ${filePath}`); + const { time, distance } = parseInput(filePath); + const result = calculateWinningWays(time, distance); + assert.strictEqual(result, expectedResult, `Test failed! Expected ${expectedResult}, got ${result}`); + console.log("Test passed successfully."); +} + +function main(): void { + try { + const testFilePath = "../test.txt"; + const expectedTestResult = 71503; // Expected result from the test file + runTest(testFilePath, expectedTestResult); + + const inputFilePath = "../input.txt"; + console.log(`\nProcessing input file: ${inputFilePath}`); + const { time, distance } = parseInput(inputFilePath); + const result = calculateWinningWays(time, distance); + console.log(`Final result from input file: ${result}`); + + } catch (error) { + console.error(`An error occurred: ${(error as Error).message}`); + } +} + +main();