@@ -6,7 +6,7 @@ export async function part1(inputFile: string) {
66}
77
88export async function part2 ( inputFile : string ) {
9- return await day11 ( inputFile ) ;
9+ return await day11 ( inputFile , countPathsVia ) ;
1010}
1111
1212async 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+ }
0 commit comments