Files
python-aoc-2020/day01/part2.py
Sebastian Seedorf f6974a7ba3 Day 1 (glamorized)
2020-12-19 00:56:03 +01:00

31 lines
780 B
Python

import operator
from functools import reduce
items = (int(x.strip()) for x in open("input.txt"))
values = {(2020, 3)}
tree = dict()
def tree_up(idx, step=1, offset=0):
nxt = tree[(idx, step)] if (idx, step) in tree else None
appendix = [] if nxt is None else tree_up(nxt+idx, step=step+1, offset=idx)
return [idx-offset] + appendix
def prod(iterable):
return reduce(operator.mul, iterable, 1)
for item in items:
tmp_values = set(values)
for elem in values:
new = (elem[0] - item, elem[1]-1)
if new == (0, 0):
vals = tree_up(item)
print(prod(vals))
elif new[0] > 0 and new[1] > 0:
if new not in tree:
tree[new] = item
tmp_values.add(new)
values = tmp_values