Compare commits
2 Commits
28e400f221
...
82e934b690
| Author | SHA1 | Date | |
|---|---|---|---|
| 82e934b690 | |||
| 464206c04c |
@@ -1,13 +1,2 @@
|
|||||||
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 () => {
|
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);
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ export default async function Laboratories() {
|
|||||||
"src/exercises/assets/day_7_input.txt",
|
"src/exercises/assets/day_7_input.txt",
|
||||||
);
|
);
|
||||||
console.log(">> Bean splits: ", count_bean_splits(tachyon_manifold));
|
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 {
|
export function count_bean_splits(tm: TachyonManifold): number {
|
||||||
@@ -44,16 +46,49 @@ export function count_bean_splits(tm: TachyonManifold): number {
|
|||||||
|
|
||||||
export function count_quantum_splits(tm: TachyonManifold): number {
|
export function count_quantum_splits(tm: TachyonManifold): number {
|
||||||
const full_tree = generate_full_quantum_tree(tm);
|
const full_tree = generate_full_quantum_tree(tm);
|
||||||
console.log(
|
|
||||||
">> Full tree\n",
|
|
||||||
full_tree.map((row) => row.join("")).join("\n"),
|
|
||||||
);
|
|
||||||
let total = 0;
|
|
||||||
|
|
||||||
console.log("==", tm[tm.length - 2]);
|
// Downwards
|
||||||
for (let i = 0; i < tm[0].length; i++) {
|
const init = full_tree[0].findIndex((p) => p === "S");
|
||||||
if (tm[tm.length - 2][i] === "|") {
|
return count_quantum_paths_downwards(full_tree, init, 1);
|
||||||
total += count_quantum_paths_upwards(tm, i, tm.length - 2) + 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,154 +14,6 @@ export default async function Playground() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
function calc_distance(a: Coordinate, b: Coordinate): number {
|
||||||
return Math.sqrt(
|
return Math.sqrt(
|
||||||
Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2) + Math.pow(a.z - b.z, 2),
|
Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2) + Math.pow(a.z - b.z, 2),
|
||||||
|
|||||||
Reference in New Issue
Block a user