From 88d6e8b852bb12c5f0894c13b21eff73497253f2 Mon Sep 17 00:00:00 2001 From: wieerwill Date: Sat, 2 Dec 2023 11:29:49 +0100 Subject: [PATCH] solve Day01 in ts --- Day01/ts/solution.ts | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Day01/ts/solution.ts diff --git a/Day01/ts/solution.ts b/Day01/ts/solution.ts new file mode 100644 index 0000000..c2b6929 --- /dev/null +++ b/Day01/ts/solution.ts @@ -0,0 +1,77 @@ +import * as fs from 'fs'; +import * as path from 'path'; + +function extractDigits(line: string): number[] { + const digitMap: { [key: string]: number } = { + zero: 0, one: 1, two: 2, three: 3, four: 4, + five: 5, six: 6, seven: 7, eight: 8, nine: 9 + }; + + for (let i = 0; i <= 9; i++) { + digitMap[i.toString()] = i; + } + + const digitsFound: number[] = []; + let i = 0; + while (i < line.length) { + let matched = false; + for (const [word, digit] of Object.entries(digitMap)) { + if (line.startsWith(word, i)) { + digitsFound.push(digit); + i += word.length - 1; + matched = true; + break; + } + } + i++; + } + + return digitsFound; +} + +function extractCalibrationValue(line: string): number { + const digits = extractDigits(line); + return digits.length > 0 ? parseInt(`${digits[0]}${digits[digits.length - 1]}`, 10) : 0; +} + +function calculateTotalCalibrationValue(filePath: string): number { + try { + const lines = fs.readFileSync(filePath, 'utf-8').split('\n').filter(line => line); + return lines.reduce((sum, line) => sum + extractCalibrationValue(line), 0); + } catch (error) { + throw new Error(`Failed to process file: ${filePath}. Error: ${error}`); + } +} + +function test(): void { + console.warn("Starting Tests"); + const testLines = [ + "two1nine", + "eightwothree", + "abcone2threexyz", + "xtwone3four", + "4nineeightseven2", + "zoneight234", + "7pqrstsixteen" + ]; + const expectedResults = [29, 83, 13, 24, 42, 14, 76]; + + testLines.forEach((line, index) => { + const result = extractCalibrationValue(line); + console.assert(result === expectedResults[index], `Error in line '${line}': Expected ${expectedResults[index]}, got ${result}`); + console.log(`Line: '${line}', Calibration Value: ${result}`); + }); + console.log("Finished Tests\n"); +} + +function main(): void { + try { + test(); + const totalValue = calculateTotalCalibrationValue(path.join(__dirname, '../input.txt')); + console.log(`Total Calibration Value from input.txt: ${totalValue}`); + } catch (error) { + console.error(error); + } +} + +main();