Part 1 uses the excellent itertools.combinations(). Part 2 introduces Bron-Kerbosch algorithm to the util library!
38 lines
979 B
Python
38 lines
979 B
Python
from collections import defaultdict
|
|
from itertools import combinations
|
|
|
|
from output import bk
|
|
|
|
|
|
def solve(data):
|
|
graph = defaultdict(set)
|
|
for line in data.splitlines():
|
|
a, b = line.strip().split("-")
|
|
graph[a].add(b)
|
|
graph[b].add(a)
|
|
triplets_connected = sum(
|
|
(
|
|
a in graph[b]
|
|
and a in graph[c]
|
|
and b in graph[a]
|
|
and b in graph[c]
|
|
and c in graph[a]
|
|
and a in graph[b]
|
|
and any(x.startswith("t") for x in [a, b, c])
|
|
)
|
|
for a, b, c in combinations(graph.keys(), r=3)
|
|
)
|
|
cliques = bk(graph, set(graph.keys()))
|
|
max_clique = sorted(cliques, key=lambda i: len(i), reverse=True)[0]
|
|
lan_password = ",".join(sorted(list(max_clique)))
|
|
return triplets_connected, lan_password
|
|
|
|
|
|
if __name__ == "__main__":
|
|
with open("./input/23.txt", "r") as f:
|
|
inp = f.read().strip()
|
|
|
|
p1, p2 = solve(inp)
|
|
|
|
print(p1)
|
|
print(p2)
|