day 7, easy recursion
This commit is contained in:
parent
ee0c78709e
commit
8b8d0ee58b
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue