From 5b07e73382bce05c0b2f205c246c5cb2c6eef666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 6 Jan 2024 15:45:54 +0100 Subject: [PATCH] 2015 day 17. --- src/holt59/aoc/2015/day17.py | 30 +++++++++++++++++++++ src/holt59/aoc/inputs/holt59/2015/day17.txt | 20 ++++++++++++++ src/holt59/aoc/inputs/tests/2015/day17.txt | 5 ++++ 3 files changed, 55 insertions(+) create mode 100644 src/holt59/aoc/2015/day17.py create mode 100644 src/holt59/aoc/inputs/holt59/2015/day17.txt create mode 100644 src/holt59/aoc/inputs/tests/2015/day17.txt diff --git a/src/holt59/aoc/2015/day17.py b/src/holt59/aoc/2015/day17.py new file mode 100644 index 0000000..e4704c3 --- /dev/null +++ b/src/holt59/aoc/2015/day17.py @@ -0,0 +1,30 @@ +import sys +from typing import Iterator + + +def iter_combinations(value: int, containers: list[int]) -> Iterator[tuple[int, ...]]: + if value < 0: + return + + if value == 0: + yield () + + for i in range(len(containers)): + for combination in iter_combinations( + value - containers[i], containers[i + 1 :] + ): + yield (containers[i],) + combination + + +containers = [int(c) for c in sys.stdin.read().split()] +total = 25 if len(containers) <= 5 else 150 + +combinations = [combination for combination in iter_combinations(total, containers)] + +answer_1 = len(combinations) +print(f"answer 1 is {answer_1}") + +min_containers = min(len(combination) for combination in combinations) + +answer_2 = sum(1 for combination in combinations if len(combination) == min_containers) +print(f"answer 2 is {answer_2}") diff --git a/src/holt59/aoc/inputs/holt59/2015/day17.txt b/src/holt59/aoc/inputs/holt59/2015/day17.txt new file mode 100644 index 0000000..6b25a72 --- /dev/null +++ b/src/holt59/aoc/inputs/holt59/2015/day17.txt @@ -0,0 +1,20 @@ +33 +14 +18 +20 +45 +35 +16 +35 +1 +13 +18 +13 +50 +44 +48 +6 +24 +41 +30 +42 diff --git a/src/holt59/aoc/inputs/tests/2015/day17.txt b/src/holt59/aoc/inputs/tests/2015/day17.txt new file mode 100644 index 0000000..77f10f3 --- /dev/null +++ b/src/holt59/aoc/inputs/tests/2015/day17.txt @@ -0,0 +1,5 @@ +20 +15 +10 +5 +5