Skip to content

Commit 8885bee

Browse files
committed
solved day 8
1 parent e2dd8c0 commit 8885bee

File tree

3 files changed

+1161
-0
lines changed

3 files changed

+1161
-0
lines changed

src/2025/day08.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
function straightLineDistance3D(a, b) {
2+
return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2 + (a.z - b.z) ** 2);
3+
}
4+
5+
function top3Circuits(junctions) {
6+
let circuitCounts = new Map();
7+
for (let junction of junctions) {
8+
if (junction.circuit === null) continue;
9+
circuitCounts.set(
10+
junction.circuit,
11+
(circuitCounts.get(junction.circuit) || 0) + 1,
12+
);
13+
}
14+
let counts = Array.from(circuitCounts.values());
15+
counts.sort((a, b) => b - a);
16+
return counts.slice(0, 3).reduce((a, b) => a * b, 1);
17+
}
18+
19+
function getAllPairs(junctions) {
20+
let pairs = [];
21+
for (let i = 0; i < junctions.length; i++) {
22+
for (let j = i + 1; j < junctions.length; j++) {
23+
let a = junctions[i];
24+
let b = junctions[j];
25+
let dist = straightLineDistance3D(a, b);
26+
pairs.push({ a, b, dist });
27+
}
28+
}
29+
return pairs.sort((p1, p2) => p1.dist - p2.dist);
30+
}
31+
32+
function connectPair(pair, junctions) {
33+
let { a, b } = pair;
34+
if (a.circuit === null && b.circuit === null) {
35+
a.circuit = b.circuit = Symbol();
36+
} else if (a.circuit !== null && b.circuit === null) {
37+
b.circuit = a.circuit;
38+
} else if (a.circuit === null && b.circuit !== null) {
39+
a.circuit = b.circuit;
40+
} else if (a.circuit !== b.circuit) {
41+
let oldCircuit = b.circuit;
42+
let newCircuit = a.circuit;
43+
for (let j = 0; j < junctions.length; j++) {
44+
if (junctions[j].circuit === oldCircuit) {
45+
junctions[j].circuit = newCircuit;
46+
}
47+
}
48+
}
49+
return pair;
50+
}
51+
52+
function allConnected(junctions) {
53+
let firstCircuit = junctions[0].circuit;
54+
if (firstCircuit === null) return false;
55+
for (let junction of junctions) {
56+
if (junction.circuit !== firstCircuit) {
57+
return false;
58+
}
59+
}
60+
return true;
61+
}
62+
63+
export function part1(input, times = 1000) {
64+
let junctions = input.split("\n").map(line => {
65+
let [x, y, z] = line.split(",").map(Number);
66+
return { x, y, z, circuit: null };
67+
});
68+
let pairs = getAllPairs(junctions).slice(0, times);
69+
for (let i = 0; i < pairs.length; i++) connectPair(pairs[i], junctions);
70+
return top3Circuits(junctions);
71+
}
72+
73+
export function part2(input) {
74+
let junctions = input.split("\n").map(line => {
75+
let [x, y, z] = line.split(",").map(Number);
76+
return { x, y, z, circuit: null };
77+
});
78+
let pair;
79+
let pairs = getAllPairs(junctions);
80+
while (!allConnected(junctions)) pair = connectPair(pairs.shift(), junctions);
81+
return pair.a.x * pair.b.x;
82+
}

src/2025/day08.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { part1, part2 } from "./day08.js";
2+
import { describe, test, expect } from "vitest";
3+
import readInput from "../utils/read-input.js";
4+
5+
let input = readInput(import.meta.url);
6+
7+
describe("day08 2025", () => {
8+
describe("part1", () => {
9+
test("it should work for part 1 examples", () => {
10+
expect(
11+
part1(
12+
[
13+
"162,817,812",
14+
"57,618,57",
15+
"906,360,560",
16+
"592,479,940",
17+
"352,342,300",
18+
"466,668,158",
19+
"542,29,236",
20+
"431,825,988",
21+
"739,650,466",
22+
"52,470,668",
23+
"216,146,977",
24+
"819,987,18",
25+
"117,168,530",
26+
"805,96,715",
27+
"346,949,466",
28+
"970,615,88",
29+
"941,993,340",
30+
"862,61,35",
31+
"984,92,344",
32+
"425,690,689",
33+
].join("\n"),
34+
10,
35+
),
36+
).toEqual(40);
37+
});
38+
39+
test("it should work for part 1 input", () => {
40+
expect(part1(input)).toEqual(140008);
41+
});
42+
});
43+
44+
describe("part2", () => {
45+
test("it should work for part 2 examples", () => {
46+
expect(
47+
part2(
48+
[
49+
"162,817,812",
50+
"57,618,57",
51+
"906,360,560",
52+
"592,479,940",
53+
"352,342,300",
54+
"466,668,158",
55+
"542,29,236",
56+
"431,825,988",
57+
"739,650,466",
58+
"52,470,668",
59+
"216,146,977",
60+
"819,987,18",
61+
"117,168,530",
62+
"805,96,715",
63+
"346,949,466",
64+
"970,615,88",
65+
"941,993,340",
66+
"862,61,35",
67+
"984,92,344",
68+
"425,690,689",
69+
].join("\n"),
70+
),
71+
).toEqual(25272);
72+
});
73+
74+
test("it should work for part 2 input", () => {
75+
expect(part2(input)).toEqual(9253260633);
76+
});
77+
});
78+
});

0 commit comments

Comments
 (0)