ebc895fe62e840125435724a3e193659fcb58c6f
[advent-of-code-15.git] / advent15.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 24,
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": 1,
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": 1,
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": 9,
44 "metadata": {
45 "collapsed": false
46 },
47 "outputs": [
48 {
49 "data": {
50 "text/plain": [
51 "{'Butterscotch': {'calories': 6,\n",
52 " 'capacity': -1,\n",
53 " 'durability': 0,\n",
54 " 'flavor': 5,\n",
55 " 'texture': 0},\n",
56 " 'Candy': {'calories': 8,\n",
57 " 'capacity': 0,\n",
58 " 'durability': 5,\n",
59 " 'flavor': -1,\n",
60 " 'texture': 0},\n",
61 " 'Frosting': {'calories': 5,\n",
62 " 'capacity': 4,\n",
63 " 'durability': -2,\n",
64 " 'flavor': 0,\n",
65 " 'texture': 0},\n",
66 " 'Sugar': {'calories': 1,\n",
67 " 'capacity': 0,\n",
68 " 'durability': 0,\n",
69 " 'flavor': -2,\n",
70 " 'texture': 2}}"
71 ]
72 },
73 "execution_count": 9,
74 "metadata": {},
75 "output_type": "execute_result"
76 }
77 ],
78 "source": [
79 "ingredients = {}\n",
80 "for l in pi15:\n",
81 " ls = l.split(': ')\n",
82 " name = ls[0]\n",
83 " props = ls[1].split(', ')\n",
84 " properties = {}\n",
85 " for p in props:\n",
86 " ps = p.split(' ')\n",
87 " properties[ps[0].strip()] = int(ps[1].strip())\n",
88 " ingredients[name] = properties\n",
89 "ingredients"
90 ]
91 },
92 {
93 "cell_type": "code",
94 "execution_count": 33,
95 "metadata": {
96 "collapsed": false
97 },
98 "outputs": [],
99 "source": [
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",
105 " total = 1\n",
106 " for p in property_scores:\n",
107 " total *= max(property_scores[p], 0)\n",
108 " return total"
109 ]
110 },
111 {
112 "cell_type": "code",
113 "execution_count": 34,
114 "metadata": {
115 "collapsed": false
116 },
117 "outputs": [
118 {
119 "data": {
120 "text/plain": [
121 "9396000000"
122 ]
123 },
124 "execution_count": 34,
125 "metadata": {},
126 "output_type": "execute_result"
127 }
128 ],
129 "source": [
130 "score([('Frosting', 30), ('Butterscotch', 30), ('Candy', 30), ('Sugar', 10)], ingredients)"
131 ]
132 },
133 {
134 "cell_type": "code",
135 "execution_count": 31,
136 "metadata": {
137 "collapsed": false,
138 "scrolled": true
139 },
140 "outputs": [
141 {
142 "data": {
143 "text/plain": [
144 "[(0, 0, 0, 3),\n",
145 " (0, 0, 1, 2),\n",
146 " (0, 0, 2, 1),\n",
147 " (0, 0, 3, 0),\n",
148 " (0, 1, 0, 2),\n",
149 " (0, 1, 1, 1),\n",
150 " (0, 1, 2, 0),\n",
151 " (0, 2, 0, 1),\n",
152 " (0, 2, 1, 0),\n",
153 " (0, 3, 0, 0),\n",
154 " (1, 0, 0, 2),\n",
155 " (1, 0, 1, 1),\n",
156 " (1, 0, 2, 0),\n",
157 " (1, 1, 0, 1),\n",
158 " (1, 1, 1, 0),\n",
159 " (1, 2, 0, 0),\n",
160 " (2, 0, 0, 1),\n",
161 " (2, 0, 1, 0),\n",
162 " (2, 1, 0, 0),\n",
163 " (3, 0, 0, 0)]"
164 ]
165 },
166 "execution_count": 31,
167 "metadata": {},
168 "output_type": "execute_result"
169 }
170 ],
171 "source": [
172 "capacity = 3\n",
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))))"
176 ]
177 },
178 {
179 "cell_type": "code",
180 "execution_count": 38,
181 "metadata": {
182 "collapsed": false,
183 "scrolled": true
184 },
185 "outputs": [
186 {
187 "data": {
188 "text/plain": [
189 "10618782720"
190 ]
191 },
192 "execution_count": 38,
193 "metadata": {},
194 "output_type": "execute_result"
195 }
196 ],
197 "source": [
198 "capacity = 100\n",
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))))"
203 ]
204 },
205 {
206 "cell_type": "code",
207 "execution_count": 23,
208 "metadata": {
209 "collapsed": false
210 },
211 "outputs": [
212 {
213 "ename": "TypeError",
214 "evalue": "unhashable type: 'slice'",
215 "output_type": "error",
216 "traceback": [
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'"
221 ]
222 }
223 ],
224 "source": []
225 },
226 {
227 "cell_type": "code",
228 "execution_count": null,
229 "metadata": {
230 "collapsed": true
231 },
232 "outputs": [],
233 "source": []
234 }
235 ],
236 "metadata": {
237 "kernelspec": {
238 "display_name": "Python 3",
239 "language": "python",
240 "name": "python3"
241 },
242 "language_info": {
243 "codemirror_mode": {
244 "name": "ipython",
245 "version": 3
246 },
247 "file_extension": ".py",
248 "mimetype": "text/x-python",
249 "name": "python",
250 "nbconvert_exporter": "python",
251 "pygments_lexer": "ipython3",
252 "version": "3.4.3"
253 }
254 },
255 "nbformat": 4,
256 "nbformat_minor": 0
257 }