Started on day 15
[advent-of-code-15.git] / advent15.ipynb
diff --git a/advent15.ipynb b/advent15.ipynb
new file mode 100644 (file)
index 0000000..ebc895f
--- /dev/null
@@ -0,0 +1,257 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 24,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "from collections import defaultdict\n",
+    "import itertools"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "['Frosting: capacity 4, durability -2, flavor 0, texture 0, calories 5',\n",
+       " 'Candy: capacity 0, durability 5, flavor -1, texture 0, calories 8',\n",
+       " 'Butterscotch: capacity -1, durability 0, flavor 5, texture 0, calories 6',\n",
+       " 'Sugar: capacity 0, durability 0, flavor -2, texture 2, calories 1']"
+      ]
+     },
+     "execution_count": 1,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "pi15 = [l.strip() for l in open('advent15.txt').readlines()]\n",
+    "pi15"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{'Butterscotch': {'calories': 6,\n",
+       "  'capacity': -1,\n",
+       "  'durability': 0,\n",
+       "  'flavor': 5,\n",
+       "  'texture': 0},\n",
+       " 'Candy': {'calories': 8,\n",
+       "  'capacity': 0,\n",
+       "  'durability': 5,\n",
+       "  'flavor': -1,\n",
+       "  'texture': 0},\n",
+       " 'Frosting': {'calories': 5,\n",
+       "  'capacity': 4,\n",
+       "  'durability': -2,\n",
+       "  'flavor': 0,\n",
+       "  'texture': 0},\n",
+       " 'Sugar': {'calories': 1,\n",
+       "  'capacity': 0,\n",
+       "  'durability': 0,\n",
+       "  'flavor': -2,\n",
+       "  'texture': 2}}"
+      ]
+     },
+     "execution_count": 9,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "ingredients = {}\n",
+    "for l in pi15:\n",
+    "    ls = l.split(': ')\n",
+    "    name = ls[0]\n",
+    "    props = ls[1].split(', ')\n",
+    "    properties = {}\n",
+    "    for p in props:\n",
+    "        ps = p.split(' ')\n",
+    "        properties[ps[0].strip()] = int(ps[1].strip())\n",
+    "    ingredients[name] = properties\n",
+    "ingredients"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 33,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "def score(recipe, ingredients):\n",
+    "    property_scores = defaultdict(int)\n",
+    "    for ingredient, quantity in recipe:\n",
+    "        for p in ingredients[ingredient]:\n",
+    "            property_scores[p] += ingredients[ingredient][p] * quantity\n",
+    "    total = 1\n",
+    "    for p in property_scores:\n",
+    "        total *= max(property_scores[p], 0)\n",
+    "    return total"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 34,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "9396000000"
+      ]
+     },
+     "execution_count": 34,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "score([('Frosting', 30), ('Butterscotch', 30), ('Candy', 30), ('Sugar', 10)], ingredients)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 31,
+   "metadata": {
+    "collapsed": false,
+    "scrolled": true
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[(0, 0, 0, 3),\n",
+       " (0, 0, 1, 2),\n",
+       " (0, 0, 2, 1),\n",
+       " (0, 0, 3, 0),\n",
+       " (0, 1, 0, 2),\n",
+       " (0, 1, 1, 1),\n",
+       " (0, 1, 2, 0),\n",
+       " (0, 2, 0, 1),\n",
+       " (0, 2, 1, 0),\n",
+       " (0, 3, 0, 0),\n",
+       " (1, 0, 0, 2),\n",
+       " (1, 0, 1, 1),\n",
+       " (1, 0, 2, 0),\n",
+       " (1, 1, 0, 1),\n",
+       " (1, 1, 1, 0),\n",
+       " (1, 2, 0, 0),\n",
+       " (2, 0, 0, 1),\n",
+       " (2, 0, 1, 0),\n",
+       " (2, 1, 0, 0),\n",
+       " (3, 0, 0, 0)]"
+      ]
+     },
+     "execution_count": 31,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "capacity = 3\n",
+    "list(filter(lambda t: sum(t) == capacity,\n",
+    "                         itertools.product(range(capacity+1), range(capacity+1), \n",
+    "                                           range(capacity+1), range(capacity+1))))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 38,
+   "metadata": {
+    "collapsed": false,
+    "scrolled": true
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "10618782720"
+      ]
+     },
+     "execution_count": 38,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "capacity = 100\n",
+    "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', s)], ingredients) \n",
+    " for b, c, f, s in filter(lambda t: sum(t) == capacity,\n",
+    "                         itertools.product(range(capacity+1), range(capacity+1), \n",
+    "                                           range(capacity+1), range(capacity+1))))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 23,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "ename": "TypeError",
+     "evalue": "unhashable type: 'slice'",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
+      "\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",
+      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'slice'"
+     ]
+    }
+   ],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.4.3"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}