import { LoadInput } from "../common.ts"; /** * Calculate the sum of all calibration values in a document * * @param {string[]} document All lines of a document containing calibration values * @returns {number} The sum of all calibration values found in a document */ function CalculateCalibrationSum(document: string[]): number { return document.reduce((register, currVal) => register + extractCalibrationValue(currVal), 0); } /** * Extract a calibration value from a line of text * * Extracts the first and last digit from a string of text, * concatenates the two, and returns this as the calibration value. * * If only one digit is present, the one digit is concatenated to * itself. * * @param {string} line The line of text to extract the calibration value out of * @returns {number} The two-digit calibration value hidden in that string */ function extractCalibrationValue(line: string): number { const digitsInString = line.replace(/\D/g, ""); return Number(digitsInString[0] + digitsInString[digitsInString.length - 1]); } const tests: string[] = [ "1abc2", "pqr3stu8vwx", "a1b2c3d4e5f", "treb7uchet" ]; const input = await LoadInput(1); console.log("Test Cases"); console.log("Expected: 142", `Actual: ${CalculateCalibrationSum(tests)}`); console.log("Input"); console.log(`Your calibration sum ${CalculateCalibrationSum(input)}`);