X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=02-lifts%2Flifts-solution.ipynb;h=281eb44f2400f2a061adc2012c8a7a30d812c494;hb=c20e756988ba074608de8769c98ea0f12b2f87c5;hp=08e3e77784f66e7bc85dce33075acd4ed01cbd29;hpb=cc6a9653d2d019572412d31bea180326eef8a022;p=ou-summer-of-code-2017.git diff --git a/02-lifts/lifts-solution.ipynb b/02-lifts/lifts-solution.ipynb index 08e3e77..281eb44 100644 --- a/02-lifts/lifts-solution.ipynb +++ b/02-lifts/lifts-solution.ipynb @@ -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" } @@ -87,6 +87,46 @@ "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": {}, @@ -96,7 +136,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -105,7 +145,7 @@ "209" ] }, - "execution_count": 14, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -128,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 7, "metadata": { "collapsed": true }, @@ -142,12 +182,12 @@ " 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": [ { @@ -156,7 +196,7 @@ "215" ] }, - "execution_count": 13, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -165,6 +205,86 @@ "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,