Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ I also worked on [advent of code](https://adventofcode.com/) in the past years

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

[advent of code 2025](./advent_of_code/2025(python)/)

---

Here are python implementations for different ML/data-mining projects
Expand Down
53 changes: 53 additions & 0 deletions advent_of_code/2025(python)/day8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
sample_test_size = 10
large_test_size = 1000


class DSU:
def __init__(self, n):
self.p = list(range(n))
self.sz = [1] * n

def find(self, x):
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]

def union(self, a, b):
ra, rb = self.find(a), self.find(b)
if ra == rb:
return False
if self.sz[ra] < self.sz[rb]:
ra, rb = rb, ra
self.p[rb] = ra
self.sz[ra] += self.sz[rb]
return True


def solve_day8_part1(input_):
input_ = [line.split(",") for line in input_]
distances = []

for i, line in enumerate(input_):
for j in range(i+1, len(input_)):
dis = get_distance(line, input_[j])
distances.append((dis, (i, j)))

distances.sort()

dsu = DSU(len(input_))
for i in range(sample_test_size):
_, (j, k) = distances[i]
dsu.union(j, k)

# get the largest 3 circles
roots = [dsu.sz[i] for i in range(len(input_)) if dsu.find(i) == i]
roots.sort(reverse=True)
return roots[0] * roots[1] * roots[2]


def get_distance(line1, line2):
x_dis = int(line1[0])-int(line2[0])
y_dis = int(line1[1])-int(line2[1])
z_dis = int(line1[2])-int(line2[2])

return x_dis**2+y_dis**2+z_dis**2
36 changes: 36 additions & 0 deletions advent_of_code/2025(python)/test_day8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest

from day8 import solve_day8_part1

question_input = [
"162,817,812",
"57,618,57",
"906,360,560",
"592,479,940",
"352,342,300",
"466,668,158",
"542,29,236",
"431,825,988",
"739,650,466",
"52,470,668",
"216,146,977",
"819,987,18",
"117,168,530",
"805,96,715",
"346,949,466",
"970,615,88",
"941,993,340",
"862,61,35",
"984,92,344",
"425,690,689",
]


class TestSample(unittest.TestCase):

def test_part1(self):
self.assertEqual(solve_day8_part1(question_input), 40)


if __name__ == "__main__":
unittest.main()
Loading