From da61e91642966ebdacdc5a86246c799f0a2894d3 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Tue, 7 Jan 2025 22:12:47 +0100 Subject: [PATCH] Day 23 --- day23.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 day23.py diff --git a/day23.py b/day23.py new file mode 100644 index 0000000..6699b0e --- /dev/null +++ b/day23.py @@ -0,0 +1,70 @@ +graph = [[0 for _ in range(26**2)] for _ in range(26**2)] + +edges = [] + +with open("day23") as f: + for l in f: + (a,b) = l.strip().split('-') + x = (ord(a[0])-ord('a'))*26 + (ord(a[1])-ord('a')) + y = (ord(b[0])-ord('a'))*26 + (ord(b[1])-ord('a')) + + graph[x][y] = 1 + graph[y][x] = 1 + + edges.append((x,y)) + +components = [] +for edge in edges: + a = edge[0] + b = edge[1] + for c, e in enumerate(graph[b]): + if e == 1 and graph[c][a] == 1: + components.append((a,b,c)) + +t_components = set() +for component in components: + for c in component: + if c >= 494 and c<520: + t_components.add(tuple(sorted(component))) + +print(len(t_components)) + + +def find_component(start): + component = [start[0],start[1]] + + changed = True + while changed: + changed = False + a = component[-1] + + candidates = [] + for b, e in enumerate(graph[a]): + if e == 1: candidates.append(b) + + for candidate in candidates: + for comp in component: + if graph[comp][candidate] != 1: break + else: + component.append(candidate) + changed = True + return component + +components = [] +for edge in edges: + components.append(find_component(edge)) + +max_len = 0 +max_idx = -1 +for idx, c in enumerate(components): + if len(c) > max_len: + max_len = len(c) + max_idx = idx + +max_component = components[max_idx] +max_component_alpha = [] +for mc in max_component: + a = chr(ord('a')+(mc//26)) + b = chr(ord('a')+(mc%26)) + max_component_alpha.append(a+b) +print(','.join(sorted(max_component_alpha)))