From 464206c04cf12322be1d039b27d6dc89a20f25d4 Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Tue, 9 Dec 2025 18:10:21 +0100 Subject: [PATCH] fix(day_7): B part fixed but inefficient --- src/exercises/day_7.ts | 53 +++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/src/exercises/day_7.ts b/src/exercises/day_7.ts index dc6aa7f..5f4355f 100644 --- a/src/exercises/day_7.ts +++ b/src/exercises/day_7.ts @@ -5,6 +5,8 @@ export default async function Laboratories() { "src/exercises/assets/day_7_input.txt", ); console.log(">> Bean splits: ", count_bean_splits(tachyon_manifold)); + const total_splits = count_quantum_splits(tachyon_manifold); + console.log(">> Cuantum splits: ", total_splits); } export function count_bean_splits(tm: TachyonManifold): number { @@ -44,16 +46,49 @@ export function count_bean_splits(tm: TachyonManifold): number { export function count_quantum_splits(tm: TachyonManifold): number { const full_tree = generate_full_quantum_tree(tm); - console.log( - ">> Full tree\n", - full_tree.map((row) => row.join("")).join("\n"), - ); - let total = 0; - console.log("==", tm[tm.length - 2]); - for (let i = 0; i < tm[0].length; i++) { - if (tm[tm.length - 2][i] === "|") { - total += count_quantum_paths_upwards(tm, i, tm.length - 2) + 1; + // Downwards + const init = full_tree[0].findIndex((p) => p === "S"); + return count_quantum_paths_downwards(full_tree, init, 1); + + // Upwards + // let total = 0; + // for (let i = 0; i < tm[0].length; i++) { + // if (tm[tm.length - 2][i] === "|") { + // total += count_quantum_paths_upwards(full_tree, i, tm.length - 2) + 1; + // } + // } + // return total; +} + +function count_quantum_paths_downwards( + tm: TachyonManifold, + initial_x: number, + initial_y: number, +): number { + let total = 0; + const FILE_END = tm.length - 2; + const current = tm[initial_y][initial_x]; + + if (initial_y > FILE_END) { + return 0; + } + if (initial_y === FILE_END && current === "|") { + return 1; + } else if (initial_y === FILE_END) { + return 0; + } + + const next = tm[initial_y + 1][initial_x]; + switch (next) { + case "|": { + total += count_quantum_paths_downwards(tm, initial_x, initial_y + 1); + break; + } + case "^": { + total += count_quantum_paths_downwards(tm, initial_x - 1, initial_y + 1); + total += count_quantum_paths_downwards(tm, initial_x + 1, initial_y + 1); + break; } }