Skip to content

Commit f9f84c6

Browse files
committed
AoC2025: day 2 part 1
1 parent 7401081 commit f9f84c6

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

2024-2025/src/2025/day02/day02.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import {part1, part2} from "./day02";
22

33
describe('2025 Day 2', () => {
44
test('Part 1', async () => {
5-
expect(await part1('testInput1')).toEqual(11);
6-
expect(await part1('input')).toEqual(3246517);
5+
expect(await part1('testInput1')).toEqual(1227775554);
6+
expect(await part1('input')).toEqual(19574776074);
77
});
88

99
test('Part 2', async () => {
10-
expect(await part2('testInput1')).toEqual(31);
11-
expect(await part2('input')).toEqual(29379307);
10+
// expect(await part2('testInput1')).toEqual(31);
11+
// expect(await part2('input')).toEqual(29379307);
1212
});
1313
});

2024-2025/src/2025/day02/day02.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,47 @@ import path from "node:path";
22
import {readInputLineByLine} from "@utils/io";
33

44
export async function part1(inputFile: string) {
5-
return await day2(inputFile);
5+
return await day2(inputFile, findInvalidIds);
66
}
77

88
export async function part2(inputFile: string) {
99
return await day2(inputFile);
1010
}
1111

12-
async function day2(inputFile: string, calcFn?: (lines: string[]) => number) {
12+
type NumRange = { from: number; to: number };
13+
14+
async function day2(inputFile: string, calcFn?: (ranges: NumRange[]) => number) {
1315
const inputPath = path.join(__dirname, inputFile);
1416
const lines = await readInputLineByLine(inputPath);
1517

16-
return calcFn?.(lines);
18+
const rangeStrings = lines[0].split(',');
19+
const ranges: NumRange[] = rangeStrings.map(range => {
20+
const res = range.split('-');
21+
return {
22+
from: Number.parseInt(res[0]),
23+
to: Number.parseInt(res[1]),
24+
}
25+
})
26+
return calcFn?.(ranges);
27+
}
28+
29+
function findInvalidIds(ranges: NumRange[]): number {
30+
let invalidIdSum = 0;
31+
ranges.forEach(({ from, to }) => {
32+
33+
for (let n = from; n <= to; n ++) {
34+
const numAsString = `${n}`;
35+
if (numAsString.length % 2 !== 0)
36+
continue;
37+
38+
const mid = numAsString.length / 2;
39+
40+
const firstHalf = numAsString.slice(0, mid);
41+
const secondHalf = numAsString.slice(mid);
42+
if (firstHalf === secondHalf)
43+
invalidIdSum += n;
44+
}
45+
})
46+
47+
return invalidIdSum;
1748
}

0 commit comments

Comments
 (0)