feat(day_10): part B done fortest input but to slow

This commit is contained in:
2025-12-15 21:56:58 +01:00
parent f0bd46a65a
commit b19fc90241
3 changed files with 97 additions and 3 deletions

View File

@@ -2,6 +2,8 @@
[Advent of Code](https://adventofcode.com/) problems of 2025. [Advent of Code](https://adventofcode.com/) problems of 2025.
### Progress
| Day | Name | Part 1 | Part 2 | | Day | Name | Part 1 | Part 2 |
| :-: | :------------------ | :----: | :----: | | :-: | :------------------ | :----: | :----: |
| 1 | Secret Entrance | ⭐ | ⭐ | | 1 | Secret Entrance | ⭐ | ⭐ |
@@ -10,7 +12,13 @@
| 4 | Printing Department | ⭐ | ⭐ | | 4 | Printing Department | ⭐ | ⭐ |
| 5 | Cafeteria | ⭐ | ⭐ | | 5 | Cafeteria | ⭐ | ⭐ |
| 6 | Trash Compactor | ⭐ | ⭐ | | 6 | Trash Compactor | ⭐ | ⭐ |
| 7 | Laboratories | ⭐ | 🌓 | | 7 | Laboratories | ⭐ | ⏱️ |
| 8 | Playground | ⭐ | ⭐ | | 8 | Playground | ⭐ | ⭐ |
| 9 | Movie Theater | ⭐ | 🌓 | | 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.

View File

@@ -1,6 +1,7 @@
import { assertEquals } from "@std/assert"; import { assertEquals } from "@std/assert";
import { import {
count_min_button_presses, count_min_button_presses,
count_min_button_presses_joltage,
read_machine_manuals, read_machine_manuals,
} from "../exercises/day_10.ts"; } from "../exercises/day_10.ts";
@@ -11,3 +12,11 @@ Deno.test("Day 10 - A", async () => {
const result = count_min_button_presses(manual_list); const result = count_min_button_presses(manual_list);
assertEquals(result, 7); 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);
});

View File

@@ -22,11 +22,16 @@ export default async function Factory() {
">> Min button presses to start: ", ">> Min button presses to start: ",
min_presses, 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 { export function count_min_button_presses(manuals: MachineManual[]): number {
let total_sum = 0; let total_sum = 0;
manuals.forEach((manual, m_i) => { manuals.forEach((manual) => {
const wiring_list = manual.button_wirings; const wiring_list = manual.button_wirings;
let min_presses = Infinity; let min_presses = Infinity;
@@ -64,6 +69,52 @@ export function count_min_button_presses(manuals: MachineManual[]): number {
return total_sum; 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( function apply_wiring_to_indicators(
wiring: ButtonWiring, wiring: ButtonWiring,
indicator: Indicator[], 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<T extends unknown[]>( function make_all_combinations_of_size<T extends unknown[]>(
list: T, list: T,
size: number, size: number,
@@ -99,6 +157,25 @@ function make_all_combinations_of_size<T extends unknown[]>(
return result_list; return result_list;
} }
function make_all_combinations_repeating<T extends unknown[]>(
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( export async function read_machine_manuals(
path: string, path: string,
): Promise<MachineManual[]> { ): Promise<MachineManual[]> {