diff --git a/README.md b/README.md index 1ef434a..2cec496 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,5 @@ | 3 | Lobby | ⭐ | ⭐ | | 4 | Printing Department | ⭐ | ⭐ | | 5 | Cafeteria | ⭐ | ⭐ | +| 6 | Trash Compactor | ⭐ | ⭐ | +| 7 | Laboratories | ⭐ | | diff --git a/src/__tests__/day_7_test.ts b/src/__tests__/day_7_test.ts new file mode 100644 index 0000000..e1d90d1 --- /dev/null +++ b/src/__tests__/day_7_test.ts @@ -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); +}); diff --git a/src/exercises/assets/day_7_input.txt b/src/exercises/assets/day_7_input.txt new file mode 100644 index 0000000..4edbf16 --- /dev/null +++ b/src/exercises/assets/day_7_input.txt @@ -0,0 +1,142 @@ +......................................................................S...................................................................... +............................................................................................................................................. +......................................................................^...................................................................... +............................................................................................................................................. +.....................................................................^.^..................................................................... +............................................................................................................................................. +....................................................................^.^.^.................................................................... +............................................................................................................................................. +...................................................................^.^...^................................................................... +............................................................................................................................................. +..................................................................^...^.^.^.................................................................. +............................................................................................................................................. +.................................................................^.^...^.^.^................................................................. +............................................................................................................................................. +................................................................^...^.^.^.^.^................................................................ +............................................................................................................................................. +...............................................................^.............^............................................................... +............................................................................................................................................. +..............................................................^...^.^.^...^.^.^.............................................................. +............................................................................................................................................. +.............................................................^.^.^...^.....^.^.^............................................................. +............................................................................................................................................. +............................................................^...^.^.^...^.^...^.^............................................................ +............................................................................................................................................. +...........................................................^.^...^.^...^.^.^...^.^........................................................... +............................................................................................................................................. +..........................................................^.^.^.^...^...^...^.....^.......................................................... +............................................................................................................................................. +.........................................................^.^.^.....^.^.^.^...^.....^......................................................... +............................................................................................................................................. +........................................................^.^.^.^.^.......^...^.^.^...^........................................................ +............................................................................................................................................. +.......................................................^.^.^.^.^.^.^.^...^.^.^.^.^.^.^....................................................... +............................................................................................................................................. +......................................................^.....^...^.^.^...........^...^.^...................................................... +............................................................................................................................................. +.....................................................^...^.^.^.^...^.....^.^.^...^...^.^..................................................... +............................................................................................................................................. +....................................................^.....^.^.^.^.....^.^.^.^.^.^...^.^.^.................................................... +............................................................................................................................................. +...................................................^...^.^.^.^.^.^.^...^.^.^.^...^.^.^.^.^................................................... +............................................................................................................................................. +..................................................^.^.^.^.^...^.^...^.^...^.^.^.^.^.^...^.^.................................................. +............................................................................................................................................. +.................................................^...^.....^...^.^.^.^...^.....^.^...^.^...^................................................. +............................................................................................................................................. +................................................^.^.^.....^.^.^.^.....^.^.^.^...^.....^.^.^.^................................................ +............................................................................................................................................. +...............................................^.....^.......^.....^...^.^...^.^.^...^.......^............................................... +............................................................................................................................................. +..............................................^.^...^...^.^...^...^...^.^...^.^.^.^...^...^.^.^.............................................. +............................................................................................................................................. +.............................................^...^...^.^.........^.^...^.^.....^.^.^.^.^.^...^.^............................................. +............................................................................................................................................. +............................................^.^.^...^.^.^.^.^...^.^.^...^.^...^.^...^.^.^.^.^.^.^............................................ +............................................................................................................................................. +...........................................^.....^.........^.^.^.^.^.^.^...^.^.^...^.^.^.^...^...^........................................... +............................................................................................................................................. +..........................................^.^.^.....^.......^.^...^.^.^.^.^.^.....^...^.^.^.^.^...^.......................................... +............................................................................................................................................. +.........................................^.^.^.^.^...^.^.....^.....^.^.^.^.^.^.^...^...^.^.^.^.^.^.^......................................... +............................................................................................................................................. +........................................^.^.^...^...^.^.^.^.^.^.^.^...^...^...^.^...^.^...^...^.....^........................................ +............................................................................................................................................. +.......................................^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.....^.^.^.^.^.^.^.........^.^....................................... +............................................................................................................................................. +......................................^.^.^.^.^.^.^.^.^.^.^...^.....^.......^.^.^.^.^.^.^.^...^...^...^...................................... +............................................................................................................................................. +.....................................^.^.^.^.^...^...^.^.......^...^.^.....^...^.^.^.^.^.....^.^.^.^.^.^..................................... +............................................................................................................................................. +....................................^.....^.^...^...^.^.^.^.^.^...^.^.^...^.^.^.^.....^...^.^.^.^.^.^...^.................................... +............................................................................................................................................. +...................................^.....^.^.........^...^.^.^.^.^.^.^...^.^.^...^.^.^.^.^...^.^...^.^...^................................... +............................................................................................................................................. +..................................^.^.^.^...^.^.^.^.^...^.^...^.^.^.^.^.....^.^.^.^.^.....^...^.....^.^.^.^.................................. +............................................................................................................................................. +.................................^.^.........^...^.....^.^...^.^...^.^.^...^.^...^...^.^...^.^.^.^.^.....^.^................................. +............................................................................................................................................. +................................^.^...^.^.^.^.^...^...^...............^.....^.....^.^.^.^.^.^.^.^.^.^.....^.^................................ +............................................................................................................................................. +...............................^.^.^...^.^...^.^.^...^...^.^.....^.^.^...^.^.^.^...^.^.^.^.^.^.^.^.^...^.^.^.^............................... +............................................................................................................................................. +..............................^...^.^.^.^.^.^.....^.^.^.^.....^.......^.^.^.^.^...^.^.^.^...^...^...^.^.^.....^.............................. +............................................................................................................................................. +.............................^.^.^...^.^.^.^.^.^...^...^.^.^.^.^.^...^.^.^.^.^.......^.^.^.^...^.^.^.^...^.^...^............................. +............................................................................................................................................. +............................^.....^.^...^.^...^.^.....^.^...^.......^...^.^.....^...^.^...^...^.^.^.....^.^.....^............................ +............................................................................................................................................. +...........................^...^...^...^.^.......^.....^.^.....^.^.^.^.^...^.^.^.^.....^.^.^...^.^.^.....^...^...^........................... +............................................................................................................................................. +..........................^.....^.^.^.......^.^...^.^.^.^.^.^.^.^.^.^.....^.....^.^...^.^...^.^.^...^...^.^.^.^...^.......................... +............................................................................................................................................. +.........................^.^.^.^.^.^.^.^.^.^...^.....^.^...^.^.^.^.^.^.^.^.^...^.^.^.^.^.^...^.^.^...^.^...^.^.^.^.^......................... +............................................................................................................................................. +........................^.^.^...^.^.^...^.^...^.........^.^.^...^.^.^.^.....^.^...^.^.^.^.^...^.^.^...^.^.....^.....^........................ +............................................................................................................................................. +.......................^.^.^.^...^.^.....^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.....^.......^.^.^.^...^.^...^....................... +............................................................................................................................................. +......................^.^...^.^.^.^.....^.^.^.^.^.....^.^...^.^.^.^.^.^.^...^...^.^.^.^...^.....^.^...^.^.^.^.^.^.^.^.^...................... +............................................................................................................................................. +.....................^.^.^.^.^...^.^...^...^.^...^.^...^.^...^.......^.^.^.^.....^.^...^.^.^.^...^.^.^.^.^.^.^.^...^.^.^..................... +............................................................................................................................................. +....................^...^.^.^.....^.^.^.^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.......^...^.....^.^...^...^.^.....^.^.^...^.^.^.................... +............................................................................................................................................. +...................^.^.^.^.^.^.......^.^.^.......^...^.^.^.^...^.^.^...^.^.^.^...^.^.^.^.....^.^.^.^.^.^...^.^...^.....^.^................... +............................................................................................................................................. +..................^.^...^.......^.^.^.^.....^.^...^.^...^...^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^...^.^...^.^.^.^.^...^.^.^...^.................. +............................................................................................................................................. +.................^.....^.^...^...^.^.^.^...^.^.^.^...^.......^.^...^...^.^.^.....^.^.^...^.^...^.....^.^...^.^.^.^.^.^.^.^.^................. +............................................................................................................................................. +................^.^.^.....^.^.^.^.^.^.^.......^.^.^.....^...^.^...^.^.^.^.^...^.^...^.^.^...^...^.^.^.^...^.^.^.^...^.^.....^................ +............................................................................................................................................. +...............^...^.^.^.^.^.^...^.^...^...^.^.^.^.^.^.^...^...^.^...............^...^.^.^.^.....^.....^.^.^.^.....^.^...^.^.^............... +............................................................................................................................................. +..............^.^...^.^.^.........^.........^...^.^.^.....^.^...^.^.^.^.^.^...^...^.^...^.^.^.^.......^.^...^.....^.^.^.^.^.^.^.............. +............................................................................................................................................. +.............^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^.^.^.^.....^.^.^.^...^.^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^............. +............................................................................................................................................. +............^.^.^.^...^.^.^.^.^...^...^.....^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^.^.^...^...^.....^.^...^.^.^.^.^.......^...^............ +............................................................................................................................................. +...........^.....^.^.^.^.....^.^...^.^.^.^.^.....^.^.^...^.^.^...^.^.....^.......^.^.^.....^.^.^.^...^.^.^.^.^.^.^.^.^.^...^...^.^........... +............................................................................................................................................. +..........^.^.^.^.........^.^.....^.......^.^...^.^.^.......^.......^.^.^.^.....^.^...^.^...^.^.^.^...^...^.^.^.^.^.^.^...^.^.^.^.^.......... +............................................................................................................................................. +.........^.^.^...^.^.........^...^.^...^.^.^.^.^.^.^.^.....^.^.^.^...^.^.^...^...^...^.^.^.^.^.^.^.^.^.^.^...^.^...^.^...^.^.^...^.^......... +............................................................................................................................................. +........^.^.^.^.^...^.^...^.^.^.^...^.^.^.^.^.....^...^...^.^...^.^...^.^.^.^...^...^.^...^.......^.^.^.^...^.....^...^.^.^.....^.^.^........ +............................................................................................................................................. +.......^.^.^...^.^.^.....^.^.^.^.....^.....^.^...^.^.^.....^.^...^...........^...^.^.^.^.....^...^.^.^...^.^.^.^.......^.^.^.^.^.....^....... +............................................................................................................................................. +......^.^...^.....^.^...^.....^...^...^.^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.....^.^.....^.^.^.^.^...^...^.^...... +............................................................................................................................................. +.....^.^.^.^.^.^...^.^.......^.^.^...^...^.^.^.^.^.^.^.....^.^.....^...^.....^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^...^.^.^...^.^..... +............................................................................................................................................. +....^.^.^.....^.^...^.^...^.^.......^...^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.....^.^...^...^.^.^.^.^.^.^.... +............................................................................................................................................. +...^.^.......^.^.....^.^.^.^.^...^.^...^.^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.^.....^.^.^...^.^.^...^...^.^.^...^...^.^.^.^...^.^...^.^...^.^... +............................................................................................................................................. +..^.....^.^.^...^.........^.....^...^.^.^...^.^.....^...^.^.^.^.^.^...^.........^...^.^...^...^.^...^...^.......^.^.....^.^.......^.^.^...^.. +............................................................................................................................................. +.^.^...^.....^.^.^.^...^.^.....^.^.^.^.^.^.^.^...^...^.^.......^.^...^.^.....^.^.^...^.....^.^.^.^.^.^.......^.^.^...^.^.^.^.^...^...^.^.^.^. +............................................................................................................................................. diff --git a/src/exercises/assets/day_7_test_input.txt b/src/exercises/assets/day_7_test_input.txt new file mode 100644 index 0000000..57a2466 --- /dev/null +++ b/src/exercises/assets/day_7_test_input.txt @@ -0,0 +1,16 @@ +.......S....... +............... +.......^....... +............... +......^.^...... +............... +.....^.^.^..... +............... +....^.^...^.... +............... +...^.^...^.^... +............... +..^...^.....^.. +............... +.^.^.^.^.^...^. +............... diff --git a/src/exercises/day_6.ts b/src/exercises/day_6.ts index 376780b..01a9765 100644 --- a/src/exercises/day_6.ts +++ b/src/exercises/day_6.ts @@ -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; diff --git a/src/exercises/day_7.ts b/src/exercises/day_7.ts new file mode 100644 index 0000000..9ee24b2 --- /dev/null +++ b/src/exercises/day_7.ts @@ -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 { + const input_txt = await Deno.readTextFile(path); + const input_rows = input_txt.split("\n"); + return input_rows.map((row) => { + return row.split(""); + }); +}