Skip to content

Commit 2f344f7

Browse files
committed
Day 23 solution
1 parent 8560d20 commit 2f344f7

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

solutions/day23.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from itertools import combinations
2+
from typing import List
3+
4+
import networkx as nx
5+
6+
from aoc.models.base import SolutionBase
7+
8+
9+
class Solution(SolutionBase):
10+
"""Solution for Advent of Code 2024 - Day 23: LAN Party.
11+
12+
This class solves a puzzle about analyzing LAN party connections and finding
13+
gaming groups. Part 1 identifies valid gaming trios including teachers, while
14+
Part 2 finds the largest possible gaming group.
15+
16+
Input format:
17+
- List of connections, one per line
18+
- Each line contains two node IDs separated by a hyphen
19+
- Node IDs starting with 't' represent teachers
20+
- All other nodes represent students
21+
- Connections indicate which players can directly play together
22+
23+
This class inherits from `SolutionBase` and uses NetworkX to analyze the
24+
connection graph and find valid gaming groups of different sizes.
25+
"""
26+
27+
def construct_graph(self, data: List[str]) -> nx.Graph:
28+
"""Construct a NetworkX graph from the input connection data.
29+
30+
Creates an undirected graph where nodes represent players and edges
31+
represent possible direct connections for gaming.
32+
33+
Args:
34+
data: List of strings, each containing two node IDs separated by a hyphen
35+
36+
Returns:
37+
NetworkX Graph object representing the connection network
38+
"""
39+
G = nx.Graph()
40+
for line in data:
41+
parts = line.split("-")
42+
G.add_edge(parts[0], parts[1])
43+
44+
return G
45+
46+
def part1(self, data: List[str]) -> int:
47+
"""Count valid gaming trios that include at least one teacher.
48+
49+
Analyzes the connection graph to find all possible groups of three
50+
players that:
51+
1. Are fully connected (form a clique)
52+
2. Include at least one teacher (node starting with 't')
53+
3. Can play together based on direct connections
54+
55+
Args:
56+
data: List of strings representing player connections
57+
58+
Returns:
59+
Number of unique valid gaming trios
60+
"""
61+
G = self.construct_graph(data)
62+
cliques = [
63+
clique
64+
for clique in nx.find_cliques(G)
65+
if len(clique) >= 3 and any(node[0] == "t" for node in clique)
66+
]
67+
sets = set()
68+
69+
for clique in cliques:
70+
for nodes in combinations(clique, 3):
71+
if any(node[0] == "t" for node in nodes):
72+
sets.add(tuple(sorted(nodes)))
73+
74+
return len(sets)
75+
76+
def part2(self, data: List[str]) -> int:
77+
"""Find the largest possible gaming group.
78+
79+
Identifies the maximum clique in the connection graph, representing
80+
the largest group of players that can all play together directly.
81+
Returns the players in alphabetical order as a comma-separated string.
82+
83+
Args:
84+
data: List of strings representing player connections
85+
86+
Returns:
87+
Comma-separated string of player IDs in the largest gaming group,
88+
sorted alphabetically
89+
"""
90+
G = self.construct_graph(data)
91+
cliques = nx.find_cliques(G)
92+
LAN = sorted(sorted(cliques, key=len, reverse=True)[0])
93+
94+
return ",".join(LAN)

0 commit comments

Comments
 (0)