Skip to content

Commit 4882428

Browse files
committed
Implement 2024 day 23
Is using networkx cheating? I don't think so. I've already solved max-clique before, so I know I can do it
1 parent 9e552c9 commit 4882428

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

2024/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [
88
"click>=8.1.7",
9+
"networkx>=3.4.2",
910
"numpy>=2.1.2",
1011
]
1112

2024/src/aoc/days/day23.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from collections import defaultdict
2+
3+
import networkx
4+
5+
from . import SeparateRunner
6+
7+
8+
class DayRunner(SeparateRunner):
9+
@classmethod
10+
def part1(cls, input: str) -> int:
11+
edges = defaultdict(set)
12+
13+
for line in input.strip().split("\n"):
14+
a, b = line.split("-")
15+
edges[a].add(b)
16+
edges[b].add(a)
17+
18+
found = set()
19+
20+
for a, out in edges.items():
21+
if a[0] != "t":
22+
continue
23+
24+
for b in out:
25+
for c in edges[b]:
26+
if c in out:
27+
found.add(tuple(sorted([a, b, c])))
28+
29+
return len(found)
30+
31+
@classmethod
32+
def part2(cls, input: str) -> str:
33+
graph = networkx.Graph()
34+
35+
for line in input.strip().split("\n"):
36+
a, b = line.split("-")
37+
graph.add_edge(a, b)
38+
39+
cliques = networkx.find_cliques(graph)
40+
max_clique = max(cliques, key=len)
41+
42+
return ",".join(sorted(max_clique))

2024/tests/samples/23.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
kh-tc
2+
qp-kh
3+
de-cg
4+
ka-co
5+
yn-aq
6+
qp-ub
7+
cg-tb
8+
vc-aq
9+
tb-ka
10+
wh-tc
11+
yn-cg
12+
kh-ub
13+
ta-co
14+
de-co
15+
tc-td
16+
tb-wq
17+
wh-td
18+
ta-ka
19+
td-qp
20+
aq-cg
21+
wq-ub
22+
ub-vc
23+
de-ta
24+
wq-aq
25+
wq-vc
26+
wh-yn
27+
ka-de
28+
kh-ta
29+
co-tc
30+
wh-qp
31+
tb-vc
32+
td-yn

2024/tests/test_day23.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from aoc.days.day23 import DayRunner
2+
3+
from . import get_data
4+
5+
6+
def test_sample_part1() -> None:
7+
assert DayRunner.part1(get_data(23)) == 7
8+
9+
10+
def test_sample_part2() -> None:
11+
assert DayRunner.part2(get_data(23)) == "co,de,ka,ta"

2024/uv.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)