From 5969abf45845baf4f0c744bd8c3438cc973064d7 Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Sun, 14 Dec 2025 23:01:08 +0100 Subject: [PATCH] chore: day 10 WIP --- src/exercises/day_10.ts | 75 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/src/exercises/day_10.ts b/src/exercises/day_10.ts index c06008c..6d62bda 100644 --- a/src/exercises/day_10.ts +++ b/src/exercises/day_10.ts @@ -17,15 +17,20 @@ export default async function Factory() { export function count_min_button_presses(manuals: MachineManual[]): number { let total_sum = 0; - manuals.forEach((manual) => { + manuals.forEach((manual, i) => { const wiring_list = manual.button_wirings; let min_presses = Infinity; wiring_list.forEach((wiring, wiring_i) => { - let current_indicator = manual.indicators.map((i) => i); // Clone the indicator - current_indicator = apply_wiring_to_indicators(wiring, current_indicator); + let pairing_indicator = manual.indicators.map((_) => Indicator.OFF); - if (current_indicator.join("") === manual.indicators.join("")) { + // Apply the current wiring + pairing_indicator = apply_wiring_to_indicators( + wiring, + pairing_indicator, + ); + + if (pairing_indicator.join("") === manual.indicators.join("")) { min_presses = 1; return; } @@ -35,15 +40,71 @@ export function count_min_button_presses(manuals: MachineManual[]): number { inner_i < wiring_list.length; inner_i++ ) { - current_indicator = apply_wiring_to_indicators( - wiring_list[wiring_i], - current_indicator, + pairing_indicator = manual.indicators.map((_) => Indicator.OFF); + // Apply the current wiring + pairing_indicator = apply_wiring_to_indicators( + wiring, + pairing_indicator, ); + // Apply the next wiring + pairing_indicator = apply_wiring_to_indicators( + wiring_list[inner_i], + pairing_indicator, + ); + + if (pairing_indicator.join("") === manual.indicators.join("")) { + min_presses = 2; + break; + } } }); + // If - not a simple press/pairing found + // then - check all combinations + // + // For size, starts at 1 + // for index spacing? + // OR binary + if (min_presses === Infinity) { + let cumulative_presses = 0; + // DOES NOT MAKE ALL COMBINATIONS, only secuentials + wiring_list.forEach((wiring, wiring_i) => { + let cumulative_indicator = manual.indicators.map((_) => Indicator.OFF); + + // Apply the current wiring + cumulative_indicator = apply_wiring_to_indicators( + wiring, + cumulative_indicator, + ); + cumulative_presses += 1; + + for ( + let inner_i = wiring_i + 1; + inner_i < wiring_list.length; + inner_i++ + ) { + cumulative_presses += 1; + // Apply the next wiring + cumulative_indicator = apply_wiring_to_indicators( + wiring_list[inner_i], + cumulative_indicator, + ); + if (cumulative_indicator.join("") === manual.indicators.join("")) { + break; + } + } + + if (cumulative_presses < min_presses) { + min_presses = cumulative_presses; + } + }); + } + + console.log(">> Total and min", { total_sum, min_presses }); total_sum += min_presses; }); + + return total_sum; } function apply_wiring_to_indicators(