|
@@ -0,0 +1,39 @@
|
|
1
|
+#!/usr/bin/python
|
|
2
|
+
|
|
3
|
+import operator
|
|
4
|
+import functools
|
|
5
|
+from collections import defaultdict
|
|
6
|
+
|
|
7
|
+PART_TWO = True
|
|
8
|
+
|
|
9
|
+ingredients = []
|
|
10
|
+
|
|
11
|
+with open('15.txt', 'r') as file:
|
|
12
|
+ for line in file.readlines():
|
|
13
|
+ _, details = line.split(': ')
|
|
14
|
+ ingredients.append({p: int(v) for p, v in (x.split(' ') for x in details.split(', '))})
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+def sums(n, target):
|
|
18
|
+ if n == 1:
|
|
19
|
+ yield [target]
|
|
20
|
+ else:
|
|
21
|
+ for i in xrange(target + 1):
|
|
22
|
+ for j in sums(n - 1, target - i):
|
|
23
|
+ yield [i] + j
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+def score(amounts):
|
|
27
|
+ properties = defaultdict(int)
|
|
28
|
+
|
|
29
|
+ for i, amount in enumerate(amounts):
|
|
30
|
+ for k, v in ingredients[i].iteritems():
|
|
31
|
+ properties[k] += v * amount
|
|
32
|
+
|
|
33
|
+ if properties.pop('calories') != 500 and PART_TWO:
|
|
34
|
+ return -1
|
|
35
|
+
|
|
36
|
+ return functools.reduce(operator.mul, (max(0, v) for v in properties.values()))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+print(max(map(score, sums(len(ingredients), 100))))
|