this repo has no description
0
fork

Configure Feed

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

tried product of steps to first end node

didn't work! noticed this worked for the example data, but my answer is too high

+18 -24
+17 -23
2023/day08/part2.ts
··· 1 1 type Node = { L: string; R: string }; 2 2 type Network = Record<string, Node>; 3 3 4 - export function answer(input: string): number { 4 + export function answer(input: string) { 5 5 const [instructions, , ...networkRaw] = input.split("\n"); 6 6 7 7 const network: Network = networkRaw.reduce((network, line) => { ··· 10 10 return { ...network, [id]: { L, R } }; 11 11 }, {}); 12 12 13 - const pathsFinished = (paths: string[]): boolean => 14 - paths.every((p) => p.endsWith("Z")); 15 - 16 - const step = (paths: string[], dir: keyof Node): string[] => 17 - paths.map((c) => network[c][dir]); 13 + const paths = Object.keys(network).filter((n) => n.endsWith("A")); 14 + return BigInt( 15 + paths 16 + .map((p) => countSteps(p, network, instructions)) 17 + .reduce((total, x) => (total *= x), 1), 18 + ); 19 + } 18 20 19 - let paths = Object.keys(network).filter((n) => n.endsWith("A")); 21 + function countSteps( 22 + start: string, 23 + network: Network, 24 + instructions: string, 25 + ): number { 26 + let currentNode = start; 20 27 let stepCount = 0; 21 - 22 - console.log(`network: ${Object.keys(network).length}`); 23 - console.log(`paths: ${paths.length}`); 24 - 25 - while (!pathsFinished(paths)) { 28 + while (!currentNode.endsWith("Z")) { 26 29 for (const dir of instructions.split("")) { 27 - logPaths(stepCount, paths); 28 - paths = step(paths, dir as keyof Node); 30 + currentNode = network[currentNode][dir as keyof Node]; 29 31 stepCount++; 30 - if (pathsFinished(paths)) break; 32 + if (currentNode.endsWith("Z")) break; 31 33 } 32 34 } 33 35 34 36 return stepCount; 35 - } 36 - 37 - function logPaths(stepCount: number, paths: string[]) { 38 - console.log( 39 - `${stepCount.toString().padStart(20, "0")} ${paths 40 - .map((p) => (p.endsWith("Z") ? "x" : ".")) 41 - .join("")}`, 42 - ); 43 37 } 44 38 45 39 if (import.meta.main) {
+1 -1
2023/day08/test.ts
··· 41 41 "22Z = (22B, 22B)", 42 42 "XXX = (XXX, XXX)", 43 43 ].join("\n"); 44 - assertEquals(p2.answer(examples), 6); 44 + assertEquals(p2.answer(examples), BigInt(6)); 45 45 });