Skip to content

Commit dd41500

Browse files
committed
AoC2025: day 6 part 1
1 parent 3b6f576 commit dd41500

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {part1, part2} from "./day06";
2+
3+
describe('2025 Day 6', () => {
4+
test('Part 1', async () => {
5+
expect(await part1('testInput1')).toEqual(4277556);
6+
expect(await part1('input')).toEqual(5877594983578);
7+
});
8+
9+
test('Part 2', async () => {
10+
// expect(await part2('testInput1')).toEqual(31);
11+
// expect(await part2('input')).toEqual(29379307);
12+
});
13+
});

2024-2025/src/2025/day06/day06.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import path from "node:path";
2+
import {readInputLineByLine} from "@utils/io";
3+
import {forEach} from "mathjs";
4+
5+
export async function part1(inputFile: string) {
6+
return await day6(inputFile, calcGrantTotal);
7+
}
8+
9+
export async function part2(inputFile: string) {
10+
return await day6(inputFile);
11+
}
12+
13+
async function day6(inputFile: string, calcFn?: (numberRows: number[][], ops: string[]) => number) {
14+
const inputPath = path.join(__dirname, inputFile);
15+
const lines = await readInputLineByLine(inputPath);
16+
17+
const numberRows: number[][] = lines.slice(0, -1).map(line => line.trim().split(/\s+/).map(Number));
18+
const ops: string[] = lines[lines.length - 1].split(/\s+/);
19+
20+
return calcFn?.(numberRows, ops);
21+
}
22+
23+
function calcGrantTotal(numberRows: number[][], ops: string[]): number {
24+
let total: number = 0;
25+
const cols = numberRows[0].length;
26+
for (let i = 0; i < cols; i++) {
27+
const op = ops[i];
28+
const column: number[] = numberRows.map(row => row[i]);
29+
const result = column.reduce((acc, cur) => op === '+' ? acc + cur : acc * cur, op === '+' ? 0 : 1);
30+
total += result;
31+
}
32+
return total;
33+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
123 328 51 64
2+
45 64 387 23
3+
6 98 215 314
4+
* + * +

0 commit comments

Comments
 (0)