2025(08): I misread the assignment, I think

This commit is contained in:
2025-12-09 19:46:36 +01:00
parent f648a40dd6
commit 76e962969d
3 changed files with 166 additions and 0 deletions

87
2025/08/part1.py Normal file
View File

@ -0,0 +1,87 @@
import numpy as np
debug = False
def load_junctions(filename: str):
"""
Load junction file into memory.
"""
data = np.loadtxt(filename, dtype=int, delimiter=",")
return data
def distances(junctions, sorted=True):
"""
Use the upper triangle to calculate distances so we don't do it twice per
pair.
NOTE: The upper triangle includes the pairs of each junction with itself.
"""
jlen = len(junctions)
X, Y = np.triu_indices(jlen)
# Ignore distances if x == y.
non_self_identities = X != Y
X = X[non_self_identities]
Y = Y[non_self_identities]
# NOTE: I do note use np.zeros_like as I want the dtype to be the default (float).
dists = np.zeros(X.shape)
# For each pair, calculate the distance.
for i in range(len(X)):
x, y = X[i], Y[i]
dists[i] = np.linalg.norm(junctions[x] - junctions[y])
# Sort the distances, X, and Y from shortest to longest distance.
if sorted:
sorted_indices = np.argsort(dists)
X = X[sorted_indices]
Y = Y[sorted_indices]
dists = dists[sorted_indices]
return X, Y, dists
def num_of_combinations(length):
return length*(length - 1)//2
def is_connected(x, connections):
for connection in connections:
if x in connection:
return True
return False
def get_circuits(junctions, connections):
"""
Convert connections into circuits.
Start by adding all junction indices as circuits, then iterating over
connection to make disjunct sets.
"""
# circuits = [[x] for x in range(len(junctions))]
circuits = [list(x) for x in connections]
changed = True
while changed:
changed = False
for i in range(len(circuits)):
for j in range(i + 1, len(circuits)):
for x in circuits[i]:
if x in circuits[j]:
# NOTE: Elements might appear multiple times per circuit.
circuits[i] += circuits.pop[j]
changed = True
return circuits
if __name__ == "__main__":
test_junctions = load_junctions("testinput")
X, Y, dists = distances(test_junctions)
connections = []
# unconnected = list(range(len(test_junctions)))
for i in range(len(X)):
x, y = X[i], Y[i]
if not is_connected(x, connections) and not is_connected(y, connections):
connections.append((x, y))
print(len(connections))