11from collections import defaultdict
22from itertools import combinations
33
4+ from typing_extensions import Mapping
5+
46from aoc import main
57
8+ type Edges = Mapping [str , set [str ]]
9+
610
7- def parse (s : str ):
11+ def parse (s : str ) -> Edges :
812 edges = defaultdict (set )
913 for line in s .splitlines ():
1014 a , b = line .split ("-" )
@@ -18,23 +22,17 @@ def part1(s: str) -> int:
1822 regions = set ()
1923 for node , neighbors in edges .items ():
2024 for a , b in combinations (neighbors , 2 ):
21- if b in edges [a ] and a in edges [ b ] :
25+ if a in edges [b ] and any ( n . startswith ( "t" ) for n in [ node , a , b ]) :
2226 regions .add (frozenset ([node , a , b ]))
23- count = 0
24- for region in regions :
25- if any (node .startswith ("t" ) for node in region ):
26- count += 1
27- return count
27+ return len (regions )
2828
2929
30- def best_region (edges , nodes : set [str ]):
31- xs = list (nodes )
30+ def best_region (edges : Edges , nodes : set [str ]):
3231 connected_count = defaultdict (int )
33- for i , a in enumerate (xs ):
34- for b in xs [i + 1 :]:
35- if a in edges [b ]:
36- connected_count [a ] += 1
37- connected_count [b ] += 1
32+ for a , b in combinations (nodes , 2 ):
33+ if a in edges [b ]:
34+ connected_count [a ] += 1
35+ connected_count [b ] += 1
3836 x = max (connected_count .values ())
3937 best = {n for n , c in connected_count .items () if c == x }
4038 return best if len (best ) == x + 1 else set ()
0 commit comments