-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.py
More file actions
91 lines (69 loc) · 2.06 KB
/
day8.py
File metadata and controls
91 lines (69 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import sys
import numpy as np
import math
input_file = "input/8.ex" if len(sys.argv) < 2 else sys.argv[1]
with open(input_file, "r") as f:
lines = f.readlines()
lines = [ l.strip() for l in lines ]
class Box:
def __init__(self, id, coords):
self.id = id
self.x = coords[0]
self.y = coords[1]
self.z = coords[2]
self.circuit = None
def __repr__(self):
return f"{self.id} ({self.x}, {self.y}, {self.z}) {self.circuit}"
def set_circuit(self, circuit):
self.circuit = circuit
def distance(self, box):
dx = self.x - box.x
dy = self.y - box.y
dz = self.z - box.z
return math.sqrt(dx*dx + dy*dy + dz*dz)
class Circuit:
def __init__(self, id, box):
self.id = id
self.boxes = { box }
box.set_circuit(self)
def __repr__(self):
return f"{{{self.id}={len(self)}}}"
def __len__(self):
return len(self.boxes)
def __gt__(self, other):
return len(self) > len(other)
def merge(self, circuit):
for box in circuit.boxes:
self.boxes.add(box)
box.set_circuit(self)
boxes = [ Box(i, [ int(t) for t in line.split(",") ]) for i, line in enumerate(lines) ]
# D = [ [ -1 ] * len(boxes) for _ in range(len(boxes)) ]
# for i, box in enumerate(boxes):
# for j in range(i+1, len(boxes)):
# D[i][j] = box.distance(boxes[j])
D = []
for i, box in enumerate(boxes):
for j in range(i+1, len(boxes)):
D.append((box.distance(boxes[j]), box, boxes[j]))
D = sorted(D)
circuits = [ Circuit(i, box) for i, box in enumerate(boxes) ]
num_steps = 1000 # 10 for 8.ex, 1000 for 8.in
num_largest = 3
b1 = None
b2 = None
#for _ in range(num_steps): # Part A
while len(circuits) > 1: # Part B
d, b1, b2 = D.pop(0)
c1 = b1.circuit
c2 = b2.circuit
if c1 == c2:
continue
c1.merge(c2)
circuits.remove(c2)
circuits = sorted(circuits)
print(circuits)
part_a = 1
for c in circuits[-num_largest:]:
part_a *= len(c)
print("Part A", part_a)
print("Part B", b1.x * b2.x)