Fixed typo
[advent-of-code-15.git] / advent15.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 3,
6 "metadata": {
7 "collapsed": true
8 },
9 "outputs": [],
10 "source": [
11 "from collections import defaultdict\n",
12 "import itertools"
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": 4,
18 "metadata": {
19 "collapsed": false
20 },
21 "outputs": [
22 {
23 "data": {
24 "text/plain": [
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']"
29 ]
30 },
31 "execution_count": 4,
32 "metadata": {},
33 "output_type": "execute_result"
34 }
35 ],
36 "source": [
37 "pi15 = [l.strip() for l in open('advent15.txt').readlines()]\n",
38 "pi15"
39 ]
40 },
41 {
42 "cell_type": "code",
43 "execution_count": 36,
44 "metadata": {
45 "collapsed": true
46 },
47 "outputs": [],
48 "source": [
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']"
51 ]
52 },
53 {
54 "cell_type": "code",
55 "execution_count": 5,
56 "metadata": {
57 "collapsed": false
58 },
59 "outputs": [
60 {
61 "data": {
62 "text/plain": [
63 "{'Butterscotch': {'calories': 6,\n",
64 " 'capacity': -1,\n",
65 " 'durability': 0,\n",
66 " 'flavor': 5,\n",
67 " 'texture': 0},\n",
68 " 'Candy': {'calories': 8,\n",
69 " 'capacity': 0,\n",
70 " 'durability': 5,\n",
71 " 'flavor': -1,\n",
72 " 'texture': 0},\n",
73 " 'Frosting': {'calories': 5,\n",
74 " 'capacity': 4,\n",
75 " 'durability': -2,\n",
76 " 'flavor': 0,\n",
77 " 'texture': 0},\n",
78 " 'Sugar': {'calories': 1,\n",
79 " 'capacity': 0,\n",
80 " 'durability': 0,\n",
81 " 'flavor': -2,\n",
82 " 'texture': 2}}"
83 ]
84 },
85 "execution_count": 5,
86 "metadata": {},
87 "output_type": "execute_result"
88 }
89 ],
90 "source": [
91 "ingredients = {}\n",
92 "for l in pi15:\n",
93 " ls = l.split(': ')\n",
94 " name = ls[0]\n",
95 " props = ls[1].split(', ')\n",
96 " properties = {}\n",
97 " for p in props:\n",
98 " ps = p.split(' ')\n",
99 " properties[ps[0].strip()] = int(ps[1].strip())\n",
100 " ingredients[name] = properties\n",
101 "ingredients"
102 ]
103 },
104 {
105 "cell_type": "code",
106 "execution_count": 6,
107 "metadata": {
108 "collapsed": false
109 },
110 "outputs": [],
111 "source": [
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",
117 " total = 1\n",
118 " for p in property_scores:\n",
119 " if p != 'calories':\n",
120 " total *= max(property_scores[p], 0)\n",
121 " return total"
122 ]
123 },
124 {
125 "cell_type": "code",
126 "execution_count": 12,
127 "metadata": {
128 "collapsed": false,
129 "scrolled": true
130 },
131 "outputs": [
132 {
133 "data": {
134 "text/plain": [
135 "[(0, 0, 0, 3),\n",
136 " (0, 0, 1, 2),\n",
137 " (0, 0, 2, 1),\n",
138 " (0, 0, 3, 0),\n",
139 " (0, 1, 0, 2),\n",
140 " (0, 1, 1, 1),\n",
141 " (0, 1, 2, 0),\n",
142 " (0, 2, 0, 1),\n",
143 " (0, 2, 1, 0),\n",
144 " (0, 3, 0, 0),\n",
145 " (1, 0, 0, 2),\n",
146 " (1, 0, 1, 1),\n",
147 " (1, 0, 2, 0),\n",
148 " (1, 1, 0, 1),\n",
149 " (1, 1, 1, 0),\n",
150 " (1, 2, 0, 0),\n",
151 " (2, 0, 0, 1),\n",
152 " (2, 0, 1, 0),\n",
153 " (2, 1, 0, 0),\n",
154 " (3, 0, 0, 0)]"
155 ]
156 },
157 "execution_count": 12,
158 "metadata": {},
159 "output_type": "execute_result"
160 }
161 ],
162 "source": [
163 "capacity = 3\n",
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))))"
167 ]
168 },
169 {
170 "cell_type": "code",
171 "execution_count": 13,
172 "metadata": {
173 "collapsed": false,
174 "scrolled": true
175 },
176 "outputs": [
177 {
178 "data": {
179 "text/plain": [
180 "[(0, 0, 0, 3),\n",
181 " (0, 0, 1, 2),\n",
182 " (0, 0, 2, 1),\n",
183 " (0, 0, 3, 0),\n",
184 " (0, 1, 0, 2),\n",
185 " (0, 1, 1, 1),\n",
186 " (0, 1, 2, 0),\n",
187 " (0, 2, 0, 1),\n",
188 " (0, 2, 1, 0),\n",
189 " (0, 3, 0, 0),\n",
190 " (1, 0, 0, 2),\n",
191 " (1, 0, 1, 1),\n",
192 " (1, 0, 2, 0),\n",
193 " (1, 1, 0, 1),\n",
194 " (1, 1, 1, 0),\n",
195 " (1, 2, 0, 0),\n",
196 " (2, 0, 0, 1),\n",
197 " (2, 0, 1, 0),\n",
198 " (2, 1, 0, 0),\n",
199 " (3, 0, 0, 0)]"
200 ]
201 },
202 "execution_count": 13,
203 "metadata": {},
204 "output_type": "execute_result"
205 }
206 ],
207 "source": [
208 "capacity = 3\n",
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))))"
212 ]
213 },
214 {
215 "cell_type": "code",
216 "execution_count": 7,
217 "metadata": {
218 "collapsed": true
219 },
220 "outputs": [],
221 "source": [
222 "def recipe(t, capacity):\n",
223 " return [('Frosting', t[0]), ('Butterscotch', t[1]), ('Candy', t[2]), ('Sugar', capacity-sum(t))]"
224 ]
225 },
226 {
227 "cell_type": "code",
228 "execution_count": 32,
229 "metadata": {
230 "collapsed": false,
231 "scrolled": true
232 },
233 "outputs": [
234 {
235 "data": {
236 "text/plain": [
237 "18965440"
238 ]
239 },
240 "execution_count": 32,
241 "metadata": {},
242 "output_type": "execute_result"
243 }
244 ],
245 "source": [
246 "capacity = 100\n",
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))))"
251 ]
252 },
253 {
254 "cell_type": "code",
255 "execution_count": 8,
256 "metadata": {
257 "collapsed": false,
258 "scrolled": true
259 },
260 "outputs": [
261 {
262 "data": {
263 "text/plain": [
264 "18965440"
265 ]
266 },
267 "execution_count": 8,
268 "metadata": {},
269 "output_type": "execute_result"
270 }
271 ],
272 "source": [
273 "capacity = 100\n",
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))))"
278 ]
279 },
280 {
281 "cell_type": "code",
282 "execution_count": 38,
283 "metadata": {
284 "collapsed": false,
285 "scrolled": true
286 },
287 "outputs": [
288 {
289 "data": {
290 "text/plain": [
291 "57600000"
292 ]
293 },
294 "execution_count": 38,
295 "metadata": {},
296 "output_type": "execute_result"
297 }
298 ],
299 "source": [
300 "capacity = 100\n",
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)))))"
305 ]
306 },
307 {
308 "cell_type": "code",
309 "execution_count": 28,
310 "metadata": {
311 "collapsed": false
312 },
313 "outputs": [
314 {
315 "data": {
316 "text/plain": [
317 "(defaultdict(<class 'int'>, {'flavor': 152, 'texture': 76, 'capacity': 68, 'calories': 520, 'durability': 80}),\n",
318 " 62842880)"
319 ]
320 },
321 "execution_count": 28,
322 "metadata": {},
323 "output_type": "execute_result"
324 }
325 ],
326 "source": [
327 "score([('Cinnamon', 56), ('Butterscotch', 44)], ingredients)"
328 ]
329 },
330 {
331 "cell_type": "code",
332 "execution_count": 10,
333 "metadata": {
334 "collapsed": false
335 },
336 "outputs": [],
337 "source": [
338 "def calories(recipe, ingredients):\n",
339 " return sum(ingredients[i]['calories'] * quantity for i, quantity in recipe)"
340 ]
341 },
342 {
343 "cell_type": "code",
344 "execution_count": 34,
345 "metadata": {
346 "collapsed": false
347 },
348 "outputs": [
349 {
350 "data": {
351 "text/plain": [
352 "320"
353 ]
354 },
355 "execution_count": 34,
356 "metadata": {},
357 "output_type": "execute_result"
358 }
359 ],
360 "source": [
361 "calories([('Sugar', 56), ('Butterscotch', 44)], ingredients)"
362 ]
363 },
364 {
365 "cell_type": "code",
366 "execution_count": 35,
367 "metadata": {
368 "collapsed": false
369 },
370 "outputs": [
371 {
372 "data": {
373 "text/plain": [
374 "320"
375 ]
376 },
377 "execution_count": 35,
378 "metadata": {},
379 "output_type": "execute_result"
380 }
381 ],
382 "source": [
383 "56*1 + 44*6"
384 ]
385 },
386 {
387 "cell_type": "code",
388 "execution_count": 11,
389 "metadata": {
390 "collapsed": false,
391 "scrolled": true
392 },
393 "outputs": [
394 {
395 "data": {
396 "text/plain": [
397 "15862900"
398 ]
399 },
400 "execution_count": 11,
401 "metadata": {},
402 "output_type": "execute_result"
403 }
404 ],
405 "source": [
406 "capacity = 100\n",
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)))))"
412 ]
413 },
414 {
415 "cell_type": "code",
416 "execution_count": null,
417 "metadata": {
418 "collapsed": true
419 },
420 "outputs": [],
421 "source": []
422 }
423 ],
424 "metadata": {
425 "kernelspec": {
426 "display_name": "Python 3",
427 "language": "python",
428 "name": "python3"
429 },
430 "language_info": {
431 "codemirror_mode": {
432 "name": "ipython",
433 "version": 3
434 },
435 "file_extension": ".py",
436 "mimetype": "text/x-python",
437 "name": "python",
438 "nbconvert_exporter": "python",
439 "pygments_lexer": "ipython3",
440 "version": "3.4.3"
441 }
442 },
443 "nbformat": 4,
444 "nbformat_minor": 0
445 }