From 26be46cdf3f68e5114b5304f9634d678c0c324df Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Sun, 14 Dec 2025 18:04:44 +0100 Subject: [PATCH] feat(day_10): file read and base done --- README.md | 5 +- src/__tests__/day_10_test.ts | 13 ++++ src/exercises/assets/day_10_test_input.txt | 3 + src/exercises/day_10.ts | 87 ++++++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/day_10_test.ts create mode 100644 src/exercises/assets/day_10_test_input.txt create mode 100644 src/exercises/day_10.ts diff --git a/README.md b/README.md index f34b07b..54abf69 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ | 4 | Printing Department | ⭐ | ⭐ | | 5 | Cafeteria | ⭐ | ⭐ | | 6 | Trash Compactor | ⭐ | ⭐ | -| 7 | Laboratories | ⭐ | | +| 7 | Laboratories | ⭐ | 🌓 | | 8 | Playground | ⭐ | ⭐ | -| 9 | Movie Theater | ⭐ | | +| 9 | Movie Theater | ⭐ | 🌓 | +| 10 | Factory | | | diff --git a/src/__tests__/day_10_test.ts b/src/__tests__/day_10_test.ts new file mode 100644 index 0000000..f2afea5 --- /dev/null +++ b/src/__tests__/day_10_test.ts @@ -0,0 +1,13 @@ +import { assertEquals } from "@std/assert"; +import { + count_min_button_presses, + read_machine_manuals, +} from "../exercises/day_10.ts"; + +Deno.test("Day 10 - A", async () => { + const manual_list = await read_machine_manuals( + "src/exercises/assets/day_10_test_input.txt", + ); + const result = count_min_button_presses(manual_list); + assertEquals(result, 7); +}); diff --git a/src/exercises/assets/day_10_test_input.txt b/src/exercises/assets/day_10_test_input.txt new file mode 100644 index 0000000..dd91d7b --- /dev/null +++ b/src/exercises/assets/day_10_test_input.txt @@ -0,0 +1,3 @@ +[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7} +[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2} +[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5} diff --git a/src/exercises/day_10.ts b/src/exercises/day_10.ts new file mode 100644 index 0000000..c06008c --- /dev/null +++ b/src/exercises/day_10.ts @@ -0,0 +1,87 @@ +enum Indicator { + ON = "#", + OFF = ".", +} + +type ButtonWiring = number[]; +type Joltage = number; + +interface MachineManual { + indicators: Indicator[]; + button_wirings: ButtonWiring[]; + joltage_requirements: Joltage[]; +} + +export default async function Factory() { +} + +export function count_min_button_presses(manuals: MachineManual[]): number { + let total_sum = 0; + manuals.forEach((manual) => { + 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); + + if (current_indicator.join("") === manual.indicators.join("")) { + min_presses = 1; + return; + } + + for ( + let inner_i = wiring_i + 1; + inner_i < wiring_list.length; + inner_i++ + ) { + current_indicator = apply_wiring_to_indicators( + wiring_list[wiring_i], + current_indicator, + ); + } + }); + + total_sum += min_presses; + }); +} + +function apply_wiring_to_indicators( + wiring: ButtonWiring, + indicator: Indicator[], +): Indicator[] { + return indicator.map((w, i) => + wiring.includes(i) ? w === Indicator.ON ? Indicator.OFF : Indicator.ON : w + ); +} + +export async function read_machine_manuals( + path: string, +): Promise { + const line_regex = /\[([.#]+)\] (.*)+ \{(.*)\}/g; + const input_txt = await Deno.readTextFile(path); + const lines = input_txt.split("\n"); + + const manual_list: MachineManual[] = lines.map((line) => { + const matched_line = line.matchAll(line_regex)?.toArray()?.[0]; + if (matched_line && matched_line.length > 3) { + const indicators = matched_line[1].toString().split("").map((ch) => + ch === Indicator.ON ? Indicator.ON : Indicator.OFF + ); + const buttons = matched_line[2].split(" ").map((b) => + b.replaceAll("(", "").replaceAll(")", "").split(",").map((n) => + parseInt(n) + ) + ); + const joltages = matched_line[3].split(",").map((j) => parseInt(j)); + return { + indicators, + button_wirings: buttons, + joltage_requirements: joltages, + }; + } + return null; + }).filter((v) => v !== null); + + return manual_list; +}