Move 2015 out of the way.

This commit is contained in:
Bert Peters
2016-12-01 11:25:19 +01:00
parent a75d14ec17
commit b37fd44fa7
50 changed files with 0 additions and 0 deletions

4
2015/day-15/input.txt Normal file
View File

@@ -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

47
2015/day-15/solution.py Normal file
View File

@@ -0,0 +1,47 @@
from __future__ import print_function
import fileinput
import re
from operator import mul
from functools import reduce
pattern = re.compile(r"(\w+): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)")
ingredients = []
calories = []
def computeScore(amounts):
scores = [sum(ingredients[idx][x] * value for idx, value in enumerate(amounts)) for x in range(4)]
return reduce(mul, [max(x, 0) for x in scores], 1)
def computeCalories(amounts):
return sum(a * b for a, b in zip(calories, amounts))
def computeMax(ingredient, budget, currentAmounts):
budgetLeft = budget - sum(currentAmounts[:ingredient])
if ingredient == len(ingredients) - 1:
currentAmounts[ingredient] = budgetLeft
score = computeScore(currentAmounts)
if computeCalories(currentAmounts) == 500:
return score, score
return score, None
results = []
resultsCorrect = [0]
for n in range(budgetLeft + 1):
currentAmounts[ingredient] = n
general, correct = computeMax(ingredient + 1, budget, currentAmounts)
results.append(general)
resultsCorrect.append(correct)
return max(results), max(x for x in resultsCorrect if x is not None)
for line in fileinput.input():
match = pattern.match(line)
ingredients.append([int(x) for x in match.group(2, 3, 4, 5)])
calories.append(int(match.group(6)))
print(computeMax(0, 100, [0] * len(ingredients)))