From 0ec2cf656d6f5bcb0e0776e0660e0e309202098e Mon Sep 17 00:00:00 2001 From: Pascal Syma Date: Tue, 2 Dec 2025 11:01:55 +0100 Subject: [PATCH] Solve 2025-02 --- 2025/02/2025-02.run.xml | 7 +++++++ 2025/02/index.test.ts | 34 ++++++++++++++++++++++++++++++++++ 2025/02/index.ts | 34 ++++++++++++++++++++++++++++++++++ 2025/02/run.ts | 12 ++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 2025/02/2025-02.run.xml create mode 100644 2025/02/index.test.ts create mode 100644 2025/02/index.ts create mode 100644 2025/02/run.ts diff --git a/2025/02/2025-02.run.xml b/2025/02/2025-02.run.xml new file mode 100644 index 0000000..537c8ee --- /dev/null +++ b/2025/02/2025-02.run.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/2025/02/index.test.ts b/2025/02/index.test.ts new file mode 100644 index 0000000..8478284 --- /dev/null +++ b/2025/02/index.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from 'bun:test'; +import { + findMultiRepetition, + findSingleRepetition, + solveFirst, + solveSecond, + sumMatches +} from './index.ts'; + +describe('2025-02', () => { + const testInput = `11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124`; + it('first', () => { + expect(solveFirst(testInput)).toBe(1227775554); + }); + it('second', () => { + expect(solveSecond(testInput)).toBe(4174379265); + }); + it('findSingleRepetition', () => { + expect(sumMatches([[11, 22]], findSingleRepetition)).toBe(11 + 22); + expect(sumMatches([[95, 115]], findSingleRepetition)).toBe(99); + expect(sumMatches([[1188511880, 1188511890]], findSingleRepetition)).toBe( + 1188511885 + ); + expect(sumMatches([[565653, 565659]], findSingleRepetition)).toBe(0); + }); + it('findMultiRepetition', () => { + expect(sumMatches([[11, 22]], findMultiRepetition)).toBe(11 + 22); + expect(sumMatches([[95, 115]], findMultiRepetition)).toBe(99 + 111); + expect(sumMatches([[1188511880, 1188511890]], findMultiRepetition)).toBe( + 1188511885 + ); + expect(sumMatches([[565653, 565659]], findMultiRepetition)).toBe(565656); + }); +}); diff --git a/2025/02/index.ts b/2025/02/index.ts new file mode 100644 index 0000000..e429a52 --- /dev/null +++ b/2025/02/index.ts @@ -0,0 +1,34 @@ +import type { Coordinate, Coordinates } from '../../utils/coordinates.ts'; + +function parseInput(input: string) { + const regex = /(\d*)-(\d*)/gm; + const match = input.matchAll(regex); + + return Array.from(match, (m) => [Number(m[1]), Number(m[2])] as Coordinate); +} + +export function sumMatches(ranges: Coordinates, foundRegex: RegExp) { + return ranges.reduce((s, range) => { + for (let i = range[0]; i <= range[1]; i++) { + if (foundRegex.test(String(i))) { + s += i; + } + } + + return s; + }, 0); +} + +export const findSingleRepetition = /^(\d*?)\1$/; +export function solveFirst(input: string): number { + const ranges = parseInput(input); + + return sumMatches(ranges, findSingleRepetition); +} + +export const findMultiRepetition = /^(\d*?)\1+$/; +export function solveSecond(input: string): number { + const ranges = parseInput(input); + + return sumMatches(ranges, findMultiRepetition); +} diff --git a/2025/02/run.ts b/2025/02/run.ts new file mode 100644 index 0000000..88783cd --- /dev/null +++ b/2025/02/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);