summaryrefslogtreecommitdiff
path: root/2025/aoc2025-d08.py
diff options
context:
space:
mode:
authorbthink <bthink@fake.com>2025-12-12 12:42:43 +0100
committerbthink <bthink@fake.com>2025-12-12 12:42:43 +0100
commitde308443f4ed4e12cd18578af09b93d7d01e2a84 (patch)
treee8977d445fe6a86b45dfc8bec8aa46b1fddde76c /2025/aoc2025-d08.py
parented395fe7a7fd68f6db4ea94de8fdcdcb0fac5179 (diff)
final week of 2025HEADmasterbthink
Diffstat (limited to '2025/aoc2025-d08.py')
-rw-r--r--2025/aoc2025-d08.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/2025/aoc2025-d08.py b/2025/aoc2025-d08.py
new file mode 100644
index 0000000..2e053eb
--- /dev/null
+++ b/2025/aoc2025-d08.py
@@ -0,0 +1,69 @@
+#advent of code 2025
+#day 08
+
+def getDistance(j1,j2):
+ jx1,jy1,jz1=j1;
+ jx2,jy2,jz2=j2;
+ euclid=(jx2-jx1)*(jx2-jx1) + (jy2-jy1)*(jy2-jy1) + (jz2-jz1)*(jz2-jz1);
+ #no real need to take square root since if a^2 > b^2 then a > b
+ #no negatives since it's distance
+ return euclid;
+#parsing
+boxes=[];
+limit=1000; #10 for example
+PuzzleInput=open("08.in","r");
+for line in PuzzleInput:
+ boxes.append(tuple([int(p) for p in line.split(",")]));
+PuzzleInput.close();
+
+numberOfBoxes=len(boxes);
+distances=set();
+for bi1,b1 in enumerate(boxes):
+ for bi2,b2 in enumerate(boxes):
+ if bi2==bi1:continue;
+ d1=getDistance(b1,b2);
+ distances.add((d1,tuple(sorted((b1,b2))) ) );
+distances=sorted(distances,key=lambda m: m[0]);
+
+circuits=dict();
+for di in range(len(distances)):
+ d,boxtuple=distances[di];
+ box1,box2=boxtuple;
+ if box1 not in circuits and box2 not in circuits:
+ circuits[box1]=set([box1,box2]); #first box contains the circuit
+ circuits[box2]=box1; #second box links to first one
+ elif box1 in circuits and box2 not in circuits:
+ b3=box1; #find the box which has the circuit set assigned in dictionary
+ while type(circuits[b3])!=set:
+ b3=circuits[b3];
+ circuits[b3].add(box2);
+ circuits[box2]=b3;
+ elif box2 in circuits and box1 not in circuits:
+ b4=box2;
+ while type(circuits[b4])!=set:
+ b4=circuits[b4];
+ circuits[b4].add(box1);
+ circuits[box1]=b4;
+ elif box2 in circuits and box1 in circuits:
+ b3=box1;
+ while type(circuits[b3])!=set:
+ b3=circuits[b3];
+ b4=box2;
+ while type(circuits[b4])!=set:
+ b4=circuits[b4];
+ if b3==b4: continue;
+ circuits[b3].update(circuits[b4]);
+ circuits[b4]=b3;
+
+ clustering=sorted([len(circuits[cluster]) for cluster in circuits if type(circuits[cluster])==set],reverse=True);
+ if clustering[0]==numberOfBoxes:
+ part2=box1[0]*box2[0];
+ break;
+
+ if di==limit:
+ part1=1;
+ for i in range(3):
+ part1*=clustering[i];
+
+print("part 1 =",part1, part1==103488);
+print("part 2 =",part2, part2==8759985540);