feat(day_10): part A done by fixing a performace issue on the file-read
This commit is contained in:
@@ -14,7 +14,7 @@ interface MachineManual {
|
|||||||
|
|
||||||
export default async function Factory() {
|
export default async function Factory() {
|
||||||
console.log("== Factory ==");
|
console.log("== Factory ==");
|
||||||
const manual_list = await read_machine_manuals(
|
const manual_list = await read_machine_manuals_quick(
|
||||||
"src/exercises/assets/day_10_input.txt",
|
"src/exercises/assets/day_10_input.txt",
|
||||||
);
|
);
|
||||||
const min_presses = count_min_button_presses(manual_list);
|
const min_presses = count_min_button_presses(manual_list);
|
||||||
@@ -27,7 +27,6 @@ export default async function Factory() {
|
|||||||
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, m_i) => {
|
||||||
console.log(`== Manual ${m_i} out of ${manuals.length}`);
|
|
||||||
const wiring_list = manual.button_wirings;
|
const wiring_list = manual.button_wirings;
|
||||||
|
|
||||||
let min_presses = Infinity;
|
let min_presses = Infinity;
|
||||||
@@ -107,8 +106,7 @@ export async function read_machine_manuals(
|
|||||||
const input_txt = await Deno.readTextFile(path);
|
const input_txt = await Deno.readTextFile(path);
|
||||||
const lines = input_txt.split("\n");
|
const lines = input_txt.split("\n");
|
||||||
|
|
||||||
const manual_list: MachineManual[] = lines.map((line, line_i) => {
|
const manual_list: MachineManual[] = lines.map((line) => {
|
||||||
console.log(`== Reading file ${line_i} out of ${lines.length}`);
|
|
||||||
const matched_line = line.matchAll(line_regex)?.toArray()?.[0];
|
const matched_line = line.matchAll(line_regex)?.toArray()?.[0];
|
||||||
if (matched_line && matched_line.length > 3) {
|
if (matched_line && matched_line.length > 3) {
|
||||||
const indicators = matched_line[1].toString().split("").map((ch) =>
|
const indicators = matched_line[1].toString().split("").map((ch) =>
|
||||||
@@ -131,3 +129,46 @@ export async function read_machine_manuals(
|
|||||||
|
|
||||||
return manual_list;
|
return manual_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function read_machine_manuals_quick(
|
||||||
|
path: string,
|
||||||
|
): Promise<MachineManual[]> {
|
||||||
|
const input_txt = await Deno.readTextFile(path);
|
||||||
|
const lines = input_txt.split("\n");
|
||||||
|
|
||||||
|
const manual_list: MachineManual[] = [];
|
||||||
|
|
||||||
|
lines.forEach((line, line_i) => {
|
||||||
|
const manual: MachineManual = {
|
||||||
|
indicators: [],
|
||||||
|
button_wirings: [],
|
||||||
|
joltage_requirements: [],
|
||||||
|
};
|
||||||
|
line.split(" ").forEach((element) => {
|
||||||
|
if (element.includes("[")) {
|
||||||
|
manual.indicators = element.replaceAll("[", "").replaceAll("]", "")
|
||||||
|
.split("").map((e) => e as Indicator);
|
||||||
|
} else if (element.includes("{")) {
|
||||||
|
manual.joltage_requirements = element.replaceAll("{", "").replaceAll(
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
).split(",").map((e) => parseInt(e));
|
||||||
|
} else if (element.includes("(")) {
|
||||||
|
manual.button_wirings.push(
|
||||||
|
element.replaceAll("(", "").replaceAll(")", "").split(",").map((e) =>
|
||||||
|
parseInt(e)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
manual.button_wirings.length > 0 &&
|
||||||
|
manual.joltage_requirements.length > 0 && manual.indicators.length > 0
|
||||||
|
) {
|
||||||
|
manual_list.push(manual);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return manual_list;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user