diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-11-26 23:51:59 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-11-26 23:51:59 +0000 |
commit | deb9cd82db7f5b6a44f613feb0959110ce70c2e6 (patch) | |
tree | de0038e31cb65a8106fe30f4effdbcf883759683 /24 | |
parent | 0e8e49af605d3160645e773fdec53b7cf60cc068 (diff) | |
download | aoc2015-deb9cd82db7f5b6a44f613feb0959110ce70c2e6.tar.gz aoc2015-deb9cd82db7f5b6a44f613feb0959110ce70c2e6.tar.xz aoc2015-deb9cd82db7f5b6a44f613feb0959110ce70c2e6.zip |
solutions
Diffstat (limited to '24')
-rw-r--r-- | 24/solution.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/24/solution.py b/24/solution.py new file mode 100644 index 0000000..6aa9ebe --- /dev/null +++ b/24/solution.py @@ -0,0 +1,43 @@ +from functools import reduce + +def cansum(packages, target): + if target == 0: + return True + if len(packages) == 0: + return False + p = next(iter(packages)) + rest = packages - {p} + if p <= target: + return cansum(rest, target - p) or cansum(rest, target) + else: + return cansum(rest, target) + +def minimize(packages, pile, target, orig_target=None): + if orig_target is None: + orig_target = target + if target == 0 and cansum(packages, orig_target): + return len(pile), reduce(lambda a, b: a * b, pile, 1) + if len(packages) == 0: + return float('inf'), float('inf') + p = next(iter(packages)) + rest = packages - {p} + if p <= target: + return min( + minimize(rest, pile | {p}, target - p, orig_target), + minimize(rest, pile, target, orig_target) + ) + else: + return minimize(rest, pile, target) + +def part1(packages): + return minimize(frozenset(packages), frozenset(), sum(packages) // 3) + +def part2(packages): + return minimize(frozenset(packages), frozenset(), sum(packages) // 4) + +if __name__ == '__main__': + with open('input') as f: + packages = list(int(l.rstrip()) for l in f) + packages.reverse() + print(part1(packages)) + print(part2(packages)) |