Skip to content

Commit 0ec2cf6

Browse files
committed
Solve 2025-02
1 parent 0ed63a2 commit 0ec2cf6

4 files changed

Lines changed: 87 additions & 0 deletions

File tree

2025/02/2025-02.run.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="2025-02" type="BunRunConfiguration">
3+
<option name="program" value="2025/02/run.ts" />
4+
<option name="workingDirectory" value="$PROJECT_DIR$" />
5+
<method v="2" />
6+
</configuration>
7+
</component>

2025/02/index.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, expect, it } from 'bun:test';
2+
import {
3+
findMultiRepetition,
4+
findSingleRepetition,
5+
solveFirst,
6+
solveSecond,
7+
sumMatches
8+
} from './index.ts';
9+
10+
describe('2025-02', () => {
11+
const testInput = `11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124`;
12+
it('first', () => {
13+
expect(solveFirst(testInput)).toBe(1227775554);
14+
});
15+
it('second', () => {
16+
expect(solveSecond(testInput)).toBe(4174379265);
17+
});
18+
it('findSingleRepetition', () => {
19+
expect(sumMatches([[11, 22]], findSingleRepetition)).toBe(11 + 22);
20+
expect(sumMatches([[95, 115]], findSingleRepetition)).toBe(99);
21+
expect(sumMatches([[1188511880, 1188511890]], findSingleRepetition)).toBe(
22+
1188511885
23+
);
24+
expect(sumMatches([[565653, 565659]], findSingleRepetition)).toBe(0);
25+
});
26+
it('findMultiRepetition', () => {
27+
expect(sumMatches([[11, 22]], findMultiRepetition)).toBe(11 + 22);
28+
expect(sumMatches([[95, 115]], findMultiRepetition)).toBe(99 + 111);
29+
expect(sumMatches([[1188511880, 1188511890]], findMultiRepetition)).toBe(
30+
1188511885
31+
);
32+
expect(sumMatches([[565653, 565659]], findMultiRepetition)).toBe(565656);
33+
});
34+
});

2025/02/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Coordinate, Coordinates } from '../../utils/coordinates.ts';
2+
3+
function parseInput(input: string) {
4+
const regex = /(\d*)-(\d*)/gm;
5+
const match = input.matchAll(regex);
6+
7+
return Array.from(match, (m) => [Number(m[1]), Number(m[2])] as Coordinate);
8+
}
9+
10+
export function sumMatches(ranges: Coordinates, foundRegex: RegExp) {
11+
return ranges.reduce((s, range) => {
12+
for (let i = range[0]; i <= range[1]; i++) {
13+
if (foundRegex.test(String(i))) {
14+
s += i;
15+
}
16+
}
17+
18+
return s;
19+
}, 0);
20+
}
21+
22+
export const findSingleRepetition = /^(\d*?)\1$/;
23+
export function solveFirst(input: string): number {
24+
const ranges = parseInput(input);
25+
26+
return sumMatches(ranges, findSingleRepetition);
27+
}
28+
29+
export const findMultiRepetition = /^(\d*?)\1+$/;
30+
export function solveSecond(input: string): number {
31+
const ranges = parseInput(input);
32+
33+
return sumMatches(ranges, findMultiRepetition);
34+
}

2025/02/run.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { join } from 'path';
2+
import { solveFirst, solveSecond } from './index.ts';
3+
4+
const input = await Bun.file(join(__dirname, 'input.txt')).text();
5+
6+
const firstAnswer = solveFirst(input);
7+
8+
console.log(firstAnswer);
9+
10+
const secondAnswer = solveSecond(input);
11+
12+
console.log(secondAnswer);

0 commit comments

Comments
 (0)