From 8bf07308ff41e1004ef6e4566e7fcef04d62bc53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Tue, 2 Dec 2025 18:55:50 +0100 Subject: [PATCH] Solve 2025 day 2 pt 1-2 I tried to solve it without regexp at first, failed brutally. Cut the line count by 80% using a regexp instead. It was also funny to get all square roots. --- 2025-python/output/day_02.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2025-python/output/day_02.py diff --git a/2025-python/output/day_02.py b/2025-python/output/day_02.py new file mode 100644 index 0000000..909bb95 --- /dev/null +++ b/2025-python/output/day_02.py @@ -0,0 +1,35 @@ +import re + +from output import ints + + +def solve(data): + p1 = set() + p2 = set() + R = re.compile(r"^(\w+)\1+$") + for line in data.split(","): + a, b = ints(line) + for n in range(a, b + 1): + s = str(n) + ls = len(s) + for seq in re.findall(R, s): + sqrts = [i for i in range(1, ls + 1) if ls % i == 0] + for t in sqrts: + if "".join([seq] * t) == s: + if t == 2: + p1.add(n) + p2.add(n) + return sum(p1), sum(p2) + + +if __name__ == "__main__": + with open("./input/02.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2) + + assert p1 == 38437576669 + assert p2 == 49046150754