From ef9e5ba9a8fd0d0098d84a3be12f51e71abef8ee Mon Sep 17 00:00:00 2001 From: Pascal Syma Date: Fri, 5 Dec 2025 08:46:03 +0100 Subject: [PATCH] Solve 2025-05 --- 2025/05/2025-05.run.xml | 7 +++++++ 2025/05/index.test.ts | 22 ++++++++++++++++++++++ 2025/05/index.ts | 36 ++++++++++++++++++++++++++++++++++++ 2025/05/run.ts | 12 ++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 2025/05/2025-05.run.xml create mode 100644 2025/05/index.test.ts create mode 100644 2025/05/index.ts create mode 100644 2025/05/run.ts diff --git a/2025/05/2025-05.run.xml b/2025/05/2025-05.run.xml new file mode 100644 index 0000000..c8b589b --- /dev/null +++ b/2025/05/2025-05.run.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/2025/05/index.test.ts b/2025/05/index.test.ts new file mode 100644 index 0000000..062edd6 --- /dev/null +++ b/2025/05/index.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from 'bun:test'; +import { solveFirst, solveSecond } from './index.ts'; + +describe('2025-05', () => { + const testInput = `3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32`; + it('first', () => { + expect(solveFirst(testInput)).toBe(3); + }); + it('second', () => { + expect(solveSecond(testInput)).toBe(14); + }); +}); diff --git a/2025/05/index.ts b/2025/05/index.ts new file mode 100644 index 0000000..c0d9b7d --- /dev/null +++ b/2025/05/index.ts @@ -0,0 +1,36 @@ +import type { Coordinate } from '../../utils/coordinates.ts'; + +function parseRanges(input: string) { + return Array.from( + input.matchAll(/(\d*)-(\d*)/gm), + (m) => [Number(m[1]), Number(m[2])] as Coordinate + ); +} + +export function solveFirst(input: string): number { + const [r, i] = input.split('\n\n'); + + const ranges = parseRanges(r); + + const ingredients = i.split('\n').map(Number); + + return ingredients.filter((i) => + ranges.some(([from, to]) => i >= from && i <= to) + ).length; +} + +export function solveSecond(input: string): number { + const ranges = parseRanges(input).toSorted((a, b) => a[0] - b[0]); + + let total = 0; + + ranges.reduce((lastEnd, curr) => { + if (curr[1] <= lastEnd) { + return lastEnd; + } + total += curr[1] - Math.max(lastEnd + 1, curr[0]) + 1; + return curr[1]; + }, 0); + + return total; +} diff --git a/2025/05/run.ts b/2025/05/run.ts new file mode 100644 index 0000000..88783cd --- /dev/null +++ b/2025/05/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);