Solve 2025-02
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="2025-02" type="BunRunConfiguration">
|
||||||
|
<option name="program" value="2025/02/run.ts" />
|
||||||
|
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
Reference in New Issue
Block a user