Done day 15
[advent-of-code-15.git] / advent15.ipynb
index ebc895fe62e840125435724a3e193659fcb58c6f..51524906ff80e5165d2f07621b83c24591ac188f 100644 (file)
@@ -2,7 +2,7 @@
  "cells": [
   {
    "cell_type": "code",
-   "execution_count": 24,
+   "execution_count": 6,
    "metadata": {
     "collapsed": true
    },
@@ -14,7 +14,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 1,
+   "execution_count": 39,
    "metadata": {
     "collapsed": false
    },
@@ -28,7 +28,7 @@
        " 'Sugar: capacity 0, durability 0, flavor -2, texture 2, calories 1']"
       ]
      },
-     "execution_count": 1,
+     "execution_count": 39,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 36,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "pi15 = ['Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8',\n",
+    "'Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3']"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 40,
    "metadata": {
     "collapsed": false
    },
@@ -70,7 +82,7 @@
        "  'texture': 2}}"
       ]
      },
-     "execution_count": 9,
+     "execution_count": 40,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 33,
+   "execution_count": 31,
    "metadata": {
     "collapsed": false
    },
     "            property_scores[p] += ingredients[ingredient][p] * quantity\n",
     "    total = 1\n",
     "    for p in property_scores:\n",
-    "        total *= max(property_scores[p], 0)\n",
+    "        if p != 'calories':\n",
+    "            total *= max(property_scores[p], 0)\n",
     "    return total"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 34,
+   "execution_count": 12,
    "metadata": {
-    "collapsed": false
+    "collapsed": false,
+    "scrolled": true
    },
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "9396000000"
+       "[(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": 34,
+     "execution_count": 12,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "score([('Frosting', 30), ('Butterscotch', 30), ('Candy', 30), ('Sugar', 10)], ingredients)"
+    "capacity = 3\n",
+    "list((a,b,c,(capacity-(a+b+c))) for (a,b,c) in filter(lambda t: sum(t) <= capacity,\n",
+    "                         itertools.product(range(capacity+1), range(capacity+1), \n",
+    "                                           range(capacity+1))))"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 31,
+   "execution_count": 13,
    "metadata": {
     "collapsed": false,
     "scrolled": true
        " (3, 0, 0, 0)]"
       ]
      },
-     "execution_count": 31,
+     "execution_count": 13,
      "metadata": {},
      "output_type": "execute_result"
     }
     "                                           range(capacity+1), range(capacity+1))))"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 32,
+   "metadata": {
+    "collapsed": false,
+    "scrolled": true
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "18965440"
+      ]
+     },
+     "execution_count": 32,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "capacity = 100\n",
+    "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', capacity-(f+b+c))], ingredients) \n",
+    " for b, c, f in filter(lambda t: sum(t) <= capacity,\n",
+    "                         itertools.product(range(capacity+1), range(capacity+1), \n",
+    "                                           range(capacity+1))))"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": 38,
     {
      "data": {
       "text/plain": [
-       "10618782720"
+       "57600000"
       ]
      },
      "execution_count": 38,
    ],
    "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))))"
+    "max(score([('Cinnamon', c), ('Butterscotch', (capacity-c))], ingredients) \n",
+    " for (c, ) in filter(lambda t: calories([('Cinnamon', t[0]), ('Butterscotch', (capacity-t[0]))], ingredients) == 500,\n",
+    "                     filter(lambda t: sum(t) <= capacity,\n",
+    "                         itertools.product(range(capacity+1)))))"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 23,
+   "execution_count": 28,
    "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'"
-     ]
+     "data": {
+      "text/plain": [
+       "(defaultdict(<class 'int'>, {'flavor': 152, 'texture': 76, 'capacity': 68, 'calories': 520, 'durability': 80}),\n",
+       " 62842880)"
+      ]
+     },
+     "execution_count": 28,
+     "metadata": {},
+     "output_type": "execute_result"
     }
    ],
-   "source": []
+   "source": [
+    "score([('Cinnamon', 56), ('Butterscotch', 44)], ingredients)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 33,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "def calories(recipe, ingredients):\n",
+    "    return sum(ingredients[i]['calories'] * quantity for i, quantity in recipe)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 34,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "320"
+      ]
+     },
+     "execution_count": 34,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "calories([('Sugar', 56), ('Butterscotch', 44)], ingredients)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 35,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "320"
+      ]
+     },
+     "execution_count": 35,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "56*1 + 44*6"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 42,
+   "metadata": {
+    "collapsed": false,
+    "scrolled": true
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "15862900"
+      ]
+     },
+     "execution_count": 42,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "capacity = 100\n",
+    "max(score([('Frosting', f), ('Butterscotch', b), ('Candy', c), ('Sugar', capacity-(f+b+c))], ingredients) \n",
+    " for f, b, c in filter(lambda t: calories([('Frosting', t[0]), ('Butterscotch', t[1]), \n",
+    "                                           ('Candy', t[2]), ('Sugar', capacity-sum(t))], ingredients) == 500,\n",
+    "                       filter(lambda t: sum(t) <= capacity,\n",
+    "                         itertools.product(range(capacity+1), range(capacity+1), \n",
+    "                                           range(capacity+1)))))"
+   ]
   },
   {
    "cell_type": "code",