Solve 2025-02

This commit is contained in:
2025-12-02 11:01:55 +01:00
parent 0ed63a276e
commit 0ec2cf656d
4 changed files with 87 additions and 0 deletions
+34
View File
@@ -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);
}