feat(day_7): day 7 A done + B started + 6B cleaned
This commit is contained in:
22
src/__tests__/day_7_test.ts
Normal file
22
src/__tests__/day_7_test.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { assertEquals } from "@std/assert";
|
||||
import {
|
||||
count_bean_splits,
|
||||
count_quantum_splits,
|
||||
read_tachyon_manifold,
|
||||
} from "../exercises/day_7.ts";
|
||||
|
||||
Deno.test("Day 7 - A", async () => {
|
||||
const tachyon_manifold = await read_tachyon_manifold(
|
||||
"src/exercises/assets/day_7_test_input.txt",
|
||||
);
|
||||
const split_count = count_bean_splits(tachyon_manifold);
|
||||
assertEquals(split_count, 21);
|
||||
});
|
||||
|
||||
Deno.test("Day 7 - B", async () => {
|
||||
const tachyon_manifold = await read_tachyon_manifold(
|
||||
"src/exercises/assets/day_7_test_input.txt",
|
||||
);
|
||||
const split_count = count_quantum_splits(tachyon_manifold);
|
||||
assertEquals(split_count, 40);
|
||||
});
|
||||
142
src/exercises/assets/day_7_input.txt
Normal file
142
src/exercises/assets/day_7_input.txt
Normal file
@@ -0,0 +1,142 @@
|
||||
......................................................................S......................................................................
|
||||
.............................................................................................................................................
|
||||
......................................................................^......................................................................
|
||||
.............................................................................................................................................
|
||||
.....................................................................^.^.....................................................................
|
||||
.............................................................................................................................................
|
||||
....................................................................^.^.^....................................................................
|
||||
.............................................................................................................................................
|
||||
...................................................................^.^...^...................................................................
|
||||
.............................................................................................................................................
|
||||
..................................................................^...^.^.^..................................................................
|
||||
.............................................................................................................................................
|
||||
.................................................................^.^...^.^.^.................................................................
|
||||
.............................................................................................................................................
|
||||
................................................................^...^.^.^.^.^................................................................
|
||||
.............................................................................................................................................
|
||||
...............................................................^.............^...............................................................
|
||||
.............................................................................................................................................
|
||||
..............................................................^...^.^.^...^.^.^..............................................................
|
||||
.............................................................................................................................................
|
||||
.............................................................^.^.^...^.....^.^.^.............................................................
|
||||
.............................................................................................................................................
|
||||
............................................................^...^.^.^...^.^...^.^............................................................
|
||||
.............................................................................................................................................
|
||||
...........................................................^.^...^.^...^.^.^...^.^...........................................................
|
||||
.............................................................................................................................................
|
||||
..........................................................^.^.^.^...^...^...^.....^..........................................................
|
||||
.............................................................................................................................................
|
||||
.........................................................^.^.^.....^.^.^.^...^.....^.........................................................
|
||||
.............................................................................................................................................
|
||||
........................................................^.^.^.^.^.......^...^.^.^...^........................................................
|
||||
.............................................................................................................................................
|
||||
.......................................................^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.......................................................
|
||||
.............................................................................................................................................
|
||||
......................................................^.....^...^.^.^...........^...^.^......................................................
|
||||
.............................................................................................................................................
|
||||
.....................................................^...^.^.^.^...^.....^.^.^...^...^.^.....................................................
|
||||
.............................................................................................................................................
|
||||
....................................................^.....^.^.^.^.....^.^.^.^.^.^...^.^.^....................................................
|
||||
.............................................................................................................................................
|
||||
...................................................^...^.^.^.^.^.^.^...^.^.^.^...^.^.^.^.^...................................................
|
||||
.............................................................................................................................................
|
||||
..................................................^.^.^.^.^...^.^...^.^...^.^.^.^.^.^...^.^..................................................
|
||||
.............................................................................................................................................
|
||||
.................................................^...^.....^...^.^.^.^...^.....^.^...^.^...^.................................................
|
||||
.............................................................................................................................................
|
||||
................................................^.^.^.....^.^.^.^.....^.^.^.^...^.....^.^.^.^................................................
|
||||
.............................................................................................................................................
|
||||
...............................................^.....^.......^.....^...^.^...^.^.^...^.......^...............................................
|
||||
.............................................................................................................................................
|
||||
..............................................^.^...^...^.^...^...^...^.^...^.^.^.^...^...^.^.^..............................................
|
||||
.............................................................................................................................................
|
||||
.............................................^...^...^.^.........^.^...^.^.....^.^.^.^.^.^...^.^.............................................
|
||||
.............................................................................................................................................
|
||||
............................................^.^.^...^.^.^.^.^...^.^.^...^.^...^.^...^.^.^.^.^.^.^............................................
|
||||
.............................................................................................................................................
|
||||
...........................................^.....^.........^.^.^.^.^.^.^...^.^.^...^.^.^.^...^...^...........................................
|
||||
.............................................................................................................................................
|
||||
..........................................^.^.^.....^.......^.^...^.^.^.^.^.^.....^...^.^.^.^.^...^..........................................
|
||||
.............................................................................................................................................
|
||||
.........................................^.^.^.^.^...^.^.....^.....^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.........................................
|
||||
.............................................................................................................................................
|
||||
........................................^.^.^...^...^.^.^.^.^.^.^.^...^...^...^.^...^.^...^...^.....^........................................
|
||||
.............................................................................................................................................
|
||||
.......................................^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.....^.^.^.^.^.^.^.........^.^.......................................
|
||||
.............................................................................................................................................
|
||||
......................................^.^.^.^.^.^.^.^.^.^.^...^.....^.......^.^.^.^.^.^.^.^...^...^...^......................................
|
||||
.............................................................................................................................................
|
||||
.....................................^.^.^.^.^...^...^.^.......^...^.^.....^...^.^.^.^.^.....^.^.^.^.^.^.....................................
|
||||
.............................................................................................................................................
|
||||
....................................^.....^.^...^...^.^.^.^.^.^...^.^.^...^.^.^.^.....^...^.^.^.^.^.^...^....................................
|
||||
.............................................................................................................................................
|
||||
...................................^.....^.^.........^...^.^.^.^.^.^.^...^.^.^...^.^.^.^.^...^.^...^.^...^...................................
|
||||
.............................................................................................................................................
|
||||
..................................^.^.^.^...^.^.^.^.^...^.^...^.^.^.^.^.....^.^.^.^.^.....^...^.....^.^.^.^..................................
|
||||
.............................................................................................................................................
|
||||
.................................^.^.........^...^.....^.^...^.^...^.^.^...^.^...^...^.^...^.^.^.^.^.....^.^.................................
|
||||
.............................................................................................................................................
|
||||
................................^.^...^.^.^.^.^...^...^...............^.....^.....^.^.^.^.^.^.^.^.^.^.....^.^................................
|
||||
.............................................................................................................................................
|
||||
...............................^.^.^...^.^...^.^.^...^...^.^.....^.^.^...^.^.^.^...^.^.^.^.^.^.^.^.^...^.^.^.^...............................
|
||||
.............................................................................................................................................
|
||||
..............................^...^.^.^.^.^.^.....^.^.^.^.....^.......^.^.^.^.^...^.^.^.^...^...^...^.^.^.....^..............................
|
||||
.............................................................................................................................................
|
||||
.............................^.^.^...^.^.^.^.^.^...^...^.^.^.^.^.^...^.^.^.^.^.......^.^.^.^...^.^.^.^...^.^...^.............................
|
||||
.............................................................................................................................................
|
||||
............................^.....^.^...^.^...^.^.....^.^...^.......^...^.^.....^...^.^...^...^.^.^.....^.^.....^............................
|
||||
.............................................................................................................................................
|
||||
...........................^...^...^...^.^.......^.....^.^.....^.^.^.^.^...^.^.^.^.....^.^.^...^.^.^.....^...^...^...........................
|
||||
.............................................................................................................................................
|
||||
..........................^.....^.^.^.......^.^...^.^.^.^.^.^.^.^.^.^.....^.....^.^...^.^...^.^.^...^...^.^.^.^...^..........................
|
||||
.............................................................................................................................................
|
||||
.........................^.^.^.^.^.^.^.^.^.^...^.....^.^...^.^.^.^.^.^.^.^.^...^.^.^.^.^.^...^.^.^...^.^...^.^.^.^.^.........................
|
||||
.............................................................................................................................................
|
||||
........................^.^.^...^.^.^...^.^...^.........^.^.^...^.^.^.^.....^.^...^.^.^.^.^...^.^.^...^.^.....^.....^........................
|
||||
.............................................................................................................................................
|
||||
.......................^.^.^.^...^.^.....^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.....^.......^.^.^.^...^.^...^.......................
|
||||
.............................................................................................................................................
|
||||
......................^.^...^.^.^.^.....^.^.^.^.^.....^.^...^.^.^.^.^.^.^...^...^.^.^.^...^.....^.^...^.^.^.^.^.^.^.^.^......................
|
||||
.............................................................................................................................................
|
||||
.....................^.^.^.^.^...^.^...^...^.^...^.^...^.^...^.......^.^.^.^.....^.^...^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.....................
|
||||
.............................................................................................................................................
|
||||
....................^...^.^.^.....^.^.^.^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.......^...^.....^.^...^...^.^.....^.^.^...^.^.^....................
|
||||
.............................................................................................................................................
|
||||
...................^.^.^.^.^.^.......^.^.^.......^...^.^.^.^...^.^.^...^.^.^.^...^.^.^.^.....^.^.^.^.^.^...^.^...^.....^.^...................
|
||||
.............................................................................................................................................
|
||||
..................^.^...^.......^.^.^.^.....^.^...^.^...^...^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^...^.^...^.^.^.^.^...^.^.^...^..................
|
||||
.............................................................................................................................................
|
||||
.................^.....^.^...^...^.^.^.^...^.^.^.^...^.......^.^...^...^.^.^.....^.^.^...^.^...^.....^.^...^.^.^.^.^.^.^.^.^.................
|
||||
.............................................................................................................................................
|
||||
................^.^.^.....^.^.^.^.^.^.^.......^.^.^.....^...^.^...^.^.^.^.^...^.^...^.^.^...^...^.^.^.^...^.^.^.^...^.^.....^................
|
||||
.............................................................................................................................................
|
||||
...............^...^.^.^.^.^.^...^.^...^...^.^.^.^.^.^.^...^...^.^...............^...^.^.^.^.....^.....^.^.^.^.....^.^...^.^.^...............
|
||||
.............................................................................................................................................
|
||||
..............^.^...^.^.^.........^.........^...^.^.^.....^.^...^.^.^.^.^.^...^...^.^...^.^.^.^.......^.^...^.....^.^.^.^.^.^.^..............
|
||||
.............................................................................................................................................
|
||||
.............^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^.^.^.^.....^.^.^.^...^.^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.............
|
||||
.............................................................................................................................................
|
||||
............^.^.^.^...^.^.^.^.^...^...^.....^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^.^.^...^...^.....^.^...^.^.^.^.^.......^...^............
|
||||
.............................................................................................................................................
|
||||
...........^.....^.^.^.^.....^.^...^.^.^.^.^.....^.^.^...^.^.^...^.^.....^.......^.^.^.....^.^.^.^...^.^.^.^.^.^.^.^.^.^...^...^.^...........
|
||||
.............................................................................................................................................
|
||||
..........^.^.^.^.........^.^.....^.......^.^...^.^.^.......^.......^.^.^.^.....^.^...^.^...^.^.^.^...^...^.^.^.^.^.^.^...^.^.^.^.^..........
|
||||
.............................................................................................................................................
|
||||
.........^.^.^...^.^.........^...^.^...^.^.^.^.^.^.^.^.....^.^.^.^...^.^.^...^...^...^.^.^.^.^.^.^.^.^.^.^...^.^...^.^...^.^.^...^.^.........
|
||||
.............................................................................................................................................
|
||||
........^.^.^.^.^...^.^...^.^.^.^...^.^.^.^.^.....^...^...^.^...^.^...^.^.^.^...^...^.^...^.......^.^.^.^...^.....^...^.^.^.....^.^.^........
|
||||
.............................................................................................................................................
|
||||
.......^.^.^...^.^.^.....^.^.^.^.....^.....^.^...^.^.^.....^.^...^...........^...^.^.^.^.....^...^.^.^...^.^.^.^.......^.^.^.^.^.....^.......
|
||||
.............................................................................................................................................
|
||||
......^.^...^.....^.^...^.....^...^...^.^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.....^.^.....^.^.^.^.^...^...^.^......
|
||||
.............................................................................................................................................
|
||||
.....^.^.^.^.^.^...^.^.......^.^.^...^...^.^.^.^.^.^.^.....^.^.....^...^.....^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^...^.^.^...^.^.....
|
||||
.............................................................................................................................................
|
||||
....^.^.^.....^.^...^.^...^.^.......^...^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.....^.^...^...^.^.^.^.^.^.^....
|
||||
.............................................................................................................................................
|
||||
...^.^.......^.^.....^.^.^.^.^...^.^...^.^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.^.....^.^.^...^.^.^...^...^.^.^...^...^.^.^.^...^.^...^.^...^.^...
|
||||
.............................................................................................................................................
|
||||
..^.....^.^.^...^.........^.....^...^.^.^...^.^.....^...^.^.^.^.^.^...^.........^...^.^...^...^.^...^...^.......^.^.....^.^.......^.^.^...^..
|
||||
.............................................................................................................................................
|
||||
.^.^...^.....^.^.^.^...^.^.....^.^.^.^.^.^.^.^...^...^.^.......^.^...^.^.....^.^.^...^.....^.^.^.^.^.^.......^.^.^...^.^.^.^.^...^...^.^.^.^.
|
||||
.............................................................................................................................................
|
||||
16
src/exercises/assets/day_7_test_input.txt
Normal file
16
src/exercises/assets/day_7_test_input.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
.......S.......
|
||||
...............
|
||||
.......^.......
|
||||
...............
|
||||
......^.^......
|
||||
...............
|
||||
.....^.^.^.....
|
||||
...............
|
||||
....^.^...^....
|
||||
...............
|
||||
...^.^...^.^...
|
||||
...............
|
||||
..^...^.....^..
|
||||
...............
|
||||
.^.^.^.^.^...^.
|
||||
...............
|
||||
@@ -96,8 +96,7 @@ export async function read_problems_as_cephalopod(
|
||||
const problem_text = await Deno.readTextFile(path);
|
||||
|
||||
// FIXME: this is really the dirtiest shit I've ever seen
|
||||
const spaces: string[] = [];
|
||||
spaces.fill("0", 0, 400);
|
||||
const spaces: string[] = new Array(100).fill("0");
|
||||
const rows = problem_text.split("\n").map((row) =>
|
||||
`${row}${spaces.join("")}`
|
||||
);
|
||||
@@ -116,17 +115,15 @@ export async function read_problems_as_cephalopod(
|
||||
while (!found_spaces.has(next_space)) {
|
||||
found_spaces.add(next_space);
|
||||
|
||||
let next_char = "";
|
||||
if (rows.every((r) => r[next_space] === " " || r[next_space] === "|")) {
|
||||
const extend = row.split("")[row.length - 1] === " ";
|
||||
const new_str = row.split("");
|
||||
new_str[next_space] = "|";
|
||||
row = `${new_str.join("")}${extend ? "-" : ""}`;
|
||||
next_char = "|";
|
||||
} else {
|
||||
const extend = row.split("")[row.length - 1] === " ";
|
||||
const new_str = row.split("");
|
||||
new_str[next_space] = "-";
|
||||
row = `${new_str.join("")}${extend ? "-" : ""}`;
|
||||
next_char = "-";
|
||||
}
|
||||
const new_str = row.split("");
|
||||
new_str[next_space] = next_char;
|
||||
row = new_str.join("");
|
||||
next_space = row.indexOf(" ");
|
||||
}
|
||||
return row;
|
||||
|
||||
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