diff --git a/.idea/misc.xml b/.idea/misc.xml index f8cc37f..35686ab 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/day01/part2.py b/day01/part2.py index 8f2c0b9..bacb945 100644 --- a/day01/part2.py +++ b/day01/part2.py @@ -1,14 +1,33 @@ import math -from itertools import combinations -items = [int(x.strip()) for x in open("input.txt")] +items = (int(x.strip()) for x in open("input.txt")) +values = {(2020, 3)} +tree = dict() -COUNT = 3 -for vals in combinations(items, COUNT): - if sum(vals) == 2020: - print("Found [{}]: Sum={}, Product={}".format( - ", ".join(map(str, vals)), - sum(vals), - math.prod(vals) - )) +def tree_up(idx, step=1, offset = 0): + print(idx, step) + next = tree[(idx, step)] if (idx, step) in tree else None + appendix = [] if next is None else tree_up(next+idx, step=step+1, offset=idx) + return [idx - offset] + appendix + + +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("Found [{}]: Sum={}, Product={}".format( + ", ".join(map(str, vals)), + sum(vals), + math.prod(vals) + )) + exit(0) + elif new[0] < 0 or new[1] < 0: + # target value too low or to many sums + continue + else: + tree[new] = item + tmp_values.add(new) + values = tmp_values diff --git a/python-aoc-2020.iml b/python-aoc-2020.iml index 1e36f4d..ec6b888 100644 --- a/python-aoc-2020.iml +++ b/python-aoc-2020.iml @@ -2,7 +2,7 @@ - + @@ -11,6 +11,6 @@ - + \ No newline at end of file