Advent of Code 2015 day 17 part 1&2: solved

This commit is contained in:
Fabian Tessmer 2025-04-01 13:58:41 +02:00
parent b5d7381f62
commit b5a25fa789
3 changed files with 88 additions and 0 deletions

20
2015/day17/input.txt Normal file
View File

@ -0,0 +1,20 @@
33
14
18
20
45
35
16
35
1
13
18
13
50
44
48
6
24
41
30
42

23
2015/day17/problem.txt Normal file
View File

@ -0,0 +1,23 @@
--- Day 17: No Such Thing as Too Much ---
The elves bought too much eggnog again - 150 liters this time. To fit it all into your refrigerator, you'll need to move it into smaller containers. You take an inventory of the capacities of the available containers.
For example, suppose you have containers of size 20, 15, 10, 5, and 5 liters. If you need to store 25 liters, there are four ways to do it:
15 and 10
20 and 5 (the first 5)
20 and 5 (the second 5)
15, 5, and 5
Filling all containers entirely, how many different combinations of containers can exactly fit all 150 liters of eggnog?
Your puzzle answer was 1304.
--- Part Two ---
While playing with all the containers in the kitchen, another load of eggnog arrives! The shipping and receiving department is requesting as many containers as you can spare.
Find the minimum number of containers that can exactly fit all 150 liters of eggnog. How many different ways can you fill that number of containers and still hold exactly 150 litres?
In the example above, the minimum number of containers was two. There were three ways to use that many containers, and so the answer there would be 3.
Your puzzle answer was 18.
Both parts of this puzzle are complete! They provide two gold stars: **

45
2015/day17/solution.py Normal file
View File

@ -0,0 +1,45 @@
from itertools import count
def get_all_possible_combinations(total_volume: int, cups: list[int]) -> list[list[int]]:
possible_combinations = []
if len(cups) == 0:
return [[]]
if total_volume <= 0:
return [[]]
for i, cup in enumerate(cups):
new_cups = cups.copy()
new_cups = new_cups[i + 1:]
for p in get_all_possible_combinations(total_volume - cup, new_cups):
p = list(p)
p.insert(0, cup)
possible_combinations.append(p)
filtered_list = []
for p in possible_combinations:
calculated_volume = 0
for cup in p:
calculated_volume += cup
if calculated_volume == total_volume:
filtered_list.append(p)
return filtered_list
def get_all_possible_smallest_combinations(total_volume: int, cups: list[int]) -> int:
combinations = get_all_possible_combinations(total_volume, cups)
lengths = [len(i) for i in combinations]
min_cups = min(lengths)
min_length_count = lengths.count(min_cups)
return min_length_count
if __name__ == '__main__':
test_cups = [20, 15, 10, 5, 5]
assert len(get_all_possible_combinations(25, test_cups)) == 4, "Error: Example failed"
print("All test passed")
puzzle_input = open("input.txt", "r").read().splitlines()
puzzle_input = list(map(int, puzzle_input))
print("solution: ", len(get_all_possible_combinations(150, puzzle_input)))
print("Part2: ")
print("solution: ", get_all_possible_smallest_combinations(150, puzzle_input))