Skip to content

Commit 348284a

Browse files
committed
AoC2025: day 7 part 2
1 parent a6354ca commit 348284a

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

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

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

2024-2025/src/2025/day07/day07.ts

Lines changed: 23 additions & 2 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 day7(inputFile);
10+
return await day7(inputFile, findBeanSplitTimelines);
1111
}
1212

1313
async function day7(inputFile: string, calcFn?: (grid: Grid, initialPos: Coord) => number) {
@@ -49,4 +49,25 @@ function findBeanSplits(grid: Grid, initialPos: Coord): number {
4949
}
5050

5151
return splits;
52-
}
52+
}
53+
54+
function findBeanSplitTimelines(grid: Grid, initialPos: Coord): number {
55+
const memo = new Map<string, number>();
56+
57+
function timelines(coord: Coord): number {
58+
const key = coord.serialize();
59+
if (memo.has(key)) return memo.get(key)!;
60+
61+
const cellValue = grid.get(key);
62+
if (cellValue === undefined) return 1;
63+
64+
const result = cellValue === '^'
65+
? timelines(move(coord, Cardinal.WEST)) + timelines(move(coord, Cardinal.EAST))
66+
: timelines(move(coord, Cardinal.SOUTH));
67+
68+
memo.set(key, result);
69+
return result;
70+
}
71+
72+
return timelines(move(initialPos, Cardinal.SOUTH));
73+
}

0 commit comments

Comments
 (0)