X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=02-lifts%2Flifts-solution.ipynb;h=723d33e92a3e4f5c93f9e9aab040b99003928093;hb=35334be6542c54514e0a1bf11d247063c2c1ed51;hp=e938ff7e1e0f53e794396b867f79db8e929ee444;hpb=e7b049e703a0fbce5aa31aac196606e1b6474d06;p=ou-summer-of-code-2017.git diff --git a/02-lifts/lifts-solution.ipynb b/02-lifts/lifts-solution.ipynb index e938ff7..723d33e 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": 4, + "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": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10002, 216, -6)" + ] + }, + "execution_count": 5, + "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": 6, "metadata": {}, "outputs": [ { @@ -105,7 +145,7 @@ "209" ] }, - "execution_count": 14, + "execution_count": 6, "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,310 @@ "max(exits(instructions))" ] }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-5" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min(exits(instructions))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "209" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "exits(instructions)[-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-2" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "exits(instructions)[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1259" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(exits(instructions))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Counter({-5: 1,\n", + " -4: 1,\n", + " -2: 2,\n", + " 1: 1,\n", + " 3: 1,\n", + " 6: 1,\n", + " 11: 1,\n", + " 12: 1,\n", + " 18: 1,\n", + " 19: 2,\n", + " 20: 3,\n", + " 21: 3,\n", + " 22: 4,\n", + " 23: 4,\n", + " 24: 6,\n", + " 25: 4,\n", + " 26: 4,\n", + " 27: 6,\n", + " 28: 4,\n", + " 29: 4,\n", + " 30: 2,\n", + " 31: 1,\n", + " 32: 7,\n", + " 33: 3,\n", + " 34: 5,\n", + " 35: 2,\n", + " 36: 2,\n", + " 37: 2,\n", + " 38: 3,\n", + " 39: 5,\n", + " 40: 9,\n", + " 41: 4,\n", + " 42: 7,\n", + " 43: 11,\n", + " 44: 15,\n", + " 45: 19,\n", + " 46: 22,\n", + " 47: 12,\n", + " 48: 23,\n", + " 49: 24,\n", + " 50: 27,\n", + " 51: 25,\n", + " 52: 27,\n", + " 53: 25,\n", + " 54: 16,\n", + " 55: 21,\n", + " 56: 21,\n", + " 57: 15,\n", + " 58: 13,\n", + " 59: 18,\n", + " 60: 19,\n", + " 61: 13,\n", + " 62: 21,\n", + " 63: 15,\n", + " 64: 19,\n", + " 65: 18,\n", + " 66: 17,\n", + " 67: 17,\n", + " 68: 8,\n", + " 69: 8,\n", + " 70: 12,\n", + " 71: 6,\n", + " 72: 5,\n", + " 73: 3,\n", + " 74: 6,\n", + " 75: 5,\n", + " 76: 3,\n", + " 77: 2,\n", + " 78: 1,\n", + " 79: 4,\n", + " 80: 2,\n", + " 81: 1,\n", + " 83: 1,\n", + " 84: 4,\n", + " 87: 3,\n", + " 88: 4,\n", + " 89: 2,\n", + " 90: 2,\n", + " 91: 3,\n", + " 92: 1,\n", + " 93: 5,\n", + " 94: 2,\n", + " 95: 6,\n", + " 96: 5,\n", + " 97: 11,\n", + " 98: 10,\n", + " 99: 15,\n", + " 100: 10,\n", + " 101: 17,\n", + " 102: 15,\n", + " 103: 6,\n", + " 104: 9,\n", + " 105: 6,\n", + " 106: 8,\n", + " 107: 13,\n", + " 108: 12,\n", + " 109: 9,\n", + " 110: 10,\n", + " 111: 7,\n", + " 112: 4,\n", + " 113: 8,\n", + " 114: 13,\n", + " 115: 6,\n", + " 116: 6,\n", + " 117: 6,\n", + " 118: 6,\n", + " 119: 7,\n", + " 120: 4,\n", + " 121: 3,\n", + " 122: 5,\n", + " 123: 8,\n", + " 124: 6,\n", + " 125: 7,\n", + " 126: 6,\n", + " 127: 4,\n", + " 128: 3,\n", + " 129: 5,\n", + " 130: 6,\n", + " 131: 6,\n", + " 132: 8,\n", + " 133: 5,\n", + " 134: 13,\n", + " 135: 13,\n", + " 136: 14,\n", + " 137: 9,\n", + " 138: 16,\n", + " 139: 12,\n", + " 140: 12,\n", + " 141: 11,\n", + " 142: 15,\n", + " 143: 16,\n", + " 144: 3,\n", + " 145: 5,\n", + " 146: 10,\n", + " 147: 3,\n", + " 148: 3,\n", + " 149: 2,\n", + " 150: 3,\n", + " 151: 6,\n", + " 152: 7,\n", + " 153: 5,\n", + " 154: 7,\n", + " 155: 5,\n", + " 156: 5,\n", + " 157: 1,\n", + " 158: 5,\n", + " 159: 5,\n", + " 160: 6,\n", + " 161: 2,\n", + " 162: 2,\n", + " 164: 7,\n", + " 165: 2,\n", + " 166: 2,\n", + " 167: 3,\n", + " 168: 1,\n", + " 169: 4,\n", + " 170: 3,\n", + " 171: 1,\n", + " 173: 1,\n", + " 178: 1,\n", + " 179: 1,\n", + " 181: 1,\n", + " 185: 1,\n", + " 187: 1,\n", + " 188: 1,\n", + " 189: 1,\n", + " 192: 2,\n", + " 193: 1,\n", + " 194: 3,\n", + " 195: 1,\n", + " 196: 2,\n", + " 199: 1,\n", + " 200: 2,\n", + " 201: 1,\n", + " 204: 1,\n", + " 205: 1,\n", + " 207: 2,\n", + " 208: 3,\n", + " 209: 11,\n", + " 210: 2,\n", + " 211: 1,\n", + " 212: 1,\n", + " 213: 2,\n", + " 215: 2})" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import collections\n", + "collections.Counter(exits(instructions))" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-2" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(exits(instructions[:20]))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -191,7 +535,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.5.2+" } }, "nbformat": 4,