Skip to content

Commit ee518fc

Browse files
committed
Completed day 8 of year 2025
1 parent 37ab1d6 commit ee518fc

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

2025/8.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
split_data = True
5+
completed = True
6+
raw_data = None # Not To be touched
7+
8+
def part1(data):
9+
# First we create the distance matrix...
10+
distance_map = {}
11+
for i1 in range(len(data)):
12+
for i2 in range(i1 + 1, len(data)):
13+
x1, y1, z1 = [int(x) for x in data[i1].split(',')]
14+
x2, y2, z2 = [int(x) for x in data[i2].split(',')]
15+
distance_map[data[i1] + '|' + data[i2]] = ((x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2)**0.5
16+
17+
18+
circuits = [[junc] for junc in data] # These are the individual circuits...
19+
for key in sorted(distance_map.keys(), key=lambda x: distance_map[x])[:1000]:
20+
j1, j2 = key.split('|')
21+
i1, i2 = -1, -1
22+
for i in range(len(circuits)):
23+
if j1 in circuits[i]:
24+
i1 = i
25+
if j2 in circuits[i]:
26+
i2 = i
27+
if i1 != -1 and i2 != i2: break
28+
29+
30+
if i1 == i2: continue
31+
circuits[i1].extend(circuits[i2])
32+
circuits.pop(i2)
33+
34+
35+
count = sorted([len(cir) for cir in circuits], reverse=True)
36+
return count[0] * count[1] * count[2]
37+
38+
def part2(data):
39+
# First we create the distance matrix...
40+
distance_map = {}
41+
for i1 in range(len(data)):
42+
for i2 in range(i1 + 1, len(data)):
43+
x1, y1, z1 = [int(x) for x in data[i1].split(',')]
44+
x2, y2, z2 = [int(x) for x in data[i2].split(',')]
45+
distance_map[data[i1] + '|' + data[i2]] = ((x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2)**0.5
46+
47+
48+
circuits = [[junc] for junc in data] # These are the individual circuits...
49+
for key in sorted(distance_map.keys(), key=lambda x: distance_map[x]):
50+
j1, j2 = key.split('|')
51+
i1, i2 = -1, -1
52+
for i in range(len(circuits)):
53+
if j1 in circuits[i]:
54+
i1 = i
55+
if j2 in circuits[i]:
56+
i2 = i
57+
if i1 != -1 and i2 != i2: break
58+
59+
60+
if i1 == i2: continue
61+
circuits[i1].extend(circuits[i2])
62+
circuits.pop(i2)
63+
64+
if len(circuits) == 1:
65+
return int(j1.split(',')[0]) * int(j2.split(',')[0])
66+
67+
return None

0 commit comments

Comments
 (0)