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",
43 "execution_count": 36,
49 "pi15 = ['Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8',\n",
50 "'Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3']"
63 "{'Butterscotch': {'calories': 6,\n",
65 " 'durability': 0,\n",
68 " 'Candy': {'calories': 8,\n",
70 " 'durability': 5,\n",
73 " 'Frosting': {'calories': 5,\n",
75 " 'durability': -2,\n",
78 " 'Sugar': {'calories': 1,\n",
80 " 'durability': 0,\n",
87 "output_type": "execute_result"
93 " ls = l.split(': ')\n",
95 " props = ls[1].split(', ')\n",
98 " ps = p.split(' ')\n",
99 " properties[ps[0].strip()] = int(ps[1].strip())\n",
100 " ingredients[name] = properties\n",
106 "execution_count": 6,
112 "def score(recipe, ingredients):\n",
113 " property_scores = defaultdict(int)\n",
114 " for ingredient, quantity in recipe:\n",
115 " for p in ingredients[ingredient]:\n",
116 " property_scores[p] += ingredients[ingredient][p] * quantity\n",
118 " for p in property_scores:\n",
119 " if p != 'calories':\n",
120 " total *= max(property_scores[p], 0)\n",
126 "execution_count": 12,
157 "execution_count": 12,
159 "output_type": "execute_result"
164 "list((a,b,c,(capacity-(a+b+c))) for (a,b,c) in filter(lambda t: sum(t) <= capacity,\n",
165 " itertools.product(range(capacity+1), range(capacity+1), \n",
166 " range(capacity+1))))"
171 "execution_count": 13,
202 "execution_count": 13,
204 "output_type": "execute_result"
209 "list(filter(lambda t: sum(t) == capacity,\n",
210 " itertools.product(range(capacity+1), range(capacity+1), \n",
211 " range(capacity+1), range(capacity+1))))"
216 "execution_count": 7,
222 "def recipe(t, capacity):\n",
223 " return [('Frosting', t[0]), ('Butterscotch', t[1]), ('Candy', t[2]), ('Sugar', capacity-sum(t))]"
228 "execution_count": 32,
240 "execution_count": 32,
242 "output_type": "execute_result"
247 "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', capacity-(f+b+c))], ingredients) \n",
248 " for b, c, f in filter(lambda t: sum(t) <= capacity,\n",
249 " itertools.product(range(capacity+1), range(capacity+1), \n",
250 " range(capacity+1))))"
255 "execution_count": 8,
267 "execution_count": 8,
269 "output_type": "execute_result"
274 "max(score(recipe(t, capacity), ingredients) \n",
275 " for t in filter(lambda t: sum(t) <= capacity,\n",
276 " itertools.product(range(capacity+1), range(capacity+1), \n",
277 " range(capacity+1))))"
282 "execution_count": 38,
294 "execution_count": 38,
296 "output_type": "execute_result"
301 "max(score([('Cinnamon', c), ('Butterscotch', (capacity-c))], ingredients) \n",
302 " for (c, ) in filter(lambda t: calories([('Cinnamon', t[0]), ('Butterscotch', (capacity-t[0]))], ingredients) == 500,\n",
303 " filter(lambda t: sum(t) <= capacity,\n",
304 " itertools.product(range(capacity+1)))))"
309 "execution_count": 28,
317 "(defaultdict(<class 'int'>, {'flavor': 152, 'texture': 76, 'capacity': 68, 'calories': 520, 'durability': 80}),\n",
321 "execution_count": 28,
323 "output_type": "execute_result"
327 "score([('Cinnamon', 56), ('Butterscotch', 44)], ingredients)"
332 "execution_count": 10,
338 "def calories(recipe, ingredients):\n",
339 " return sum(ingredients[i]['calories'] * quantity for i, quantity in recipe)"
344 "execution_count": 34,
355 "execution_count": 34,
357 "output_type": "execute_result"
361 "calories([('Sugar', 56), ('Butterscotch', 44)], ingredients)"
366 "execution_count": 35,
377 "execution_count": 35,
379 "output_type": "execute_result"
388 "execution_count": 11,
400 "execution_count": 11,
402 "output_type": "execute_result"
407 "max(score(recipe(t, capacity), ingredients) \n",
408 " for t in filter(lambda t: calories(recipe(t, capacity), ingredients) == 500,\n",
409 " filter(lambda t: sum(t) <= capacity,\n",
410 " itertools.product(range(capacity+1), range(capacity+1), \n",
411 " range(capacity+1)))))"
416 "execution_count": null,
426 "display_name": "Python 3",
427 "language": "python",
435 "file_extension": ".py",
436 "mimetype": "text/x-python",
438 "nbconvert_exporter": "python",
439 "pygments_lexer": "ipython3",