Day 10 (inline prod)

This commit is contained in:
Sebastian Seedorf
2020-12-10 15:28:30 +01:00
parent be938144bf
commit 06cae78503

View File

@@ -1,6 +1,4 @@
import numpy as np import numpy as np
import operator
from functools import reduce
lines = sorted(int(x.strip()) for x in open("input.txt")) lines = sorted(int(x.strip()) for x in open("input.txt"))
@@ -11,7 +9,7 @@ lines = np.array(lines)
diff = lines[1:] - lines[:-1] diff = lines[1:] - lines[:-1]
one_count = 0 one_count = 0
possibilities = [] possibilities = 1
one_cache = {} one_cache = {}
@@ -25,20 +23,14 @@ def count_one_possibilities(one_cnt: int):
return one_cache[one_cnt] return one_cache[one_cnt]
def prod(iterable):
return reduce(operator.mul, iterable, 1)
# only 3 and 1 differences are available # only 3 and 1 differences are available
# 3 has to be taken / calculate possibilities for 1 hops # 3 has to be taken / calculate possibilities for 1 hops
for num in diff: for num in diff:
if num == 1: if num == 1:
one_count += 1 one_count += 1
elif num == 3: elif num == 3:
factor = count_one_possibilities(one_count) possibilities *= count_one_possibilities(one_count)
if factor > 1:
possibilities.append(factor)
one_count = 0 one_count = 0
print(prod(possibilities)) # 64793042714624 print(possibilities) # 64793042714624
print(one_cache) # {0: 1, 1: 1, 2: 2, 3: 4, 4: 7} print(one_cache) # {0: 1, 1: 1, 2: 2, 3: 4, 4: 7}