Updated question text
[ou-summer-of-code-2017.git] / 02-lifts / lifts-solution.ipynb
index 08e3e77784f66e7bc85dce33075acd4ed01cbd29..281eb44f2400f2a061adc2012c8a7a30d812c494 100644 (file)
@@ -23,7 +23,7 @@
     "\n",
     "For instance, the sequence '^=' takes you to floor 1. \n",
     "\n",
-    "The sequence `vv^=` takes you down two floors to the sub basement (floor -2), then up one floor, and you get out in the sub-basement on floor 0.\n",
+    "The sequence `vv^=` takes you down two floors to the sub-sub-basement (floor -2), then up one floor, and you get out in the sub-basement on floor -1.\n",
     "\n",
     "The sequence `^^v=^=` would start on floor 0, go up two floors, then down one. The doors would then open on floor 1, but you'd stay in the lift. You'd then move up to floor 2, the doors open, and you get out on floor 2. \n",
     "\n",
@@ -51,7 +51,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 2,
    "metadata": {
     "collapsed": true
    },
@@ -66,7 +66,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 11,
+   "execution_count": 3,
    "metadata": {},
    "outputs": [
     {
@@ -75,7 +75,7 @@
        "209"
       ]
      },
-     "execution_count": 11,
+     "execution_count": 3,
      "metadata": {},
      "output_type": "execute_result"
     }
     "exit"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def running(sequence):\n",
+    "    current = 0\n",
+    "    floors = []\n",
+    "    for i in sequence:\n",
+    "        current += value(i)\n",
+    "        floors += [current]\n",
+    "    return floors"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 16,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(10000, 216, -6)"
+      ]
+     },
+     "execution_count": 16,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "with open('02-lifts.txt') as f:\n",
+    "    instructions = f.read()\n",
+    "    floors = running(instructions)\n",
+    "len(floors), max(floors), min(floors)"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": 4,
    "metadata": {},
    "outputs": [
     {
        "209"
       ]
      },
-     "execution_count": 14,
+     "execution_count": 4,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": 7,
    "metadata": {
     "collapsed": true
    },
     "            exits.append(current)\n",
     "        else:\n",
     "            current += value(i)\n",
-    "    return exitsto"
+    "    return exits"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": 8,
    "metadata": {},
    "outputs": [
     {
        "215"
       ]
      },
-     "execution_count": 13,
+     "execution_count": 8,
      "metadata": {},
      "output_type": "execute_result"
     }
     "max(exits(instructions))"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 17,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "-5"
+      ]
+     },
+     "execution_count": 17,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "min(exits(instructions))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 18,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "209"
+      ]
+     },
+     "execution_count": 18,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "exits(instructions)[-1]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 19,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "-2"
+      ]
+     },
+     "execution_count": 19,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "exits(instructions)[0]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 21,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1257"
+      ]
+     },
+     "execution_count": 21,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "len(exits(instructions))"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,