From 50fbc74406bd8ce7be440df494cea061ae7e3973 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Wed, 11 Nov 2026 17:35:22 +0100 Subject: [PATCH] 2015 day 17 --- adventofcode/2015/day17/day17.py | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 adventofcode/2015/day17/day17.py diff --git a/adventofcode/2015/day17/day17.py b/adventofcode/2015/day17/day17.py new file mode 100644 index 0000000..c82522e --- /dev/null +++ b/adventofcode/2015/day17/day17.py @@ -0,0 +1,42 @@ +import fileinput + + +def solve(containers, target=150): + "This is knapsack problem. Count the number of ways to sum to target using a recursive approach and pruning." + containers.sort(reverse=True) + + min_used_containers = [99, 0] # number of containers, number of ways + def rec(i=0, current_sum=0, used=0): + # base case + if i == len(containers): + if current_sum == target: + if used < min_used_containers[0]: + min_used_containers[0] = used + min_used_containers[1] = 1 + elif used == min_used_containers[0]: + min_used_containers[1] += 1 + return 1 + else: + return 0 + n = 0 + # All values are positive, so adding can only increase the sum. If the current sum overshoots, we can prune the branch + if current_sum + containers[i] <= target: + n += rec(i + 1, current_sum + containers[i], used + 1) # pick + n += rec(i + 1, current_sum, used) # don't pick + return n + + # returns the number of ways to sum to target and the number of ways to sum to target using the minimum number of containers + return rec(), min_used_containers[1] + + +def main(inp): + containers = [int(x) for x in inp] + part1, part2 = solve(containers) + print("Part 1: ", part1) + print("Part 2: ", part2) + + +if __name__ == "__main__": + with fileinput.input() as f: + lines = [x.rstrip() for x in f] + main(lines)