From de96ab0e259621a4c7cfaee42baa64b7cd5a5555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 6 Jan 2024 15:11:47 +0100 Subject: [PATCH] 2015 day 15. --- src/holt59/aoc/2015/day15.py | 57 +++++++++++++++++++++ src/holt59/aoc/inputs/holt59/2015/day15.txt | 4 ++ src/holt59/aoc/inputs/tests/2015/day15.txt | 2 + 3 files changed, 63 insertions(+) create mode 100644 src/holt59/aoc/2015/day15.py create mode 100644 src/holt59/aoc/inputs/holt59/2015/day15.txt create mode 100644 src/holt59/aoc/inputs/tests/2015/day15.txt diff --git a/src/holt59/aoc/2015/day15.py b/src/holt59/aoc/2015/day15.py new file mode 100644 index 0000000..953dabf --- /dev/null +++ b/src/holt59/aoc/2015/day15.py @@ -0,0 +1,57 @@ +import math +import sys +from typing import Sequence, cast + +import parse # type: ignore + + +def score(ingredients: list[list[int]], teaspoons: Sequence[int]) -> int: + return math.prod( + max( + 0, + sum( + ingredient[prop] * teaspoon + for ingredient, teaspoon in zip(ingredients, teaspoons) + ), + ) + for prop in range(len(ingredients[0]) - 1) + ) + + +lines = sys.stdin.read().splitlines() + +ingredients: list[list[int]] = [] +for line in lines: + _, *scores = cast( + tuple[str, int, int, int, int, int], + parse.parse( # type: ignore + "{}: capacity {:d}, durability {:d}, flavor {:d}, " + "texture {:d}, calories {:d}", + line, + ), + ) + ingredients.append(scores) + +total_teaspoons = 100 +calories: list[int] = [] +scores: list[int] = [] + +for a in range(total_teaspoons + 1): + for b in range(total_teaspoons + 1 - a): + for c in range(total_teaspoons + 1 - a - b): + teaspoons = (a, b, c, total_teaspoons - a - b - c) + + scores.append(score(ingredients, teaspoons)) + calories.append( + sum( + ingredient[-1] * teaspoon + for ingredient, teaspoon in zip(ingredients, teaspoons) + ) + ) + + +answer_1 = max(scores) +print(f"answer 1 is {answer_1}") + +answer_2 = max(score for score, calory in zip(scores, calories) if calory == 500) +print(f"answer 2 is {answer_2}") diff --git a/src/holt59/aoc/inputs/holt59/2015/day15.txt b/src/holt59/aoc/inputs/holt59/2015/day15.txt new file mode 100644 index 0000000..d1af06e --- /dev/null +++ b/src/holt59/aoc/inputs/holt59/2015/day15.txt @@ -0,0 +1,4 @@ +Sugar: capacity 3, durability 0, flavor 0, texture -3, calories 2 +Sprinkles: capacity -3, durability 3, flavor 0, texture 0, calories 9 +Candy: capacity -1, durability 0, flavor 4, texture 0, calories 1 +Chocolate: capacity 0, durability 0, flavor -2, texture 2, calories 8 diff --git a/src/holt59/aoc/inputs/tests/2015/day15.txt b/src/holt59/aoc/inputs/tests/2015/day15.txt new file mode 100644 index 0000000..1a7ff25 --- /dev/null +++ b/src/holt59/aoc/inputs/tests/2015/day15.txt @@ -0,0 +1,2 @@ +Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8 +Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3