Skip to content

Commit db24007

Browse files
committed
AoC2025: day 4 part 1
1 parent 91425f0 commit db24007

3 files changed

Lines changed: 56 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 "./day04";
2+
3+
describe('2025 Day 4', () => {
4+
test('Part 1', async () => {
5+
expect(await part1('testInput1')).toEqual(13);
6+
expect(await part1('input')).toEqual(1372);
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/day04/day04.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 {Coord, getNeighbors, Grid, readLinesToGrid} from "@utils/grid";
4+
5+
export async function part1(inputFile: string) {
6+
return await day4(inputFile, findAccessiblePaperRolls);
7+
}
8+
9+
export async function part2(inputFile: string) {
10+
return await day4(inputFile);
11+
}
12+
13+
async function day4(inputFile: string, calcFn?: (grid: Grid) => number) {
14+
const inputPath = path.join(__dirname, inputFile);
15+
const lines = await readInputLineByLine(inputPath);
16+
const { grid } = readLinesToGrid(lines);
17+
return calcFn?.(grid);
18+
}
19+
20+
function findAccessiblePaperRolls(grid: Grid) {
21+
let paperRolls = 0;
22+
23+
grid.forEach((value, coord) => {
24+
if (value === '@') {
25+
const adjacentCells = getNeighbors(Coord.deserialize(coord), grid, true);
26+
const rolls = adjacentCells.filter(cell => cell === '@').length;
27+
if (rolls < 4)
28+
paperRolls += 1;
29+
}
30+
});
31+
32+
return paperRolls;
33+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
..@@.@@@@.
2+
@@@.@.@.@@
3+
@@@@@.@.@@
4+
@.@@@@..@.
5+
@@.@@@@.@@
6+
.@@@@@@@.@
7+
.@.@.@.@@@
8+
@.@@@.@@@@
9+
.@@@@@@@@.
10+
@.@.@@@.@.

0 commit comments

Comments
 (0)