This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_science_for_hungry_people.py
52 lines (39 loc) · 1.71 KB
/
15_science_for_hungry_people.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#############################################
# --- Day 15: Science for Hungry People --- #
#############################################
import AOCUtils
def allTuplesWithSum(n, total):
if n == 1:
yield (total,)
return
for i in range(total+1):
for t in allTuplesWithSum(n-1, total-i):
yield (i,) + t
#############################################
rawIngredients = AOCUtils.loadInput(15)
ingredients = []
for rawIngredient in rawIngredients:
rawIngredient = rawIngredient.split()
ingredient = {"capacity": int(rawIngredient[2][:-1]),
"durability": int(rawIngredient[4][:-1]),
"flavor": int(rawIngredient[6][:-1]),
"texture": int(rawIngredient[8][:-1]),
"calories": int(rawIngredient[10])
}
ingredients.append(ingredient)
recipes = dict()
for amounts in allTuplesWithSum(len(ingredients), 100):
capacity, durability, flavor, texture, calories = 0, 0, 0, 0, 0
for amount, ingredient in zip(amounts, ingredients):
capacity += amount * ingredient["capacity"]
durability += amount * ingredient["durability"]
flavor += amount * ingredient["flavor"]
texture += amount * ingredient["texture"]
calories += amount * ingredient["calories"]
score = max(0, capacity) * max(0, durability) * max(0, flavor) * max(0, texture)
recipes[amounts] = {"score": score, "calories": calories}
maxScore = max(recipe["score"] for amounts, recipe in recipes.items())
print("Part 1: {}".format(maxScore))
maxScore = max(recipe["score"] for amounts, recipe in recipes.items() if recipe["calories"] == 500)
print("Part 2: {}".format(maxScore))
AOCUtils.printTimeTaken()