239 lines
5.9 KiB
TypeScript
239 lines
5.9 KiB
TypeScript
type TachyonManifold = string[][];
|
|
|
|
export default async function Laboratories() {
|
|
const tachyon_manifold = await read_tachyon_manifold(
|
|
"src/exercises/assets/day_7_input.txt",
|
|
);
|
|
console.log(">> Bean splits: ", count_bean_splits(tachyon_manifold));
|
|
const total_splits = count_quantum_splits(tachyon_manifold);
|
|
console.log(">> Cuantum splits: ", total_splits);
|
|
}
|
|
|
|
export function count_bean_splits(tm: TachyonManifold): number {
|
|
let split_count = 0;
|
|
|
|
tm.forEach((row, y) => {
|
|
row.forEach((point, x) => {
|
|
switch (point) {
|
|
case ".": {
|
|
const prev_point = tm[y - 1]?.[x];
|
|
if (prev_point === "S" || prev_point === "|") {
|
|
tm[y][x] = "|";
|
|
}
|
|
break;
|
|
}
|
|
case "^": {
|
|
const prev_point = tm[y - 1][x];
|
|
if (prev_point === "|") {
|
|
split_count += 1;
|
|
const left_point = tm[y][x - 1];
|
|
if (left_point === ".") {
|
|
tm[y][x - 1] = "|";
|
|
}
|
|
const right_point = tm[y][x + 1];
|
|
if (right_point === ".") {
|
|
tm[y][x + 1] = "|";
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
return split_count;
|
|
}
|
|
|
|
export function count_quantum_splits(tm: TachyonManifold): number {
|
|
const full_tree = generate_full_quantum_tree(tm);
|
|
|
|
// Downwards
|
|
const init = full_tree[0].findIndex((p) => p === "S");
|
|
return count_quantum_paths_downwards(full_tree, init, 1);
|
|
|
|
// Upwards
|
|
// let total = 0;
|
|
// for (let i = 0; i < tm[0].length; i++) {
|
|
// if (tm[tm.length - 2][i] === "|") {
|
|
// total += count_quantum_paths_upwards(full_tree, i, tm.length - 2) + 1;
|
|
// }
|
|
// }
|
|
// return total;
|
|
}
|
|
|
|
function count_quantum_paths_downwards(
|
|
tm: TachyonManifold,
|
|
initial_x: number,
|
|
initial_y: number,
|
|
): number {
|
|
let total = 0;
|
|
const FILE_END = tm.length - 2;
|
|
const current = tm[initial_y][initial_x];
|
|
|
|
if (initial_y > FILE_END) {
|
|
return 0;
|
|
}
|
|
if (initial_y === FILE_END && current === "|") {
|
|
return 1;
|
|
} else if (initial_y === FILE_END) {
|
|
return 0;
|
|
}
|
|
|
|
const next = tm[initial_y + 1][initial_x];
|
|
switch (next) {
|
|
case "|": {
|
|
total += count_quantum_paths_downwards(tm, initial_x, initial_y + 1);
|
|
break;
|
|
}
|
|
case "^": {
|
|
total += count_quantum_paths_downwards(tm, initial_x - 1, initial_y + 1);
|
|
total += count_quantum_paths_downwards(tm, initial_x + 1, initial_y + 1);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return total;
|
|
}
|
|
|
|
export function count_quantum_paths_upwards(
|
|
tm: TachyonManifold,
|
|
initial_x: number,
|
|
initial_y: number,
|
|
): number {
|
|
if (initial_y === 0) {
|
|
return 0;
|
|
}
|
|
|
|
let total = 0;
|
|
|
|
let current_bean = tm[initial_y][initial_x];
|
|
while (current_bean !== "." && initial_y > 2) {
|
|
switch (current_bean) {
|
|
case "|": {
|
|
const left_point = tm[initial_y][initial_x - 1];
|
|
const right_point = tm[initial_y][initial_y + 1];
|
|
// WRONG: the splitter existing does not mean that it has a bean
|
|
if (left_point === "^" && right_point === "^") {
|
|
total += 1;
|
|
}
|
|
if (left_point === "^") {
|
|
total += count_quantum_paths_upwards(
|
|
tm,
|
|
initial_x - 1,
|
|
initial_y - 1,
|
|
);
|
|
}
|
|
if (right_point === "^") {
|
|
total += count_quantum_paths_upwards(
|
|
tm,
|
|
initial_x + 1,
|
|
initial_y - 1,
|
|
);
|
|
}
|
|
|
|
if (left_point === "^" || right_point === "^") {
|
|
const contiues_upwards = tm[initial_y - 1][initial_x] === "|";
|
|
if (contiues_upwards) {
|
|
total += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
initial_y--;
|
|
current_bean = tm[initial_y][initial_x];
|
|
}
|
|
return total;
|
|
}
|
|
|
|
export function generate_full_quantum_tree(
|
|
tm: TachyonManifold,
|
|
): TachyonManifold {
|
|
tm.forEach((row, y) => {
|
|
row.forEach((point, x) => {
|
|
switch (point) {
|
|
case ".": {
|
|
const prev_point = tm[y - 1]?.[x];
|
|
if (prev_point === "S" || prev_point === "|") {
|
|
tm[y][x] = "|";
|
|
}
|
|
break;
|
|
}
|
|
case "^": {
|
|
const prev_point = tm[y - 1][x];
|
|
if (prev_point === "|") {
|
|
const left_point = tm[y][x - 1];
|
|
if (left_point === ".") {
|
|
tm[y][x - 1] = "|";
|
|
}
|
|
const right_point = tm[y][x + 1];
|
|
if (right_point === ".") {
|
|
tm[y][x + 1] = "|";
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
return tm;
|
|
}
|
|
|
|
/*
|
|
* @deprecated since its to inefficient
|
|
*/
|
|
function generate_quantum_split(
|
|
tachyonManifold: TachyonManifold,
|
|
): TachyonManifold[] {
|
|
const tm = tachyonManifold.map((a) => a.slice());
|
|
|
|
const quantum_tms: TachyonManifold[] = [];
|
|
|
|
tm.forEach((row, y) => {
|
|
row.forEach((point, x) => {
|
|
switch (point) {
|
|
case ".": {
|
|
const prev_point = tm[y - 1]?.[x];
|
|
if (prev_point === "S" || prev_point === "|") {
|
|
tm[y][x] = "|";
|
|
}
|
|
break;
|
|
}
|
|
case "^": {
|
|
const prev_point = tm[y - 1][x];
|
|
if (prev_point === "|") {
|
|
const left_point = tm[y][x - 1];
|
|
if (left_point === ".") {
|
|
const left_tm = tm.map((a) => a.slice());
|
|
left_tm[y][x - 1] = "|";
|
|
quantum_tms.push(...generate_quantum_split(left_tm));
|
|
}
|
|
// Right
|
|
const right_point = tm[y][x + 1];
|
|
if (right_point === ".") {
|
|
const right_tm = tm.map((a) => a.slice());
|
|
tm[y][x + 1] = "|";
|
|
quantum_tms.push(...generate_quantum_split(right_tm));
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case "S":
|
|
break;
|
|
}
|
|
});
|
|
});
|
|
|
|
return quantum_tms;
|
|
}
|
|
|
|
export async function read_tachyon_manifold(
|
|
path: string,
|
|
): Promise<TachyonManifold> {
|
|
const input_txt = await Deno.readTextFile(path);
|
|
const input_rows = input_txt.split("\n");
|
|
return input_rows.map((row) => {
|
|
return row.split("");
|
|
});
|
|
}
|