Advertisement
alexandrajay2002

Advent of Code 2024 day 23 part 1

Dec 23rd, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | Source Code | 0 0
  1. from collections import defaultdict
  2. from sys import argv
  3.  
  4.  
  5. def parse(src):
  6.     for line in src.splitlines():
  7.         if line == '':
  8.             continue
  9.         first, second = line.split('-', 1)
  10.         yield first.strip(), second.strip()
  11.  
  12.  
  13. def make_graph(connections):
  14.     graph = defaultdict(set)
  15.     for first, second in connections:
  16.         graph[first].add(second)
  17.         graph[second].add(first)
  18.     return graph
  19.  
  20.  
  21. def find_triples(graph):
  22.     triples = set()
  23.     nodes = list(graph.keys())
  24.     for index, a in enumerate(graph):
  25.         for b in graph[a]:
  26.             for c in nodes[index+1:]:
  27.                 if {a, b} <= graph[c]:
  28.                     triples.add(frozenset((a, b, c)))
  29.     return triples
  30.  
  31.  
  32. def main(connections):
  33.     total = 0
  34.     graph = make_graph(connections)
  35.     triples = find_triples(graph)
  36.     for triple in triples:
  37.         if any(x[0] == 't' for x in triple):
  38.             total += 1
  39.     return total
  40.  
  41.  
  42. if __name__ == '__main__':
  43.     print(main(parse(open(argv[1]).read())))
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement