question: --- Day 23: LAN Party ---
  
  As The Historians wander around a secure area at Easter Bunny HQ, you come across posters for a LAN party scheduled for today! Maybe you can find it; you connect to a nearby datalink port and download a map of the local network (your puzzle input).
  
  The network map provides a list of every connection between two computers. For example:
  
  kh-tc
  qp-kh
  de-cg
  ka-co
  yn-aq
  qp-ub
  cg-tb
  vc-aq
  tb-ka
  wh-tc
  yn-cg
  kh-ub
  ta-co
  de-co
  tc-td
  tb-wq
  wh-td
  ta-ka
  td-qp
  aq-cg
  wq-ub
  ub-vc
  de-ta
  wq-aq
  wq-vc
  wh-yn
  ka-de
  kh-ta
  co-tc
  wh-qp
  tb-vc
  td-yn
  Each line of text in the network map represents a single connection; the line kh-tc represents a connection between the computer named kh and the computer named tc. Connections aren't directional; tc-kh would mean exactly the same thing.
  
  LAN parties typically involve multiplayer games, so maybe you can locate it by finding groups of connected computers. Start by looking for sets of three computers where each computer in the set is connected to the other two computers.
  
  In this example, there are 12 such sets of three inter-connected computers:
  
  aq,cg,yn
  aq,vc,wq
  co,de,ka
  co,de,ta
  co,ka,ta
  de,ka,ta
  kh,qp,ub
  qp,td,wh
  tb,vc,wq
  tc,td,wh
  td,wh,yn
  ub,vc,wq
  If the Chief Historian is here, and he's at the LAN party, it would be best to know that right away. You're pretty sure his computer's name starts with t, so consider only sets of three computers where at least one computer's name starts with t. That narrows the list down to 7 sets of three inter-connected computers:
  
  co,de,ta
  co,ka,ta
  de,ka,ta
  qp,td,wh
  tb,vc,wq
  tc,td,wh
  td,wh,yn
  Find all the sets of three inter-connected computers. How many contain at least one computer with a name that starts with t?
solution: import argparse
  import re
  from collections import defaultdict
  from typing import Iterable
  
  
  class Network:
      completed_networks: defaultdict[int, set[tuple[str, ...]]]
      nodes: defaultdict[str, set[str]]
  
      def __init__(self, text: str):
          cache: defaultdict[str, set[str]] = defaultdict(set)
          for m in re.finditer(r'(..)-(..)', text):
              a = m.group(1)
              b = m.group(2)
              cache[a].add(b)
              cache[b].add(a)
          self.nodes = cache
          self.completed_networks = defaultdict(set)
  
      def get_names(self):
          return self.nodes.keys()
  
      def get_largest_networks(self):
          largest_network_length = max(*self.completed_networks.keys())
          return list(self.completed_networks[largest_network_length])
  
      def get_complete_network_with_n_nodes(self, n: int):
          return list(self.completed_networks[n])
  
      def build_network_for(self, names: Iterable[str]):
          networks = self.completed_networks
          for name in names:
              name_pool = set(self.nodes[name])
  
              depth = 1
              work: set[tuple[str, ...]] = {(name,)}
              while work:
                  next_work = set()
                  for network in work:
                      # Don't bother with already explored networks
                      if network in networks[depth]: continue
                      networks[depth].add(network)
  
                      for next_name in name_pool.difference(network):
                          if self.nodes[next_name].issuperset(network):
                              next_work.add(tuple(sorted({*network, next_name})))
                  depth += 1
                  work = next_work
  
  
  def solve(input_path: str, /, **_kwargs):
      with open(input_path, "r", encoding='utf-8') as file:
          input_text = file.read().strip().replace('\r', '')
  
      network = Network(input_text)
  
      names_with_t = [name for name in network.get_names() if name.startswith('t')]
      network.build_network_for(names_with_t)
      p1 = len(network.get_complete_network_with_n_nodes(3))
      print(f'p1 = {p1}')
  
      largest_networks = network.get_largest_networks()
      assert len(largest_networks) == 1, "p2 solution should have a single, unique answer"
  
      p2 = ','.join(largest_networks[0])
      print(f'p2 = {p2}')
  
  
  def main():
      parser = argparse.ArgumentParser()
      parser.add_argument("input", nargs="?", default="sample.txt")
      parser.add_argument("-v", "--verbose", action="store_true")
      args = parser.parse_args()
      solve(args.input, verbose=args.verbose)
  
  
  if __name__ == "__main__":
      main()
  