/ Python / 2015 / 15.py
15.py
 1  from lib import *
 2  
 3  input = read_input(2015, 15)
 4  
 5  TOTAL = 100
 6  
 7  
 8  ingredients = [[int(x.strip(",")) for x in i[2:-1:2]] for i in map(str.split, input.splitlines())]
 9  
10  
11  def solve1(i, x, prev):
12      if i == len(ingredients) - 1:
13          out = 1
14  
15          for p, e in zip(prev, ingredients[-1]):
16              out *= max(0, p + x * e)
17  
18          return out
19  
20      return max(solve1(i + 1, x - j, [a + j * b for a, b in zip(prev, ingredients[i])]) for j in range(x + 1))
21  
22  
23  print(solve1(0, TOTAL, [0] * 4))
24  
25  
26  ingredients = [[int(x.strip(",")) for x in i[2::2]] for i in map(str.split, input.splitlines())]
27  
28  
29  def solve2(i, x, prev):
30      if i == len(ingredients) - 1:
31          if prev[-1] + x * ingredients[-1][-1] != 500:
32              return 0
33  
34          out = 1
35  
36          for p, e in zip(prev[:-1], ingredients[-1][:-1]):
37              out *= max(0, p + x * e)
38  
39          return out
40  
41      return max(solve2(i + 1, x - j, [a + j * b for a, b in zip(prev, ingredients[i])]) for j in range(x + 1))
42  
43  
44  print(solve2(0, TOTAL, [0] * 5))