From f24f5b41487228a936d6195fcbf1fc84eea38b85 Mon Sep 17 00:00:00 2001 From: Allianzcortex Date: Mon, 8 Dec 2025 10:52:47 -0500 Subject: [PATCH 1/2] add 2025 day8 solution --- README.md | 2 + advent_of_code/2025(python)/day8.py | 53 ++++++++++++++++++++++++ advent_of_code/2025(python)/test_day8.py | 36 ++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 advent_of_code/2025(python)/day8.py create mode 100644 advent_of_code/2025(python)/test_day8.py diff --git a/README.md b/README.md index 9340644..65dcda8 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/advent_of_code/2025(python)/day8.py b/advent_of_code/2025(python)/day8.py new file mode 100644 index 0000000..f27a858 --- /dev/null +++ b/advent_of_code/2025(python)/day8.py @@ -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 diff --git a/advent_of_code/2025(python)/test_day8.py b/advent_of_code/2025(python)/test_day8.py new file mode 100644 index 0000000..a8893ec --- /dev/null +++ b/advent_of_code/2025(python)/test_day8.py @@ -0,0 +1,36 @@ +import unittest + +from day2 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() From 283b6fd3f94554516392d773c7fdf10551c76e1d Mon Sep 17 00:00:00 2001 From: Allianzcortex Date: Mon, 8 Dec 2025 10:55:23 -0500 Subject: [PATCH 2/2] fix test import --- advent_of_code/2025(python)/test_day8.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advent_of_code/2025(python)/test_day8.py b/advent_of_code/2025(python)/test_day8.py index a8893ec..46c801a 100644 --- a/advent_of_code/2025(python)/test_day8.py +++ b/advent_of_code/2025(python)/test_day8.py @@ -1,6 +1,6 @@ import unittest -from day2 import solve_day8_part1 +from day8 import solve_day8_part1 question_input = [ "162,817,812",