Spaces:
Running
Running
File size: 2,678 Bytes
a4da721 |
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
def load_data(file):
with open(file) as f:
data = f.readlines()
nodes = []
for line in data:
line = line.strip("\n")
(n1, n2) = line.split("-")
nodes.append((n1, n2))
return nodes
def get_connections(computer, all_nodes):
connections = set()
nodes = [node for node in all_nodes if computer in node]
for node in nodes:
# n1, n2 = node
for c in list(node):
if c != computer:
connections.add(c)
return list(connections)
# file = "test.txt"
file = "input.txt"
nodes = load_data(file)
# Build connections
all_nodes = []
all_computers = set()
for node in nodes:
c1, c2 = node
all_nodes.append(set(set((c1, c2))))
all_computers.add(c1)
all_computers.add(c2)
# We are pretty much brute forcing here
# Given a computer, find all its connections
# For each pair of connections, check if they are connected
all_three_nodes = set()
for c1 in all_computers:
connections = get_connections(c1, all_nodes)
for i in range(len(connections)):
for j in range(i+1, len(connections)):
c2 = connections[i]
c3 = connections[j]
if set((c2, c3)) in all_nodes:
# Checks if they are connected
trinode = [c1, c2, c3]
trinode = tuple(sorted(trinode))
all_three_nodes.add(trinode)
candidate_nodes = []
for node in all_three_nodes:
for c in node:
if c[0] == "t":
candidate_nodes.append(node)
break
print(len(candidate_nodes))
## Part 2
file = "input.txt"
# file = "test.txt"
nodes = load_data(file)
# Build connections
all_nodes = []
all_computers = set()
for node in nodes:
c1, c2 = node
all_nodes.append(set(set((c1, c2))))
all_computers.add(c1)
all_computers.add(c2)
# This will be useful!
# https://en.wikipedia.org/wiki/Clique_problem#Finding_a_single_maximal_clique
# Basically a greedy algorithm
def get_clique(computer):
clique = set()
clique.add(computer)
connections = get_connections(computer, all_nodes)
for connection in connections:
if connection == computer:
# lazy way of skipping itself
continue
is_valid = True
for c in clique:
if not set((c, connection)) in all_nodes:
is_valid = False
break
if is_valid:
clique.add(connection)
return clique
max_clique = set()
for computer in all_computers:
clique = get_clique(computer)
if len(clique) > len(max_clique):
max_clique = clique
print(",".join(sorted(list(max_clique))))
|