Solution to problem 15 in Python

This commit is contained in:
David Doblas Jiménez 2023-09-02 16:55:26 +02:00
parent e4112b79ae
commit 9eeba7ff77

111
src/Year_2015/P15.py Normal file
View File

@ -0,0 +1,111 @@
# --- Day 15: Science for Hungry People ---
# Today, you set out on the task of perfecting your milk-dunking cookie recipe.
# All you have to do is find the right balance of ingredients.
# Your recipe leaves room for exactly 100 teaspoons of ingredients. You make a
# list of the remaining ingredients you could use to finish the recipe (your
# puzzle input) and their properties per teaspoon:
# capacity (how well it helps the cookie absorb milk)
# durability (how well it keeps the cookie intact when full of milk)
# flavor (how tasty it makes the cookie)
# texture (how it improves the feel of the cookie)
# calories (how many calories it adds to the cookie)
# You can only measure ingredients in whole-teaspoon amounts accurately, and you
# have to be accurate so you can reproduce your results in the future. The total
# cookie of a cookie can be found by adding up each of the properties (negative
# totals become 0) and then multiplying together everything except calories.
# For instance, suppose you have these two ingredients:
# Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
# Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3
# Then, choosing to use 44 teaspoons of butterscotch and 56 teaspoons of
# cinnamon (because the amounts of each ingredient must add up to 100) would
# result in a cookie with the following properties:
# A capacity of 44*-1 + 56*2 = 68
# A durability of 44*-2 + 56*3 = 80
# A flavor of 44*6 + 56*-2 = 152
# A texture of 44*3 + 56*-1 = 76
# Multiplying these together (68 * 80 * 152 * 76, ignoring calories for now)
# results in a total cookie of 62842880, which happens to be the best cookie
# possible given these ingredients. If any properties had produced a negative
# total, it would have instead become zero, causing the whole cookie to multiply
# to zero.
# Given the ingredients in your kitchen and their properties, what is the total
# cookie of the highest-scoring cookie you can make?
import re
from functools import reduce
with open("files/P15.txt") as f:
input = [line for line in f.read().strip().split("\n")]
ingredients = []
for ingredient in input:
capacity, durability, flavour, texture, calorie = map(
int, re.findall("-?\d+", ingredient)
)
ingredients.append([capacity, durability, flavour, texture, calorie])
best_cookie = 0
for capacity in range(101):
for durability in range(101 - capacity):
for flavour in range(101 - capacity - durability):
texture = 100 - capacity - durability - flavour
cookie = [
capacity * ingredients[0][p]
+ durability * ingredients[1][p]
+ flavour * ingredients[2][p]
+ texture * ingredients[3][p]
for p in range(len(ingredients[0]))
]
if min(cookie) <= 0:
continue
total = reduce(lambda x, y: x * y, cookie[:-1])
best_cookie = max(total, best_cookie)
print(best_cookie)
# --- Part Two ---
# Your cookie recipe becomes wildly popular! Someone asks if you can make
# another recipe that has exactly 500 calories per cookie (so they can use it as
# a meal replacement). Keep the rest of your award-winning process the same
# (100 teaspoons, same ingredients, same scoring system).
# For example, given the ingredients above, if you had instead selected 40
# teaspoons of butterscotch and 60 teaspoons of cinnamon (which still adds to
# 100), the total calorie count would be 40*8 + 60*3 = 500. The total cookie
# would go down, though: only 57600000, the best you can do in such trying
# circumstances.
# Given the ingredients in your kitchen and their properties, what is the total
# cookie of the highest-scoring cookie you can make with a calorie total of 500?
best_for_calories = 0
for capacity in range(101):
for durability in range(101 - capacity):
for flavour in range(101 - capacity - durability):
texture = 100 - capacity - durability - flavour
cookie = [
capacity * ingredients[0][p]
+ durability * ingredients[1][p]
+ flavour * ingredients[2][p]
+ texture * ingredients[3][p]
for p in range(len(ingredients[0]))
]
if min(cookie) <= 0:
continue
total = reduce(lambda x, y: x * y, cookie[:-1])
if cookie[4] == 500:
best_for_calories = max(total, best_for_calories)
print(best_for_calories)