Done day 15
[advent-of-code-15.git] / advent15.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 6,
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": 39,
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": 39,
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": 40,
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": 40,
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": 31,
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": 32,
217 "metadata": {
218 "collapsed": false,
219 "scrolled": true
220 },
221 "outputs": [
222 {
223 "data": {
224 "text/plain": [
225 "18965440"
226 ]
227 },
228 "execution_count": 32,
229 "metadata": {},
230 "output_type": "execute_result"
231 }
232 ],
233 "source": [
234 "capacity = 100\n",
235 "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', capacity-(f+b+c))], ingredients) \n",
236 " for b, c, f in filter(lambda t: sum(t) <= capacity,\n",
237 " itertools.product(range(capacity+1), range(capacity+1), \n",
238 " range(capacity+1))))"
239 ]
240 },
241 {
242 "cell_type": "code",
243 "execution_count": 38,
244 "metadata": {
245 "collapsed": false,
246 "scrolled": true
247 },
248 "outputs": [
249 {
250 "data": {
251 "text/plain": [
252 "57600000"
253 ]
254 },
255 "execution_count": 38,
256 "metadata": {},
257 "output_type": "execute_result"
258 }
259 ],
260 "source": [
261 "capacity = 100\n",
262 "max(score([('Cinnamon', c), ('Butterscotch', (capacity-c))], ingredients) \n",
263 " for (c, ) in filter(lambda t: calories([('Cinnamon', t[0]), ('Butterscotch', (capacity-t[0]))], ingredients) == 500,\n",
264 " filter(lambda t: sum(t) <= capacity,\n",
265 " itertools.product(range(capacity+1)))))"
266 ]
267 },
268 {
269 "cell_type": "code",
270 "execution_count": 28,
271 "metadata": {
272 "collapsed": false
273 },
274 "outputs": [
275 {
276 "data": {
277 "text/plain": [
278 "(defaultdict(<class 'int'>, {'flavor': 152, 'texture': 76, 'capacity': 68, 'calories': 520, 'durability': 80}),\n",
279 " 62842880)"
280 ]
281 },
282 "execution_count": 28,
283 "metadata": {},
284 "output_type": "execute_result"
285 }
286 ],
287 "source": [
288 "score([('Cinnamon', 56), ('Butterscotch', 44)], ingredients)"
289 ]
290 },
291 {
292 "cell_type": "code",
293 "execution_count": 33,
294 "metadata": {
295 "collapsed": false
296 },
297 "outputs": [],
298 "source": [
299 "def calories(recipe, ingredients):\n",
300 " return sum(ingredients[i]['calories'] * quantity for i, quantity in recipe)"
301 ]
302 },
303 {
304 "cell_type": "code",
305 "execution_count": 34,
306 "metadata": {
307 "collapsed": false
308 },
309 "outputs": [
310 {
311 "data": {
312 "text/plain": [
313 "320"
314 ]
315 },
316 "execution_count": 34,
317 "metadata": {},
318 "output_type": "execute_result"
319 }
320 ],
321 "source": [
322 "calories([('Sugar', 56), ('Butterscotch', 44)], ingredients)"
323 ]
324 },
325 {
326 "cell_type": "code",
327 "execution_count": 35,
328 "metadata": {
329 "collapsed": false
330 },
331 "outputs": [
332 {
333 "data": {
334 "text/plain": [
335 "320"
336 ]
337 },
338 "execution_count": 35,
339 "metadata": {},
340 "output_type": "execute_result"
341 }
342 ],
343 "source": [
344 "56*1 + 44*6"
345 ]
346 },
347 {
348 "cell_type": "code",
349 "execution_count": 42,
350 "metadata": {
351 "collapsed": false,
352 "scrolled": true
353 },
354 "outputs": [
355 {
356 "data": {
357 "text/plain": [
358 "15862900"
359 ]
360 },
361 "execution_count": 42,
362 "metadata": {},
363 "output_type": "execute_result"
364 }
365 ],
366 "source": [
367 "capacity = 100\n",
368 "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', capacity-(f+b+c))], ingredients) \n",
369 " for f, b, c in filter(lambda t: calories([('Frosting', t[0]), ('Butterscotch', t[1]), \n",
370 " ('Candy', t[2]), ('Sugar', capacity-sum(t))], ingredients) == 500,\n",
371 " filter(lambda t: sum(t) <= capacity,\n",
372 " itertools.product(range(capacity+1), range(capacity+1), \n",
373 " range(capacity+1)))))"
374 ]
375 },
376 {
377 "cell_type": "code",
378 "execution_count": null,
379 "metadata": {
380 "collapsed": true
381 },
382 "outputs": [],
383 "source": []
384 }
385 ],
386 "metadata": {
387 "kernelspec": {
388 "display_name": "Python 3",
389 "language": "python",
390 "name": "python3"
391 },
392 "language_info": {
393 "codemirror_mode": {
394 "name": "ipython",
395 "version": 3
396 },
397 "file_extension": ".py",
398 "mimetype": "text/x-python",
399 "name": "python",
400 "nbconvert_exporter": "python",
401 "pygments_lexer": "ipython3",
402 "version": "3.4.3"
403 }
404 },
405 "nbformat": 4,
406 "nbformat_minor": 0
407 }