1- from ast import arg
21from collections import defaultdict
3- from itertools import combinations , product
2+ from itertools import combinations
43
5- from websockets .sync .client import connect
6-
7- from aoc import main , progress
8- from graph_dyn import (
9- all_shortest_path_lengths ,
10- all_shortest_paths ,
11- shortest_path_length ,
12- )
4+ from aoc import main
135
146
157def parse (s : str ):
@@ -35,102 +27,30 @@ def part1(s: str) -> int:
3527 return count
3628
3729
38- def best_region_orig (
39- nodes : set [str ], distances : dict [tuple [str , str ], int ]
40- ) -> set [str ]:
41- candidates = []
42- branched = False
43- for a , b in product (nodes , nodes ):
44- if a != b and distances [a , b ] > 1 :
45- branched = True
46- candidates .append (best_region (nodes - {a }, distances ))
47- if not branched :
48- return nodes
49- return max (candidates , key = len )
50-
51-
52- def best_region_new (edges , nodes : set [str ]):
30+ def best_region (edges , nodes : set [str ]):
5331 xs = list (nodes )
5432 connected_count = defaultdict (int )
5533 for i , a in enumerate (xs ):
5634 for b in xs [i + 1 :]:
5735 if a in edges [b ]:
5836 connected_count [a ] += 1
5937 connected_count [b ] += 1
60- # print(connected_count)
6138 x = max (connected_count .values ())
6239 best = {n for n , c in connected_count .items () if c == x }
6340 return best if len (best ) == x + 1 else set ()
6441
6542
66- def best_region (edges , nodes : set ) -> set [str ]:
67- candidates = []
68- branched = False
69- xs = list (nodes )
70- for i , a in enumerate (xs ):
71- for b in xs [i + 1 :]:
72- if a not in edges [b ] or b not in edges [a ]:
73- branched = True
74- candidates .append (best_region (edges , nodes - {a }))
75- if not branched :
76- return nodes
77- return max (candidates , key = len )
78-
79-
8043def part2 (s : str ) -> str :
8144 edges = parse (s )
8245 regions = []
83- # distances = all_shortest_path_lengths(edges)
84- # print("wq", best_region_new(edges, edges["wq"]))
85- # print("co", best_region_new(edges, edges["co"]))
86- # return 0
87- for node , neighbors in progress (edges .items ()):
88- regions .append ({node } | best_region_new (edges , neighbors ))
46+ for node , neighbors in edges .items ():
47+ regions .append ({node } | best_region (edges , neighbors ))
8948 region = max (regions , key = len )
9049 return "," .join (sorted (region ))
91- # possible_region = {node} | neighbors
92- # for a, b in product(neighbors, neighbors):
93- # if distances[a, b] > 1:
94- # possible_region.discard(a)
95- # possible_region.discard(b)
96- # print(node, possible_region)
97- # found = True
98- # region = {node}
99- # for n in neighbors:
100- # if not edges[n] >= possible_region:
101- # possible_region.discard(n)
102- # for a, b in product(neighbors, neighbors):
103- # if a == b:
104- # continue
105- # if distances[a, b] != 1:
106- # found = False
107- # if found:
108- # print(node, neighbors)
109- # for
110- # region = {node}
111- # print(neighbors)
112- # for a in neighbors:
113- # if all(a in edges[b] for b in neighbors):
114- # region.add(a)
115- # for a, b in product(neighbors, neighbors):
116- # if a == b:
117- # continue
118- # if node == "ub":
119- # print(" ", a, edges[a])
120- # print(" ", b, edges[b])
121- # if a in edges[b] and b in edges[a]:
122- # region |= {a, b}
123- # if node == "ub":
124- # print(node, region)
125- # if any(n.startswith("t") for n in region):
126- # regions.add(frozenset(region))
127- best = max (regions , key = len )
128- print (best )
12950
13051
13152if __name__ == "__main__" :
13253 main (
13354 part1 ,
13455 part2 ,
135- # isolate=0,
13656 )
0 commit comments