chore: current WIP day 7b and 8
This commit is contained in:
13
src/__tests__/day_8_test.ts
Normal file
13
src/__tests__/day_8_test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { assertEquals } from "@std/assert";
|
||||
import {
|
||||
get_three_largest_pairings_mul,
|
||||
read_junction_box_3d_positions,
|
||||
} from "../exercises/day_8.ts";
|
||||
|
||||
Deno.test("Day 8 - A", async () => {
|
||||
const junction_boxes_coords = await read_junction_box_3d_positions(
|
||||
"src/exercises/assets/day_8_test_input.txt",
|
||||
);
|
||||
const result = get_three_largest_pairings_mul(junction_boxes_coords, 10);
|
||||
assertEquals(result, 40);
|
||||
});
|
||||
1000
src/exercises/assets/day_8_input.txt
Normal file
1000
src/exercises/assets/day_8_input.txt
Normal file
File diff suppressed because it is too large
Load Diff
20
src/exercises/assets/day_8_test_input.txt
Normal file
20
src/exercises/assets/day_8_test_input.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
162,817,812
|
||||
57,618,57
|
||||
906,360,560
|
||||
592,479,940
|
||||
352,342,300
|
||||
466,668,158
|
||||
542,29,236
|
||||
431,825,988
|
||||
739,650,466
|
||||
52,470,668
|
||||
216,146,977
|
||||
819,987,18
|
||||
117,168,530
|
||||
805,96,715
|
||||
346,949,466
|
||||
970,615,88
|
||||
941,993,340
|
||||
862,61,35
|
||||
984,92,344
|
||||
425,690,689
|
||||
@@ -44,33 +44,70 @@ export function count_bean_splits(tm: TachyonManifold): number {
|
||||
|
||||
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");
|
||||
console.log(
|
||||
">> Full tree\n",
|
||||
full_tree.map((row) => row.join("")).join("\n"),
|
||||
);
|
||||
let total = 0;
|
||||
|
||||
return count_quantum_paths(full_tree, init_x, 1);
|
||||
console.log("==", tm[tm.length - 2]);
|
||||
for (let i = 0; i < tm[0].length; i++) {
|
||||
if (tm[tm.length - 2][i] === "|") {
|
||||
total += count_quantum_paths_upwards(tm, i, tm.length - 2) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
export function count_quantum_paths(
|
||||
export function count_quantum_paths_upwards(
|
||||
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;
|
||||
if (initial_y === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return total_paths;
|
||||
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(
|
||||
|
||||
189
src/exercises/day_8.ts
Normal file
189
src/exercises/day_8.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
interface Coordinate {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
}
|
||||
|
||||
export default async function Playground() {
|
||||
const junction_boxes_coords = await read_junction_box_3d_positions(
|
||||
"src/exercises/assets/day_8_input.txt",
|
||||
);
|
||||
console.log(
|
||||
">> Three largest pairings mul: ",
|
||||
get_three_largest_pairings_mul(junction_boxes_coords, 1000),
|
||||
);
|
||||
}
|
||||
|
||||
export function get_three_largest_pairings_mul(
|
||||
box_list: Coordinate[],
|
||||
max_pairings: number,
|
||||
): number {
|
||||
const pairing_list = generate_n_shortest_pairings(box_list, max_pairings)
|
||||
.sort((a, b) => b.length - a.length);
|
||||
let total = 1;
|
||||
for (let i = 0; i < 3; i++) {
|
||||
total *= pairing_list[i].length;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
function generate_n_shortest_distances(
|
||||
box_list: Coordinate[],
|
||||
n: number,
|
||||
): { a: number; b: number; distance: number }[] {
|
||||
const distances: { a: number; b: number; distance: number }[] = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
const shortest_distance: { distance: number; pair_index: number[] } = {
|
||||
distance: Infinity, // Dirty, but it's late
|
||||
pair_index: [],
|
||||
};
|
||||
box_list.forEach((box, box_index) => {
|
||||
for (let next_i = box_index + 1; next_i < box_list.length; next_i++) {
|
||||
const next_box = box_list[next_i];
|
||||
const next_distance = calc_distance(box, next_box);
|
||||
|
||||
if (
|
||||
next_distance < shortest_distance.distance &&
|
||||
!distances.some((d) => d.a === box_index && d.b === next_i)
|
||||
) {
|
||||
shortest_distance.distance = next_distance;
|
||||
shortest_distance.pair_index = [box_index, next_i];
|
||||
}
|
||||
}
|
||||
});
|
||||
distances.push({
|
||||
a: shortest_distance.pair_index[0],
|
||||
b: shortest_distance.pair_index[1],
|
||||
distance: shortest_distance.distance,
|
||||
});
|
||||
}
|
||||
|
||||
return distances;
|
||||
}
|
||||
|
||||
function generate_n_shortest_pairings(
|
||||
box_list: Coordinate[],
|
||||
max_pairings: number,
|
||||
): number[][] {
|
||||
const distance_list = generate_n_shortest_distances(box_list, max_pairings);
|
||||
const circuits: number[][] = [];
|
||||
|
||||
distance_list.forEach((distance) => {
|
||||
const a = circuits.find((c) => c.includes(distance.a));
|
||||
let b = circuits.find((c) => c.includes(distance.b));
|
||||
|
||||
if (a && b) {
|
||||
b.forEach((x) => a.push(x));
|
||||
b = [];
|
||||
} else if (a) {
|
||||
a.push(distance.b);
|
||||
} else if (b) {
|
||||
b.push(distance.a);
|
||||
} else {
|
||||
circuits.push([distance.a, distance.b]);
|
||||
}
|
||||
});
|
||||
|
||||
return circuits;
|
||||
}
|
||||
|
||||
function _generate_n_shortest_pairings_old(
|
||||
box_list: Coordinate[],
|
||||
max_pairings: number,
|
||||
): number[][] {
|
||||
const circuits: number[][] = [];
|
||||
|
||||
// Pair generation
|
||||
for (let i = 0; i < max_pairings; i++) {
|
||||
const shortest_distance: { distance: number; pair_index: number[] } = {
|
||||
distance: Infinity, // Dirty, but it's late
|
||||
pair_index: [],
|
||||
};
|
||||
|
||||
// Pair boxes
|
||||
box_list.forEach((box, box_index) => {
|
||||
for (let next_i = box_index + 1; next_i < box_list.length; next_i++) {
|
||||
const next_box = box_list[next_i];
|
||||
const next_distance = calc_distance(box, next_box);
|
||||
if (
|
||||
next_distance < shortest_distance.distance &&
|
||||
!circuits.some((pairing) =>
|
||||
pairing.includes(box_index) && pairing.includes(next_i)
|
||||
)
|
||||
) {
|
||||
shortest_distance.distance = next_distance;
|
||||
shortest_distance.pair_index = [box_index, next_i];
|
||||
}
|
||||
}
|
||||
});
|
||||
// Check shortest pairings and then push
|
||||
const a_circiut = circuits.find((c) =>
|
||||
c.includes(shortest_distance.pair_index[0])
|
||||
);
|
||||
const b_circiut = circuits.find((c) =>
|
||||
c.includes(shortest_distance.pair_index[1])
|
||||
);
|
||||
|
||||
console.log(">> ", {
|
||||
circuits,
|
||||
a_circiut,
|
||||
b_circiut,
|
||||
new: shortest_distance.pair_index,
|
||||
distance: shortest_distance.distance,
|
||||
});
|
||||
|
||||
if (
|
||||
a_circiut && b_circiut
|
||||
) {
|
||||
const n_index = circuits.findIndex((p) =>
|
||||
p.includes(shortest_distance.pair_index[1])
|
||||
);
|
||||
b_circiut.forEach((b) => {
|
||||
circuits.find((p) => p.includes(shortest_distance.pair_index[0]))
|
||||
?.push(b);
|
||||
});
|
||||
circuits[n_index] = [];
|
||||
} else if (
|
||||
a_circiut
|
||||
) {
|
||||
circuits.find((p) => p.includes(shortest_distance.pair_index[0]))
|
||||
?.push(shortest_distance.pair_index[1]);
|
||||
} else if (
|
||||
b_circiut
|
||||
) {
|
||||
circuits.find((p) => p.includes(shortest_distance.pair_index[1]))
|
||||
?.push(shortest_distance.pair_index[0]);
|
||||
} else {
|
||||
circuits.push(shortest_distance.pair_index);
|
||||
}
|
||||
}
|
||||
|
||||
return circuits;
|
||||
}
|
||||
|
||||
function calc_distance(a: Coordinate, b: Coordinate): number {
|
||||
return Math.sqrt(
|
||||
Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2) + Math.pow(a.z - b.z, 2),
|
||||
);
|
||||
}
|
||||
|
||||
export async function read_junction_box_3d_positions(
|
||||
path: string,
|
||||
): Promise<Coordinate[]> {
|
||||
const file_txt = await Deno.readTextFile(path);
|
||||
const coordinates: Coordinate[] = [];
|
||||
file_txt.split("\n").forEach((raw_coord) => {
|
||||
if (!raw_coord.length) {
|
||||
return;
|
||||
}
|
||||
const coord_array = raw_coord.split(",");
|
||||
coordinates.push({
|
||||
x: parseInt(coord_array[0]),
|
||||
y: parseInt(coord_array[1]),
|
||||
z: parseInt(coord_array[2]),
|
||||
});
|
||||
});
|
||||
|
||||
return coordinates;
|
||||
}
|
||||
Reference in New Issue
Block a user