this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

logic works, but still running forever

+9 -59
+9 -59
2023/day08/part2.ts
··· 1 1 type Node = { L: string; R: string; stepsToZCache: Record<string, number> }; 2 2 type Network = Record<string, Node>; 3 - type Path = { 4 - checkpoint: string; 5 - sinceCheckpoint: number; 6 - zsSeen: string[]; 7 - current: string; 8 - }; 9 - 10 - // for every node in the network, calculate the steps 3 + type Path = { checkpoint: string; sinceCheckpoint: number; current: string }; 11 4 12 5 export function answer(input: string): number { 13 6 const [instructionsRaw, , ...networkRaw] = input.split("\n"); 14 - const instructions = instructionsRaw.split(""); 15 7 16 8 const network: Network = networkRaw.reduce((network, line) => { 17 9 const [id, leftAndRight] = line.split(" = "); ··· 19 11 return { ...network, [id]: { L, R, stepsToZCache: {} } }; 20 12 }, {}); 21 13 22 - const cacheKey = (instIdx: number, endNode: string) => 23 - `${endNode}.${instIdx}`; 24 - 25 - const allPaths: Path[] = Object.keys(network).map((n) => ({ 26 - checkpoint: n, 27 - sinceCheckpoint: 0, 28 - current: n, 29 - zsSeen: [], 30 - })); 31 - 32 - walkPaths(allPaths, instructions, network); 33 - 34 - // const startPaths: Path[] = Object.keys(network) 35 - // .filter((n) => n.endsWith("A")) 36 - // .map((n) => ({ checkpoint: n, sinceCheckpoint: 0, current: n, zsSeen: [] })); 37 - 38 - // const endNodes = Object.keys(network).filter((n) => n.endsWith("Z")); 39 - 40 - // MATH?!!! 41 - // - permutations of `stepsToZ` 42 - // - what operation can I perform with these numbers to know that it is the right answer? 43 - 44 - return 1; 45 - } 14 + const paths: Path[] = Object.keys(network) 15 + .filter((n) => n.endsWith("A")) 16 + .map((n) => ({ checkpoint: n, sinceCheckpoint: 0, current: n })); 46 17 47 - function walkPaths(paths: Path[], instructions: string[], network: Network) { 48 - const endNodes = Object.keys(network).filter((n) => n.endsWith("Z")); 18 + let step = -1; 19 + const instructions = instructionsRaw.split(""); 49 20 50 - let step = -1; 51 21 while (true) { 52 22 step++; 53 23 for (let instIdx = 0; instIdx < instructions.length; instIdx++) { 24 + // console.log(`step: ${step}`); 54 25 paths.forEach((p, _pathIdx) => { 55 26 p.sinceCheckpoint++; 56 27 57 28 p.current = network[p.current][instructions[instIdx] as "L" | "R"]; 58 29 59 30 if (p.current.endsWith("Z")) { 60 - console.log( 61 - `found path to z! ${p.checkpoint} from inst ${instIdx} takes ${p.sinceCheckpoint} steps`, 62 - ); 63 31 network[p.checkpoint].stepsToZCache[`${instIdx}`] = p.sinceCheckpoint; 64 32 65 33 p.checkpoint = p.current; ··· 67 35 } 68 36 }); 69 37 70 - // const stepsToZ = paths 71 - // .map((p) => 72 - // endNodes.map( 73 - // (n) => network[p.current].stepsToZCache[`${n}.${instIdx}`], 74 - // ), 75 - // ) 76 - // .flat(); 77 38 const stepsToZ = paths.map( 78 39 (p) => network[p.current].stepsToZCache[`${instIdx}`], 79 40 ); 80 41 81 - console.log( 82 - Object.keys(network).map( 83 - (n) => `${n}: ${JSON.stringify(network[n].stepsToZCache)}`, 84 - ), 85 - ); 86 - 87 - if ( 88 - Object.values(network).every( 89 - (n) => Object.values(n.stepsToZCache).length === endNodes.length, 90 - ) 91 - ) { 92 - return; 93 - // return stepsToZ.reduce((product, x) => (product *= x), 1); 42 + if (stepsToZ.every((s) => s !== undefined)) { 43 + return stepsToZ.reduce((product, x) => (product *= x), 1); 94 44 } 95 45 } 96 46 }