-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday23.py
More file actions
executable file
·61 lines (47 loc) · 1.63 KB
/
day23.py
File metadata and controls
executable file
·61 lines (47 loc) · 1.63 KB
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
from collections import defaultdict
from itertools import combinations
from aoc_utils import * # type: ignore
from aocd import get_data
data = get_data(year=2024, day=23, block=True)
def parse(data):
network = defaultdict(set)
for line in data.splitlines():
a, b = line.split("-")
network[a].add(b)
network[b].add(a)
return network
def part_one(data):
network = parse(data)
candidates = [c for c in network if c[0] == "t"]
t = 0
history = set()
for candidate in candidates:
for a, b in combinations(network[candidate], 2):
if b in network[a] and a in network[b]:
subgraph = frozenset([candidate, a, b])
t += subgraph not in history
history.add(subgraph)
return t
def part_two(data):
network = parse(data)
computers = set(network.keys())
# Bron-Kerbosch algorithm
# Finds all maximal cliques
def _find(partial, candidates, exclude):
if not candidates and not exclude:
yield partial
else:
for candidate in list(candidates):
if candidate in exclude:
continue
yield from _find(
partial.union([candidate]),
candidates.intersection(network[candidate]),
exclude.intersection(network[candidate]),
)
candidates.remove(candidate)
exclude.add(candidate)
max_clique = max(_find(set(), set(computers), set()), key=lambda c: len(c))
return ",".join(sorted(max_clique))
print(part_one(data))
print(part_two(data))