From 8b8d0ee58b53b1069a78e3752fa25ca432d35a98 Mon Sep 17 00:00:00 2001 From: setop Date: Sat, 7 Dec 2024 13:06:18 +0100 Subject: [PATCH] day 7, easy recursion --- d07/run.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 d07/run.py diff --git a/d07/run.py b/d07/run.py new file mode 100644 index 0000000..e534a68 --- /dev/null +++ b/d07/run.py @@ -0,0 +1,30 @@ +import sys +from math import * + +PART2 = len(sys.argv)>1 and sys.argv[1] == "2" +I = sys.stdin.read().strip().split("\n") + +def conccat(a, b): + # 12 345 become 12345 + # return a * pow(10, ceil(log10(b))) + b <= breaksfor 1000 + # but int <-> str convertions are slow (x4) + return int(str(a)+str(b)) + +def gen_nb(L:list[int]): + if len(L) == 1: + yield L[0] + else: + for x in gen_nb(L[:-1]): + yield L[-1] * x + yield L[-1] + x + if PART2: + yield conccat(x, L[-1]) + +S = 0 +for l in I: + r, nb = l.split(":") + x = int(r) + nb = list(map(int,nb.strip().split(" "))) + S += x * any(x == a for a in gen_nb(nb)) +print(S) +