chore: day_10 A done but file read to slow WIP

This commit is contained in:
2025-12-14 23:39:10 +01:00
parent 5969abf458
commit ecfe1aad35
3 changed files with 237 additions and 75 deletions

View File

@@ -13,94 +13,52 @@ interface MachineManual {
}
export default async function Factory() {
console.log("== Factory ==");
const manual_list = await read_machine_manuals(
"src/exercises/assets/day_10_input.txt",
);
const min_presses = count_min_button_presses(manual_list);
console.log(
">> Min button presses to start: ",
min_presses,
);
}
export function count_min_button_presses(manuals: MachineManual[]): number {
let total_sum = 0;
manuals.forEach((manual, i) => {
manuals.forEach((manual, m_i) => {
console.log(`== Manual ${m_i} out of ${manuals.length}`);
const wiring_list = manual.button_wirings;
let min_presses = Infinity;
wiring_list.forEach((wiring, wiring_i) => {
let pairing_indicator = manual.indicators.map((_) => Indicator.OFF);
// Apply the current wiring
pairing_indicator = apply_wiring_to_indicators(
wiring,
pairing_indicator,
for (let size = 1; size < wiring_list.length; size++) {
const button_combinations = make_all_combinations_of_size(
wiring_list,
size,
);
if (pairing_indicator.join("") === manual.indicators.join("")) {
min_presses = 1;
return;
}
for (
let inner_i = wiring_i + 1;
inner_i < wiring_list.length;
inner_i++
) {
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,
button_combinations.forEach((combination) => {
// Apply combination
let pairing_indicator = manual.indicators.map((_) => Indicator.OFF);
combination.forEach((b) => {
pairing_indicator = apply_wiring_to_indicators(
b,
pairing_indicator,
);
if (cumulative_indicator.join("") === manual.indicators.join("")) {
break;
}
}
});
if (cumulative_presses < min_presses) {
min_presses = cumulative_presses;
// Check if valid
if (pairing_indicator.join("") === manual.indicators.join("")) {
min_presses = combination.length;
return;
}
});
if (min_presses < Infinity) {
break;
}
}
console.log(">> Total and min", { total_sum, min_presses });
total_sum += min_presses;
});
@@ -116,6 +74,32 @@ function apply_wiring_to_indicators(
);
}
function make_all_combinations_of_size<T extends unknown[]>(
list: T,
size: number,
): T[] {
const binary_cap = Math.pow(2, list.length);
const result_list: T[] = [];
for (let i = 0; i < binary_cap; i++) {
const combination = i.toString(2).padStart(list.length, "0").split("");
if (
combination.reduce((sum, n) => sum + (n === "1" ? 1 : 0), 0) !== size
) {
continue;
}
const next_l = list.map((l, i) => combination[i] === "1" ? l : null).filter(
(l) => l !== null,
);
result_list.push(next_l as T);
}
return result_list;
}
export async function read_machine_manuals(
path: string,
): Promise<MachineManual[]> {
@@ -123,7 +107,8 @@ export async function read_machine_manuals(
const input_txt = await Deno.readTextFile(path);
const lines = input_txt.split("\n");
const manual_list: MachineManual[] = lines.map((line) => {
const manual_list: MachineManual[] = lines.map((line, line_i) => {
console.log(`== Reading file ${line_i} out of ${lines.length}`);
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) =>