Skip to content

Commit 3e21d94

Browse files
committed
AoC2025: day 8 part 1
1 parent 348284a commit 3e21d94

4 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {part1, part2} from "./day08";
2+
3+
describe('2025 Day 8', () => {
4+
test('Part 1', async () => {
5+
expect(await part1('testInput1', true)).toEqual(40);
6+
expect(await part1('input')).toEqual(123234);
7+
});
8+
9+
test('Part 2', async () => {
10+
// expect(await part2('testInput1')).toEqual(31);
11+
// expect(await part2('input')).toEqual(29379307);
12+
});
13+
});

2024-2025/src/2025/day08/day08.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import path from "node:path";
2+
import {readInputLineByLine} from "@utils/io";
3+
import {Coord3d} from "@utils/grid3d";
4+
5+
export async function part1(inputFile: string, isTestInput: boolean = false) {
6+
return await day8(inputFile, findCircuits, isTestInput);
7+
}
8+
9+
export async function part2(inputFile: string, isTestInput: boolean = false) {
10+
return await day8(inputFile, findCircuits, isTestInput);
11+
}
12+
13+
async function day8(inputFile: string, calcFn?: (boxes: Coord3d[], isTestInput: boolean) => number, isTestInput: boolean = false) {
14+
const inputPath = path.join(__dirname, inputFile);
15+
const lines = await readInputLineByLine(inputPath);
16+
const boxes = lines.map((line) => {
17+
const split = line.split(",").map(Number);
18+
return new Coord3d(split[0], split[1], split[2]);
19+
});
20+
return calcFn?.(boxes, isTestInput);
21+
}
22+
23+
function dist(a: Coord3d, b: Coord3d): number {
24+
return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2 + (a.z - b.z) ** 2);
25+
}
26+
27+
function findCircuits(boxes: Coord3d[], isTestInput: boolean): number {
28+
const n = boxes.length;
29+
const k = isTestInput ? Math.floor(n / 2) : n;
30+
31+
// Collect all pairs sorted by distance
32+
const pairs: [number, number, number][] = [];
33+
for (let i = 0; i < n; i++) {
34+
for (let j = i + 1; j < n; j++) {
35+
pairs.push([dist(boxes[i], boxes[j]), i, j]);
36+
}
37+
}
38+
pairs.sort((a, b) => a[0] - b[0]);
39+
40+
// Union-Find
41+
const parent = Array.from({length: n}, (_, i) => i);
42+
const size = new Array(n).fill(1);
43+
44+
function find(x: number): number {
45+
while (parent[x] !== x) {
46+
parent[x] = parent[parent[x]];
47+
x = parent[x];
48+
}
49+
return x;
50+
}
51+
52+
function union(a: number, b: number) {
53+
a = find(a); b = find(b);
54+
if (a === b) return;
55+
if (size[a] < size[b]) [a, b] = [b, a];
56+
parent[b] = a;
57+
size[a] += size[b];
58+
}
59+
60+
for (let i = 0; i < k; i++) {
61+
union(pairs[i][1], pairs[i][2]);
62+
}
63+
64+
// Collect circuit sizes via root nodes
65+
const sizes: number[] = [];
66+
for (let i = 0; i < n; i++) {
67+
if (find(i) === i) sizes.push(size[i]);
68+
}
69+
sizes.sort((a, b) => b - a);
70+
71+
return sizes[0] * sizes[1] * sizes[2];
72+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
162,817,812
2+
57,618,57
3+
906,360,560
4+
592,479,940
5+
352,342,300
6+
466,668,158
7+
542,29,236
8+
431,825,988
9+
739,650,466
10+
52,470,668
11+
216,146,977
12+
819,987,18
13+
117,168,530
14+
805,96,715
15+
346,949,466
16+
970,615,88
17+
941,993,340
18+
862,61,35
19+
984,92,344
20+
425,690,689

2024-2025/src/utils/grid3d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export class Coord3d {
2+
constructor(public x: number, public y: number, public z: number) {}
3+
4+
equals(other: Coord3d): boolean {
5+
return this.x === other.x && this.y === other.y && this.z !== other.z;
6+
}
7+
8+
serialize(): string {
9+
return `{${this.x},${this.y},${this.z}}`;
10+
}
11+
12+
static deserialize(input: string): Coord3d {
13+
const [x, y, z] = input.replace("{", "").replace("}", "").split(',').map(Number);
14+
return new Coord3d(x, y, z);
15+
}
16+
17+
toString(): string {
18+
return this.serialize();
19+
}
20+
}
21+
22+
23+
export interface Coord3d {
24+
x: number;
25+
y: number;
26+
z: number;
27+
}

0 commit comments

Comments
 (0)