Compare commits
1 Commits
main
...
82e934b690
| Author | SHA1 | Date | |
|---|---|---|---|
| 82e934b690 |
@@ -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);
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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