November 18, 2023

Solution for Advent of Code 2015 - Day 17: No Such Thing as Too Much

Link to the puzzle text

Part 1

For this puzzle, we have a set of container with different capacities. We need to find the number of container combinations with a total capacity of 150 liters. Since the number of containers is small, I again used a brute force approach. We iterate over all possible container combinations and check if the sum of all capacities is the required amount. If yes, we increase a counter. The iteration over all possible combinations is done by the function itertools.combinations.

total = 0
for size in range(len(container_sizes)):
for subset in itertools.combinations(container_sizes, size):
if sum(subset) == 150:
total += 1
return total

Part 2

For this part, we need to find the number of combinations with minimal amount of containers. This is just a simpler task of part 1, so the same brute force approach is used. We iterate over the container amount and check for the total capacity. Once the number of combinations for a container amount is larger than 0, we have our answer.

for size in range(len(container_sizes)):
num_correct_sizes = 0
for subset in itertools.combinations(container_sizes, size):
if sum(subset) == 150:
num_correct_sizes += 1

if num_correct_sizes > 0:
return num_correct_sizes

 

No comments:

Post a Comment