diff --git a/src/day1/typescript/main.test.ts b/src/day1/typescript/main.test.ts index f3bc484..73fb799 100644 --- a/src/day1/typescript/main.test.ts +++ b/src/day1/typescript/main.test.ts @@ -6,9 +6,11 @@ import {partA, partB} from './main.ts'; // eslint-disable-line n/file-extension- const day = 1; const answerExample = 142; const answerA = 55_029; +const answerExampleB = 281; const answerB = 55_686; const input: string[] = readFileSync(`src/day${day}/input.txt`, 'utf8').split('\n'); const inputExample: string[] = readFileSync(`src/day${day}/input_example.txt`, 'utf8').split('\n'); +const inputExampleB: string[] = readFileSync(`src/day${day}/input_example_b.txt`, 'utf8').split('\n'); Deno.test('Example', () => { assertEquals(partA(inputExample), answerExample); @@ -18,6 +20,10 @@ Deno.test('Part A', () => { assertEquals(partA(input), answerA); }); +Deno.test('Example B', () => { + assertEquals(partB(inputExampleB), answerExampleB); +}); + Deno.test('Part B', () => { assertEquals(partB(input), answerB); }); diff --git a/src/day1/typescript/main.ts b/src/day1/typescript/main.ts index 7d72885..0600100 100644 --- a/src/day1/typescript/main.ts +++ b/src/day1/typescript/main.ts @@ -1,106 +1,46 @@ export const partA = (input: string[]) => { // Part A - const calibrationValue = (line: string) => { - // Slice the first and last digits from the string and combine them into a number - // The first digit might be anywhere in the string, so we need to find it - const firstDigit = (/\d/.exec(line))?.[0]; - // Reverse the string and find the first digit again - const lastDigit = (/\d/.exec([...line].reverse().join('')))?.[0]; - // Combine the two digits into a number - return Number((Number(firstDigit) * 10) + Number(lastDigit)); - }; - let sum = 0; for (const line of input) { - sum += calibrationValue(line); + const digits = line.match(/\d/g); + const firstDigit = digits?.[0]; + const lastDigit = digits?.[digits.length - 1]; + sum += Number((Number(firstDigit) * 10) + Number(lastDigit)); } - return sum; // Answer: 55029 + return sum; }; export const partB = (input: string[]) => { // Part B - const calibrationValue = (line: string): number => { - // Let's find all possible numbers in the string. - // For each character, if it's a digit, we'll add it to our ordered list of digits. - // If it's a letter, we'll check if the next few characters spell out a number. - // If they do, we'll add that number to our list of digits. - const digits: number[] = []; - for (let i = 0; i < line.length; i++) { - const char = line[i]; - if (/\d/.test(char)) { - digits.push(Number(char)); - } else { - switch (char) { - case 'o': { - if (line.slice(i, i + 3) === 'one') { - digits.push(1); - } - - break; - } - - case 't': { - if (line.slice(i, i + 3) === 'two') { - digits.push(2); - } else if (line.slice(i, i + 5) === 'three') { - digits.push(3); - } - - break; - } - - case 'f': { - if (line.slice(i, i + 4) === 'four') { - digits.push(4); - } else if (line.slice(i, i + 4) === 'five') { - digits.push(5); - } - - break; - } - - case 's': { - if (line.slice(i, i + 3) === 'six') { - digits.push(6); - } else if (line.slice(i, i + 5) === 'seven') { - digits.push(7); - } - - break; - } - - case 'e': { - if (line.slice(i, i + 5) === 'eight') { - digits.push(8); - } - - break; - } - - case 'n': { - if (line.slice(i, i + 4) === 'nine') { - digits.push(9); - } - - break; - } - - default: { + let sum = 0; + const getDigitsFrom = (line: string) => { + const digitStrings = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; + const digitArray: number[] = []; + let currentIndexOfCharacter = 0; + for (const character of line) { + if (/\d/.test(character)) { + digitArray.push(Number(character)); + } else if (['o', 't', 'f', 's', 'e', 'n'].includes(character)) { + const foresight = line.slice(currentIndexOfCharacter, currentIndexOfCharacter + 5); + for (const digitString of digitStrings) { + if (foresight.startsWith(digitString)) { + digitArray.push(digitStrings.indexOf(digitString) + 1); break; } } } + + currentIndexOfCharacter++; } - const firstDigit = digits[0]; - const lastDigit = digits.at(-1) ?? firstDigit; - return (firstDigit * 10) + lastDigit; + return digitArray; }; - let sum = 0; for (const line of input) { - const getValue = calibrationValue(line); - sum += getValue; + const digitArray = getDigitsFrom(line); + const firstDigit = digitArray[0]; + const lastDigit = digitArray.at(-1) ?? firstDigit; + sum += (firstDigit * 10) + lastDigit; } - return sum; // Answer: 55686 + return sum; };