Skip to content

Commit ff0d7bb

Browse files
Advent of Code 2025 - Day 8 : Python version (#54)
* add 2025 day8 solution * fix test import
1 parent b58e03d commit ff0d7bb

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ I also worked on [advent of code](https://adventofcode.com/) in the past years
1818

1919
[advent of code 2024](./advent_of_code/2024(python)/)
2020

21+
[advent of code 2025](./advent_of_code/2025(python)/)
22+
2123
---
2224

2325
Here are python implementations for different ML/data-mining projects
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
sample_test_size = 10
2+
large_test_size = 1000
3+
4+
5+
class DSU:
6+
def __init__(self, n):
7+
self.p = list(range(n))
8+
self.sz = [1] * n
9+
10+
def find(self, x):
11+
if self.p[x] != x:
12+
self.p[x] = self.find(self.p[x])
13+
return self.p[x]
14+
15+
def union(self, a, b):
16+
ra, rb = self.find(a), self.find(b)
17+
if ra == rb:
18+
return False
19+
if self.sz[ra] < self.sz[rb]:
20+
ra, rb = rb, ra
21+
self.p[rb] = ra
22+
self.sz[ra] += self.sz[rb]
23+
return True
24+
25+
26+
def solve_day8_part1(input_):
27+
input_ = [line.split(",") for line in input_]
28+
distances = []
29+
30+
for i, line in enumerate(input_):
31+
for j in range(i+1, len(input_)):
32+
dis = get_distance(line, input_[j])
33+
distances.append((dis, (i, j)))
34+
35+
distances.sort()
36+
37+
dsu = DSU(len(input_))
38+
for i in range(sample_test_size):
39+
_, (j, k) = distances[i]
40+
dsu.union(j, k)
41+
42+
# get the largest 3 circles
43+
roots = [dsu.sz[i] for i in range(len(input_)) if dsu.find(i) == i]
44+
roots.sort(reverse=True)
45+
return roots[0] * roots[1] * roots[2]
46+
47+
48+
def get_distance(line1, line2):
49+
x_dis = int(line1[0])-int(line2[0])
50+
y_dis = int(line1[1])-int(line2[1])
51+
z_dis = int(line1[2])-int(line2[2])
52+
53+
return x_dis**2+y_dis**2+z_dis**2
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import unittest
2+
3+
from day8 import solve_day8_part1
4+
5+
question_input = [
6+
"162,817,812",
7+
"57,618,57",
8+
"906,360,560",
9+
"592,479,940",
10+
"352,342,300",
11+
"466,668,158",
12+
"542,29,236",
13+
"431,825,988",
14+
"739,650,466",
15+
"52,470,668",
16+
"216,146,977",
17+
"819,987,18",
18+
"117,168,530",
19+
"805,96,715",
20+
"346,949,466",
21+
"970,615,88",
22+
"941,993,340",
23+
"862,61,35",
24+
"984,92,344",
25+
"425,690,689",
26+
]
27+
28+
29+
class TestSample(unittest.TestCase):
30+
31+
def test_part1(self):
32+
self.assertEqual(solve_day8_part1(question_input), 40)
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)