Browse Source

Day 15

master
Chris Smith 8 years ago
parent
commit
135bae75e0
2 changed files with 43 additions and 0 deletions
  1. 39
    0
      15.py
  2. 4
    0
      15.txt

+ 39
- 0
15.py View File

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

+ 4
- 0
15.txt View File

@@ -0,0 +1,4 @@
1
+Sprinkles: capacity 5, durability -1, flavor 0, texture 0, calories 5
2
+PeanutButter: capacity -1, durability 3, flavor 0, texture 0, calories 1
3
+Frosting: capacity 0, durability -1, flavor 4, texture 0, calories 6
4
+Sugar: capacity -1, durability 0, flavor 0, texture 2, calories 8

Loading…
Cancel
Save