Solve 2024 day 23

This commit is contained in:
felixm 2024-12-24 11:33:35 -05:00
parent 265829715a
commit 3dd208c088
2 changed files with 44 additions and 1 deletions

43
2024/d23.py Normal file
View 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)))

View File

@ -313,6 +313,6 @@ and focus. Of course, learning more algorithms and techniques helps.
- Day 20: `01:08:53 3637 0 01:53:01 2837 0`
- Day 21: `00:48:40 215 0 - - -`
- Day 22: `00:13:04 1930 0 00:28:29 739 0`
- Day 23:
- Day 23: ` >24h 20096 0 >24h 17620 0`
- Day 24:
- Day 25: