Skip to content

Commit 464aa88

Browse files
committed
AoC2025: day 11 part 2
1 parent 0dc5eb6 commit 464aa88

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

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

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

2024-2025/src/2025/day11/day11.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export async function part1(inputFile: string) {
66
}
77

88
export async function part2(inputFile: string) {
9-
return await day11(inputFile);
9+
return await day11(inputFile, countPathsVia);
1010
}
1111

1212
async function day11(inputFile: string, calcFn?: (graph: Map<string, string[]>) => number) {
@@ -58,3 +58,36 @@ function parseGraph(lines: string[]): Map<string, string[]> {
5858
}
5959
return graph;
6060
}
61+
62+
function countPathsVia(graph: Map<string, string[]>): number {
63+
const required = new Map<string, number>([
64+
["dac", 1],
65+
["fft", 2],
66+
]);
67+
const memo = new Map<string, number>();
68+
const visiting = new Set<string>();
69+
70+
const dfs = (node: string, mask: number): number => {
71+
const nextMask = mask | (required.get(node) ?? 0);
72+
if (node === "out") {
73+
return nextMask === 3 ? 1 : 0;
74+
}
75+
const key = `${node}|${nextMask}`;
76+
const cached = memo.get(key);
77+
if (cached !== undefined) return cached;
78+
if (visiting.has(key)) {
79+
return 0;
80+
}
81+
visiting.add(key);
82+
const neighbors = graph.get(node) ?? [];
83+
let total = 0;
84+
for (const next of neighbors) {
85+
total += dfs(next, nextMask);
86+
}
87+
visiting.delete(key);
88+
memo.set(key, total);
89+
return total;
90+
};
91+
92+
return dfs("svr", 0);
93+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
svr: aaa bbb
2+
aaa: fft
3+
fft: ccc
4+
bbb: tty
5+
tty: ccc
6+
ccc: ddd eee
7+
ddd: hub
8+
hub: fff
9+
eee: dac
10+
dac: fff
11+
fff: ggg hhh
12+
ggg: out
13+
hhh: out

0 commit comments

Comments
 (0)