Guest User

Day 8

a guest
Dec 10th, 2025
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. import math
  2. from collections import Counter
  3.  
  4. file = open("sample.txt", "r")
  5. lines = file.readlines()
  6.  
  7. grid = []
  8.  
  9. c = 0
  10. for l in lines:
  11.     l = (l.strip())
  12.     l = l.split(",")
  13.  
  14.     # FIX 1: use tuples instead of lists (lists break equality checks)
  15.     t = tuple(int(x) for x in l)
  16.  
  17.     grid.append(t)
  18.  
  19.     c += 1
  20.  
  21.  
  22. distances = []
  23. a = []
  24. b = []
  25. pairs = []
  26.  
  27. start = ""
  28. end = ""
  29.  
  30.  
  31. #Store corresponding ciruit number for each point
  32. locations = grid[:]
  33. circuitNum = list(range(len(grid)))
  34.  
  35. for x in range(len(grid)):
  36.     for y in range(x+1, len(grid)):  
  37.         #calcualte and store all distances
  38.         p = grid[x]
  39.         q = grid[y]
  40.         d = math.dist(p, q)
  41.        
  42.         #x3 parallel arrays
  43.         distances.append(d)
  44.        
  45.         a.append(p)
  46.         b.append(q)
  47.  
  48. #Sort and Store
  49. combined = list(zip(distances, a, b))
  50. combined.sort(key=lambda x: x[0])
  51.  
  52. distances, a, b = zip(*combined)
  53.  
  54. print(locations)
  55. print(circuitNum)
  56.  
  57. connections = 0
  58.  
  59. for i in range(0, len(a)):
  60.  
  61.     start = a[i]
  62.     end = b[i]
  63.  
  64.     # get circuit index
  65.     startI = locations.index(start)
  66.     endI = locations.index(end)
  67.  
  68.     #get ciruit number
  69.     circuitStar = circuitNum[startI]
  70.     circuitEnd = circuitNum[endI]
  71.  
  72.     #if not the same, marge circuits
  73.     if circuitStar != circuitEnd:
  74.         connections += 1
  75.  
  76.         # merge circuits by changing index of one set
  77.         for x in range(len(circuitNum)):
  78.             if circuitNum[x] == circuitEnd:
  79.                 circuitNum[x] = circuitStar
  80.  
  81.         print("Connected:", start, end, "→ total:", connections)
  82.  
  83.         if connections == 10:
  84.             print("Final circuits:", Counter(circuitNum))
  85.             break
  86.  
Advertisement
Add Comment
Please, Sign In to add comment