Day 10
This commit is contained in:
44
day10/part2.py
Normal file
44
day10/part2.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import numpy as np
|
||||
import operator
|
||||
from functools import reduce
|
||||
|
||||
lines = sorted(int(x.strip()) for x in open("input.txt"))
|
||||
|
||||
lines.append(lines[-1]+3)
|
||||
lines.insert(0, 0)
|
||||
lines = np.array(lines)
|
||||
|
||||
diff = lines[1:] - lines[:-1]
|
||||
|
||||
one_count = 0
|
||||
possibilities = []
|
||||
|
||||
one_cache = {}
|
||||
|
||||
|
||||
def count_one_possibilities(one_cnt: int):
|
||||
if one_cnt not in one_cache:
|
||||
if one_cnt <= 1:
|
||||
one_cache[one_cnt] = 1
|
||||
else:
|
||||
one_cache[one_cnt] = sum(count_one_possibilities(less) for less in range(max(0, one_cnt - 3), one_cnt))
|
||||
return one_cache[one_cnt]
|
||||
|
||||
|
||||
def prod(iterable):
|
||||
return reduce(operator.mul, iterable, 1)
|
||||
|
||||
|
||||
# only 3 and 1 differences are available
|
||||
# 3 has to be taken / calculate possibilities for 1 hops
|
||||
for num in diff:
|
||||
if num == 1:
|
||||
one_count += 1
|
||||
elif num == 3:
|
||||
factor = count_one_possibilities(one_count)
|
||||
if factor > 1:
|
||||
possibilities.append(factor)
|
||||
one_count = 0
|
||||
|
||||
print(prod(possibilities)) # 64793042714624
|
||||
print(one_cache) # {0: 1, 1: 1, 2: 2, 3: 4, 4: 7}
|
||||
Reference in New Issue
Block a user