Solve 2024 day 23
This commit is contained in:
parent
265829715a
commit
3dd208c088
43
2024/d23.py
Normal file
43
2024/d23.py
Normal file
@ -0,0 +1,43 @@
|
||||
from lib import get_data
|
||||
from collections import defaultdict
|
||||
|
||||
data = get_data(__file__)
|
||||
g = defaultdict(set)
|
||||
|
||||
for line in data.splitlines():
|
||||
a, b = line.split("-")
|
||||
g[a].add(b)
|
||||
g[b].add(a)
|
||||
|
||||
|
||||
cs = set()
|
||||
for a, bs in g.items():
|
||||
for b in bs:
|
||||
for c in g[b]:
|
||||
if c in bs:
|
||||
cs.add(tuple(sorted([a, b, c])))
|
||||
|
||||
cs = [t for t in cs if any(x.startswith("t") for x in t)]
|
||||
print(len(cs))
|
||||
|
||||
|
||||
def find_largest_clique(graph):
|
||||
def bron_kerbosch(r, p, x, max_clique):
|
||||
if not p and not x:
|
||||
if len(r) > len(max_clique[0]):
|
||||
max_clique[0] = r.copy()
|
||||
return
|
||||
|
||||
for v in p.copy():
|
||||
neighbors = graph[v]
|
||||
bron_kerbosch(r | {v}, p & neighbors, x & neighbors, max_clique)
|
||||
p.remove(v)
|
||||
x.add(v)
|
||||
|
||||
max_clique = [set()]
|
||||
bron_kerbosch(set(), set(graph.keys()), set(), max_clique)
|
||||
return max_clique[0]
|
||||
|
||||
|
||||
cs = find_largest_clique(g)
|
||||
print(",".join(sorted(cs)))
|
Loading…
Reference in New Issue
Block a user