feat(day_10): part B done fortest input but to slow
This commit is contained in:
12
README.md
12
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.
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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<T extends unknown[]>(
|
||||
list: T,
|
||||
size: number,
|
||||
@@ -99,6 +157,25 @@ function make_all_combinations_of_size<T extends unknown[]>(
|
||||
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(
|
||||
path: string,
|
||||
): Promise<MachineManual[]> {
|
||||
|
||||
Reference in New Issue
Block a user