feat(day_10): file read and base done
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
| 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 | | |
|
||||||
|
|||||||
13
src/__tests__/day_10_test.ts
Normal file
13
src/__tests__/day_10_test.ts
Normal file
@@ -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);
|
||||||
|
});
|
||||||
3
src/exercises/assets/day_10_test_input.txt
Normal file
3
src/exercises/assets/day_10_test_input.txt
Normal file
@@ -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}
|
||||||
87
src/exercises/day_10.ts
Normal file
87
src/exercises/day_10.ts
Normal file
@@ -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<MachineManual[]> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user