feat(day_7): day 7 A done + B started + 6B cleaned
This commit is contained in:
166
src/exercises/day_7.ts
Normal file
166
src/exercises/day_7.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
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));
|
||||
}
|
||||
|
||||
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);
|
||||
console.log(">> Tree\n", full_tree.map((row) => row.join("")).join("\n"));
|
||||
const init_x = tm[0].findIndex((value) => value === "S");
|
||||
|
||||
return count_quantum_paths(full_tree, init_x, 1);
|
||||
}
|
||||
|
||||
export function count_quantum_paths(
|
||||
tm: TachyonManifold,
|
||||
initial_x: number,
|
||||
initial_y: number,
|
||||
): number {
|
||||
let total_paths = 0;
|
||||
if (initial_y === tm.length - 2) {
|
||||
console.log(">> exit on ", { initial_y, initial_x });
|
||||
return tm[initial_y][initial_x] === "|" ? 1 : 0;
|
||||
}
|
||||
while (tm[initial_y][initial_x]) {
|
||||
const point = tm[initial_y][initial_x];
|
||||
if (point === "^") {
|
||||
console.log(">> split on ", { initial_y, initial_x });
|
||||
total_paths += count_quantum_paths(tm, initial_x + 1, initial_y + 1);
|
||||
total_paths += count_quantum_paths(tm, initial_x - 1, initial_y + 1);
|
||||
}
|
||||
initial_y += 1;
|
||||
}
|
||||
|
||||
return total_paths;
|
||||
}
|
||||
|
||||
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("");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user