Advertisement
hhoppe

Advent of code 2024 day 23

Dec 23rd, 2024 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. def day23(s, *, part2=False):
  2.   graph = networkx.Graph()
  3.   graph.add_edges_from(tuple(line.split('-')) for line in s.splitlines())
  4.  
  5.   def get_triangles(graph):
  6.     for a in graph:
  7.       for b in graph.neighbors(a):
  8.         if b > a:
  9.           for c in graph.neighbors(a):
  10.             if c > b and graph.has_edge(b, c):
  11.               yield a, b, c
  12.  
  13.   if not part2:
  14.     triangles = get_triangles(graph)
  15.     return sum(1 for triangle in triangles if any(node[0] == 't' for node in triangle))
  16.  
  17.   largest_clique = max(networkx.find_cliques(graph), key=len)  # (Finds maximal cliques.)
  18.   return ','.join(sorted(largest_clique))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement