From 9a42d884d04c713abd6fe09e4090423ed48eb246 Mon Sep 17 00:00:00 2001 From: Seth Etter Date: Sat, 9 Dec 2023 08:32:10 -0600 Subject: [PATCH] no bueno --- 2023/day08/part2.ts | 62 ++++++++++++++++++++++++++++++++++----------- 2023/day08/test.ts | 2 +- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/2023/day08/part2.ts b/2023/day08/part2.ts index d92381e..2ed5325 100644 --- a/2023/day08/part2.ts +++ b/2023/day08/part2.ts @@ -1,39 +1,71 @@ type Node = { L: string; R: string }; + type Network = Record; +type Path = { + possiblePaths: string[]; +}; + +// + export function answer(input: string) { const [instructions, , ...networkRaw] = input.split("\n"); - const network: Network = networkRaw.reduce((network, line) => { - const [id, leftAndRight] = line.split(" = "); - const [L, R] = leftAndRight.replaceAll(/[\(\)]/g, "").split(", "); - return { ...network, [id]: { L, R } }; - }, {}); - - const paths = Object.keys(network).filter((n) => n.endsWith("A")); - return BigInt( - paths - .map((p) => countSteps(p, network, instructions)) - .reduce((total, x) => (total *= x), 1), - ); + const parseNetwork = (lines: string[]): Network => + lines.reduce((network, line) => { + const [id, leftAndRight] = line.split(" = "); + const [L, R] = leftAndRight.replaceAll(/[\(\)]/g, "").split(", "); + return { ...network, [id]: { L, R } }; + }, {}); + + const network: Network = parseNetwork(networkRaw); + + // build the paths to X for each starting path + // step forward by longest path + // new positions are stepSize % pathSize + + const paths = Object.keys(network) + .filter((n) => n.endsWith("A")) + .map((p) => { + const path = buildPath(p, network, instructions); + console.log(`plength: ${path.length}, last: ${path[path.length - 1]}`); + return { current: path.length - 1, path }; + }); + + const stepSize = Math.max(...paths.map(({ path }) => path.length)); + let stepTotal = stepSize; + + while (!paths.every((p) => p.path[p.current] === "Z")) { + console.log(`step by ${stepSize}`); + paths.forEach((p) => { + p.current += stepSize; + p.current = p.path.length % p.current; + console.log(p); + }); + stepTotal += stepSize; + } + + return stepTotal; } -function countSteps( +function buildPath( start: string, network: Network, instructions: string, -): number { +): string[] { let currentNode = start; let stepCount = 0; + const endingChars = [start[2]]; while (!currentNode.endsWith("Z")) { for (const dir of instructions.split("")) { currentNode = network[currentNode][dir as keyof Node]; stepCount++; + endingChars.push(currentNode[2]); if (currentNode.endsWith("Z")) break; } } - return stepCount; + return endingChars; } if (import.meta.main) { diff --git a/2023/day08/test.ts b/2023/day08/test.ts index be1c10c..5ced77b 100644 --- a/2023/day08/test.ts +++ b/2023/day08/test.ts @@ -41,5 +41,5 @@ Deno.test("part2", () => { "22Z = (22B, 22B)", "XXX = (XXX, XXX)", ].join("\n"); - assertEquals(p2.answer(examples), BigInt(6)); + assertEquals(p2.answer(examples), 6); }); -- 2.51.2