Skip to content

Instantly share code, notes, and snippets.

@nickponline
Created December 23, 2024 12:01
Show Gist options
  • Select an option

  • Save nickponline/408de698a6368cc3045ed680c66d4561 to your computer and use it in GitHub Desktop.

Select an option

Save nickponline/408de698a6368cc3045ed680c66d4561 to your computer and use it in GitHub Desktop.
# https://adventofcode.com/2024/day/23
import networkx as nx, re
graph = nx.Graph()
with open('2024-23.in') as f:
graph.add_edges_from([(source, target) for source, target in re.findall(r'(\w+)-(\w+)', f.read())])
cliques = list(nx.enumerate_all_cliques(graph))
def solve_part_1(cliques, size=3, prefix='t'):
return len([c for c in cliques if len(c) == size and any(n.startswith(prefix) for n in c)])
def solve_part_2(cliques):
largest_clique = max(cliques, key=len)
return ",".join(sorted(largest_clique))
print(solve_part_1(cliques, 3, 't'))
print(solve_part_2(cliques))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment