From 2c2379788d9d2b566a418bd19ca5f10861686ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Thu, 2 Jan 2025 22:57:41 +0100 Subject: [PATCH] Solve 2024:23 p1-2 "LAN Party" Part 1 uses the excellent itertools.combinations(). Part 2 introduces Bron-Kerbosch algorithm to the util library! --- 2024-python/aoc.py | 24 ++++++--------------- 2024-python/output/__init__.py | 11 +++++++++- 2024-python/output/day_23.py | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 2024-python/output/day_23.py diff --git a/2024-python/aoc.py b/2024-python/aoc.py index 05e8ceb..11e1df3 100644 --- a/2024-python/aoc.py +++ b/2024-python/aoc.py @@ -15,19 +15,12 @@ except ValueError: day_no = None name = None -print( - f"\n\033[95m\033[1mAdvent of Code {year}\033[0m" - "\n###################" - "\n\n\033[96mby Anders Englöf Ytterström\033[0m" -) - Path("./input").mkdir(parents=True, exist_ok=True) Path("./output").mkdir(parents=True, exist_ok=True) if day_no and name: name = " ".join(name) padded_no = day_no.zfill(2) - print(f"\n- creating output/day_{padded_no}.py") with open("output/day_{}.py".format(padded_no), "w") as s: s.write( f""" @@ -75,19 +68,14 @@ if __name__ == "__main__": """.strip() + "\n" ) - print( - f""" -Done! start coding. - -Puzzle link: -https://adventofcode.com/{year}/day/{day_no} - -Puzzle input (copy and paste to input/{day_no.zfill(2)}.txt): -https://adventofcode.com/{year}/day/{day_no}/input - """ - ) exit(0) +print( + f"\n\033[95m\033[1mAdvent of Code {year}\033[0m" + "\n###################" + "\n\n\033[96mby Anders Englöf Ytterström\033[0m" +) + stars = 0 for i in [str(n).zfill(2) for n in range(1, 26)]: diff --git a/2024-python/output/__init__.py b/2024-python/output/__init__.py index 22da24f..be90eb8 100644 --- a/2024-python/output/__init__.py +++ b/2024-python/output/__init__.py @@ -1,4 +1,3 @@ -import functools import re # Directions/Adjacents for 2D matrices, in the order UP, RIGHT, DOWN, LEFT @@ -177,3 +176,13 @@ def dijkstras(grid, start, target): heapq.heappush(queue, ("stuffs")) return -1 + + +def bk(graph, p, r=set(), x=set()): + """Bron-Kerbosch algoritm, no pivot: https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm""" + if not p and not x: + yield r + while p: + v = p.pop() + yield from bk(graph, p & set(graph[v]), r | {v}, x & graph[v]) + x.add(v) diff --git a/2024-python/output/day_23.py b/2024-python/output/day_23.py new file mode 100644 index 0000000..536a025 --- /dev/null +++ b/2024-python/output/day_23.py @@ -0,0 +1,38 @@ +from collections import defaultdict +from itertools import combinations + +from output import bk + + +def solve(data): + graph = defaultdict(set) + for line in data.splitlines(): + a, b = line.strip().split("-") + graph[a].add(b) + graph[b].add(a) + triplets_connected = sum( + ( + a in graph[b] + and a in graph[c] + and b in graph[a] + and b in graph[c] + and c in graph[a] + and a in graph[b] + and any(x.startswith("t") for x in [a, b, c]) + ) + for a, b, c in combinations(graph.keys(), r=3) + ) + cliques = bk(graph, set(graph.keys())) + max_clique = sorted(cliques, key=lambda i: len(i), reverse=True)[0] + lan_password = ",".join(sorted(list(max_clique))) + return triplets_connected, lan_password + + +if __name__ == "__main__": + with open("./input/23.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2)