diff --git a/2025/03/2025-03.run.xml b/2025/03/2025-03.run.xml new file mode 100644 index 0000000..38fdfa6 --- /dev/null +++ b/2025/03/2025-03.run.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/2025/03/index.test.ts b/2025/03/index.test.ts new file mode 100644 index 0000000..6d2e2b3 --- /dev/null +++ b/2025/03/index.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from 'bun:test'; +import { solveFirst, solveSecond } from './index.ts'; + +describe('2025-03', () => { + const testInput = `987654321111111 +811111111111119 +234234234234278 +818181911112111`; + it('first', () => { + expect(solveFirst(testInput)).toBe(357); + }); + it('second', () => { + expect(solveSecond(testInput)).toBe(3121910778619); + }); +}); diff --git a/2025/03/index.ts b/2025/03/index.ts new file mode 100644 index 0000000..1faba1c --- /dev/null +++ b/2025/03/index.ts @@ -0,0 +1,45 @@ +import { sumArray } from '../../utils/array.ts'; + +function findHighestDigit(digits: number[], start: number, end: number) { + let highest = 0; + let highestI = start; + + for (let i = start; i < digits.length - end; i++) { + if (digits[i] > highest) { + highest = digits[i]; + highestI = i; + } + } + + return highestI; +} + +function findHighestNumber(row: string, size: number) { + const rowSplit = row.split(''); + const digits = rowSplit.map(Number); + + const highest: number[] = []; + for (let i = 0; i < size; i++) { + const highestDigit = findHighestDigit( + digits, + (i > 0 ? highest[i - 1] : -1) + 1, + size - i - 1 + ); + + highest.push(highestDigit); + } + + return Number(highest.map((key) => rowSplit[key]).join('')); +} + +export function solveFirst(input: string): number { + const rows = input.split('\n'); + + return sumArray(rows.map((row) => findHighestNumber(row, 2))); +} + +export function solveSecond(input: string): number { + const rows = input.split('\n'); + + return sumArray(rows.map((row) => findHighestNumber(row, 12))); +} diff --git a/2025/03/run.ts b/2025/03/run.ts new file mode 100644 index 0000000..88783cd --- /dev/null +++ b/2025/03/run.ts @@ -0,0 +1,12 @@ +import { join } from 'path'; +import { solveFirst, solveSecond } from './index.ts'; + +const input = await Bun.file(join(__dirname, 'input.txt')).text(); + +const firstAnswer = solveFirst(input); + +console.log(firstAnswer); + +const secondAnswer = solveSecond(input); + +console.log(secondAnswer);