11 "from collections import defaultdict\n",
25 "['Frosting: capacity 4, durability -2, flavor 0, texture 0, calories 5',\n",
26 " 'Candy: capacity 0, durability 5, flavor -1, texture 0, calories 8',\n",
27 " 'Butterscotch: capacity -1, durability 0, flavor 5, texture 0, calories 6',\n",
28 " 'Sugar: capacity 0, durability 0, flavor -2, texture 2, calories 1']"
33 "output_type": "execute_result"
37 "pi15 = [l.strip() for l in open('advent15.txt').readlines()]\n",
51 "{'Butterscotch': {'calories': 6,\n",
53 " 'durability': 0,\n",
56 " 'Candy': {'calories': 8,\n",
58 " 'durability': 5,\n",
61 " 'Frosting': {'calories': 5,\n",
63 " 'durability': -2,\n",
66 " 'Sugar': {'calories': 1,\n",
68 " 'durability': 0,\n",
75 "output_type": "execute_result"
81 " ls = l.split(': ')\n",
83 " props = ls[1].split(', ')\n",
86 " ps = p.split(' ')\n",
87 " properties[ps[0].strip()] = int(ps[1].strip())\n",
88 " ingredients[name] = properties\n",
94 "execution_count": 33,
100 "def score(recipe, ingredients):\n",
101 " property_scores = defaultdict(int)\n",
102 " for ingredient, quantity in recipe:\n",
103 " for p in ingredients[ingredient]:\n",
104 " property_scores[p] += ingredients[ingredient][p] * quantity\n",
106 " for p in property_scores:\n",
107 " total *= max(property_scores[p], 0)\n",
113 "execution_count": 34,
124 "execution_count": 34,
126 "output_type": "execute_result"
130 "score([('Frosting', 30), ('Butterscotch', 30), ('Candy', 30), ('Sugar', 10)], ingredients)"
135 "execution_count": 31,
166 "execution_count": 31,
168 "output_type": "execute_result"
173 "list(filter(lambda t: sum(t) == capacity,\n",
174 " itertools.product(range(capacity+1), range(capacity+1), \n",
175 " range(capacity+1), range(capacity+1))))"
180 "execution_count": 38,
192 "execution_count": 38,
194 "output_type": "execute_result"
199 "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', s)], ingredients) \n",
200 " for b, c, f, s in filter(lambda t: sum(t) == capacity,\n",
201 " itertools.product(range(capacity+1), range(capacity+1), \n",
202 " range(capacity+1), range(capacity+1))))"
207 "execution_count": 23,
213 "ename": "TypeError",
214 "evalue": "unhashable type: 'slice'",
215 "output_type": "error",
217 "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
218 "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
219 "\u001b[1;32m<ipython-input-23-95769c86abac>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mingredients\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
220 "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'slice'"
228 "execution_count": null,
238 "display_name": "Python 3",
239 "language": "python",
247 "file_extension": ".py",
248 "mimetype": "text/x-python",
250 "nbconvert_exporter": "python",
251 "pygments_lexer": "ipython3",