Skip to content

Commit 94a012e

Browse files
committed
AoC2025: day 4 part 2
1 parent db24007 commit 94a012e

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('2025 Day 4', () => {
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(43);
11+
expect(await part2('input')).toEqual(7922);
1212
});
1313
});

2024-2025/src/2025/day04/day04.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export async function part1(inputFile: string) {
77
}
88

99
export async function part2(inputFile: string) {
10-
return await day4(inputFile);
10+
return await day4(inputFile, findAndRemoveAccessiblePaperRolls);
1111
}
1212

1313
async function day4(inputFile: string, calcFn?: (grid: Grid) => number) {
@@ -17,17 +17,34 @@ async function day4(inputFile: string, calcFn?: (grid: Grid) => number) {
1717
return calcFn?.(grid);
1818
}
1919

20-
function findAccessiblePaperRolls(grid: Grid) {
21-
let paperRolls = 0;
22-
20+
function getRollCoords(grid: Map<string, string>): Coord[] {
21+
let paperRolls: Coord[] = [];
2322
grid.forEach((value, coord) => {
2423
if (value === '@') {
2524
const adjacentCells = getNeighbors(Coord.deserialize(coord), grid, true);
2625
const rolls = adjacentCells.filter(cell => cell === '@').length;
2726
if (rolls < 4)
28-
paperRolls += 1;
27+
paperRolls.push(Coord.deserialize(coord));
2928
}
3029
});
31-
3230
return paperRolls;
31+
}
32+
33+
function findAccessiblePaperRolls(grid: Grid) {
34+
return getRollCoords(grid).length;
35+
}
36+
37+
function findAndRemoveAccessiblePaperRolls(grid: Grid) {
38+
let totalAccessiblePaperRolls = 0;
39+
40+
let paperRollCoords: Coord[] = getRollCoords(grid);
41+
while (paperRollCoords.length > 0) {
42+
totalAccessiblePaperRolls += paperRollCoords.length;
43+
44+
paperRollCoords.forEach((p) => grid.set(p.serialize(), '.'));
45+
46+
paperRollCoords = getRollCoords(grid);
47+
}
48+
49+
return totalAccessiblePaperRolls;
3350
}

0 commit comments

Comments
 (0)