diff --git a/README.md b/README.md index 558c898..f45d582 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ [Advent of Code](https://adventofcode.com/) problems of 2025. +### Progress + | Day | Name | Part 1 | Part 2 | | :-: | :------------------ | :----: | :----: | | 1 | Secret Entrance | ⭐ | ⭐ | @@ -10,7 +12,13 @@ | 4 | Printing Department | ⭐ | ⭐ | | 5 | Cafeteria | ⭐ | ⭐ | | 6 | Trash Compactor | ⭐ | ⭐ | -| 7 | Laboratories | ⭐ | 🌓 | +| 7 | Laboratories | ⭐ | ⏱️ | | 8 | Playground | ⭐ | ⭐ | | 9 | Movie Theater | ⭐ | 🌓 | -| 10 | Factory | ⭐ | | +| 10 | Factory | ⭐ | ⏱️ | + +### Symbols + +- 🌓 Completed with test input but some corner-case left on real input. +- ⏱️ Completed with test input but to slow to complete with real input. +- ⭐ All done. diff --git a/src/__tests__/day_10_test.ts b/src/__tests__/day_10_test.ts index f2afea5..07515bd 100644 --- a/src/__tests__/day_10_test.ts +++ b/src/__tests__/day_10_test.ts @@ -1,6 +1,7 @@ import { assertEquals } from "@std/assert"; import { count_min_button_presses, + count_min_button_presses_joltage, read_machine_manuals, } from "../exercises/day_10.ts"; @@ -11,3 +12,11 @@ Deno.test("Day 10 - A", async () => { const result = count_min_button_presses(manual_list); assertEquals(result, 7); }); + +Deno.test("Day 10 - B", async () => { + const manual_list = await read_machine_manuals( + "src/exercises/assets/day_10_test_input.txt", + ); + const result = count_min_button_presses_joltage(manual_list); + assertEquals(result, 33); +}); diff --git a/src/exercises/day_10.ts b/src/exercises/day_10.ts index 8fd0ae6..e3c2bf5 100644 --- a/src/exercises/day_10.ts +++ b/src/exercises/day_10.ts @@ -22,11 +22,16 @@ export default async function Factory() { ">> Min button presses to start: ", min_presses, ); + const min_joltage_presses = count_min_button_presses_joltage(manual_list); + console.log( + ">> Min button presses to function: ", + min_joltage_presses, + ); } export function count_min_button_presses(manuals: MachineManual[]): number { let total_sum = 0; - manuals.forEach((manual, m_i) => { + manuals.forEach((manual) => { const wiring_list = manual.button_wirings; let min_presses = Infinity; @@ -64,6 +69,52 @@ export function count_min_button_presses(manuals: MachineManual[]): number { return total_sum; } +export function count_min_button_presses_joltage( + manuals: MachineManual[], +): number { + let total_sum = 0; + manuals.forEach((manual) => { + const wiring_list = manual.button_wirings; + const target_joltage = manual.joltage_requirements; + + // FIXME: calc max possible size + const MAX_SIZE = 8; + const combination_list = make_all_combinations_repeating( + wiring_list, + MAX_SIZE, + ); + + let min_presses = Infinity; + combination_list.forEach((combination) => { + // Apply combinations + let joltage_indicator = manual.joltage_requirements.map((_) => 0); + combination.forEach((c, i) => { + for (let j = 0; j < c; j++) { + joltage_indicator = apply_wiring_to_joltage( + wiring_list[i], + joltage_indicator, + ); + } + }); + const combination_size = combination.reduce( + (prev, current) => current + prev, + 0, + ); + // Update manual min + if ( + target_joltage.join("") === joltage_indicator.join("") && + min_presses > combination_size + ) { + min_presses = combination_size; + } + }); + + total_sum += min_presses; + }); + + return total_sum; +} + function apply_wiring_to_indicators( wiring: ButtonWiring, indicator: Indicator[], @@ -73,6 +124,13 @@ function apply_wiring_to_indicators( ); } +function apply_wiring_to_joltage( + wiring: ButtonWiring, + joltage: Joltage[], +): Joltage[] { + return joltage.map((j, i) => wiring.includes(i) ? j + 1 : j); +} + function make_all_combinations_of_size( list: T, size: number, @@ -99,6 +157,25 @@ function make_all_combinations_of_size( return result_list; } +function make_all_combinations_repeating( + list: T, + max_repetitions: number, +): number[][] { + const binary_base = max_repetitions + 1; + const binary_cap = Math.pow(binary_base, list.length); + + const result_list: number[][] = []; + + for (let i = 0; i < binary_cap; i++) { + const combination = i.toString(binary_base).padStart(list.length, "0") + .split(""); + + result_list.push(combination.map((c) => parseInt(c, binary_base))); + } + + return result_list; +} + export async function read_machine_manuals( path: string, ): Promise {