Started on brainfuck soluiton
[ou-summer-of-code-2017.git] / 02-lifts / lifts-solution.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Puzzle 2: Lifts\n",
8 "\n",
9 "## Part 1: using the lifts\n",
10 "\n",
11 "You've chosen your holiday, now it's time to catch your flight. You arrive at Heathwick airport and have to find the departures floor. However, getting around Terminal $\\pi$ is a real pain. The instructions for how to use the lifts are rather complex.\n",
12 "\n",
13 "You start in the car park (the basement level, floor 0) and follow this instructions one step at a time. The instructions are each one character:\n",
14 "* `^` : go up one floor\n",
15 "* `v` : go down one floor\n",
16 "* `=` : open the doors, without moving floors.\n",
17 "\n",
18 "The input contains no other characters.\n",
19 "\n",
20 "If you follow the instructions and get out at the end, what floor do you end up on?\n",
21 "\n",
22 "Terminal $\\pi$ is large and there's no limit to the number of floors, both up and down.\n",
23 "\n",
24 "For instance, the sequence '^=' takes you to floor 1. \n",
25 "\n",
26 "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",
27 "\n",
28 "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",
29 "\n",
30 "The sequence `v^^^^^v=v=` would go down one floor (to floor -1), up five floors (to floor 4), down one floor (to floor 3), open the doors, go down another floor, and you'd finally get out at floor 2.\n",
31 "\n",
32 "Given the input in [02-lifts.txt](02-lifts.txt), where would you get out?"
33 ]
34 },
35 {
36 "cell_type": "code",
37 "execution_count": 2,
38 "metadata": {
39 "collapsed": true
40 },
41 "outputs": [],
42 "source": [
43 "def value(instr):\n",
44 " if instr == '^':\n",
45 " return 1\n",
46 " elif instr == 'v':\n",
47 " return -1\n",
48 " else:\n",
49 " return 0"
50 ]
51 },
52 {
53 "cell_type": "code",
54 "execution_count": 3,
55 "metadata": {
56 "collapsed": true
57 },
58 "outputs": [],
59 "source": [
60 "def final(sequence):\n",
61 " current = 0\n",
62 " for c in sequence:\n",
63 " current += value(c)\n",
64 " return current"
65 ]
66 },
67 {
68 "cell_type": "code",
69 "execution_count": 4,
70 "metadata": {},
71 "outputs": [
72 {
73 "data": {
74 "text/plain": [
75 "209"
76 ]
77 },
78 "execution_count": 4,
79 "metadata": {},
80 "output_type": "execute_result"
81 }
82 ],
83 "source": [
84 "with open('02-lifts.txt') as f:\n",
85 " instructions = f.read()\n",
86 " exit = final(instructions)\n",
87 "exit"
88 ]
89 },
90 {
91 "cell_type": "code",
92 "execution_count": 5,
93 "metadata": {
94 "collapsed": true
95 },
96 "outputs": [],
97 "source": [
98 "def running(sequence):\n",
99 " current = 0\n",
100 " floors = []\n",
101 " for i in sequence:\n",
102 " current += value(i)\n",
103 " floors += [current]\n",
104 " return floors"
105 ]
106 },
107 {
108 "cell_type": "code",
109 "execution_count": 6,
110 "metadata": {},
111 "outputs": [
112 {
113 "data": {
114 "text/plain": [
115 "(10002, 216, -6)"
116 ]
117 },
118 "execution_count": 6,
119 "metadata": {},
120 "output_type": "execute_result"
121 }
122 ],
123 "source": [
124 "with open('02-lifts.txt') as f:\n",
125 " instructions = f.read()\n",
126 " floors = running(instructions)\n",
127 "len(floors), max(floors), min(floors)"
128 ]
129 },
130 {
131 "cell_type": "markdown",
132 "metadata": {},
133 "source": [
134 "### Smart-alec one line solution"
135 ]
136 },
137 {
138 "cell_type": "code",
139 "execution_count": 7,
140 "metadata": {},
141 "outputs": [
142 {
143 "data": {
144 "text/plain": [
145 "209"
146 ]
147 },
148 "execution_count": 7,
149 "metadata": {},
150 "output_type": "execute_result"
151 }
152 ],
153 "source": [
154 "sum(value(i) for i in open('02-lifts.txt').read())"
155 ]
156 },
157 {
158 "cell_type": "markdown",
159 "metadata": {},
160 "source": [
161 "## Part 2: getting out\n",
162 "You can only leave the lift where the doors are open. What is the highest floor you could leave from?\n",
163 "\n",
164 "For instance, the sequence `^^v=^=` would allow you to exit on floors 1 and 2, so the highest floor you could leave from would be floor 2. \n",
165 "\n",
166 "The sequence `v^^^^^v=v=` would allow you to exit on floors 3 and 2, so the highest floor you could leave from would be floor 3 (even though the lift reaches floor 4)."
167 ]
168 },
169 {
170 "cell_type": "code",
171 "execution_count": 8,
172 "metadata": {
173 "collapsed": true
174 },
175 "outputs": [],
176 "source": [
177 "def exits(sequence):\n",
178 " current = 0\n",
179 " exits = []\n",
180 " for i in sequence:\n",
181 " if value(i) == 0:\n",
182 " exits.append(current)\n",
183 " else:\n",
184 " current += value(i)\n",
185 " return exits"
186 ]
187 },
188 {
189 "cell_type": "code",
190 "execution_count": 9,
191 "metadata": {},
192 "outputs": [
193 {
194 "data": {
195 "text/plain": [
196 "215"
197 ]
198 },
199 "execution_count": 9,
200 "metadata": {},
201 "output_type": "execute_result"
202 }
203 ],
204 "source": [
205 "max(exits(instructions))"
206 ]
207 },
208 {
209 "cell_type": "code",
210 "execution_count": 10,
211 "metadata": {},
212 "outputs": [
213 {
214 "data": {
215 "text/plain": [
216 "-5"
217 ]
218 },
219 "execution_count": 10,
220 "metadata": {},
221 "output_type": "execute_result"
222 }
223 ],
224 "source": [
225 "min(exits(instructions))"
226 ]
227 },
228 {
229 "cell_type": "code",
230 "execution_count": 11,
231 "metadata": {},
232 "outputs": [
233 {
234 "data": {
235 "text/plain": [
236 "209"
237 ]
238 },
239 "execution_count": 11,
240 "metadata": {},
241 "output_type": "execute_result"
242 }
243 ],
244 "source": [
245 "exits(instructions)[-1]"
246 ]
247 },
248 {
249 "cell_type": "code",
250 "execution_count": 12,
251 "metadata": {},
252 "outputs": [
253 {
254 "data": {
255 "text/plain": [
256 "-2"
257 ]
258 },
259 "execution_count": 12,
260 "metadata": {},
261 "output_type": "execute_result"
262 }
263 ],
264 "source": [
265 "exits(instructions)[0]"
266 ]
267 },
268 {
269 "cell_type": "code",
270 "execution_count": 13,
271 "metadata": {},
272 "outputs": [
273 {
274 "data": {
275 "text/plain": [
276 "1259"
277 ]
278 },
279 "execution_count": 13,
280 "metadata": {},
281 "output_type": "execute_result"
282 }
283 ],
284 "source": [
285 "len(exits(instructions))"
286 ]
287 },
288 {
289 "cell_type": "code",
290 "execution_count": 14,
291 "metadata": {},
292 "outputs": [
293 {
294 "data": {
295 "text/plain": [
296 "Counter({-5: 1,\n",
297 " -4: 1,\n",
298 " -2: 2,\n",
299 " 1: 1,\n",
300 " 3: 1,\n",
301 " 6: 1,\n",
302 " 11: 1,\n",
303 " 12: 1,\n",
304 " 18: 1,\n",
305 " 19: 2,\n",
306 " 20: 3,\n",
307 " 21: 3,\n",
308 " 22: 4,\n",
309 " 23: 4,\n",
310 " 24: 6,\n",
311 " 25: 4,\n",
312 " 26: 4,\n",
313 " 27: 6,\n",
314 " 28: 4,\n",
315 " 29: 4,\n",
316 " 30: 2,\n",
317 " 31: 1,\n",
318 " 32: 7,\n",
319 " 33: 3,\n",
320 " 34: 5,\n",
321 " 35: 2,\n",
322 " 36: 2,\n",
323 " 37: 2,\n",
324 " 38: 3,\n",
325 " 39: 5,\n",
326 " 40: 9,\n",
327 " 41: 4,\n",
328 " 42: 7,\n",
329 " 43: 11,\n",
330 " 44: 15,\n",
331 " 45: 19,\n",
332 " 46: 22,\n",
333 " 47: 12,\n",
334 " 48: 23,\n",
335 " 49: 24,\n",
336 " 50: 27,\n",
337 " 51: 25,\n",
338 " 52: 27,\n",
339 " 53: 25,\n",
340 " 54: 16,\n",
341 " 55: 21,\n",
342 " 56: 21,\n",
343 " 57: 15,\n",
344 " 58: 13,\n",
345 " 59: 18,\n",
346 " 60: 19,\n",
347 " 61: 13,\n",
348 " 62: 21,\n",
349 " 63: 15,\n",
350 " 64: 19,\n",
351 " 65: 18,\n",
352 " 66: 17,\n",
353 " 67: 17,\n",
354 " 68: 8,\n",
355 " 69: 8,\n",
356 " 70: 12,\n",
357 " 71: 6,\n",
358 " 72: 5,\n",
359 " 73: 3,\n",
360 " 74: 6,\n",
361 " 75: 5,\n",
362 " 76: 3,\n",
363 " 77: 2,\n",
364 " 78: 1,\n",
365 " 79: 4,\n",
366 " 80: 2,\n",
367 " 81: 1,\n",
368 " 83: 1,\n",
369 " 84: 4,\n",
370 " 87: 3,\n",
371 " 88: 4,\n",
372 " 89: 2,\n",
373 " 90: 2,\n",
374 " 91: 3,\n",
375 " 92: 1,\n",
376 " 93: 5,\n",
377 " 94: 2,\n",
378 " 95: 6,\n",
379 " 96: 5,\n",
380 " 97: 11,\n",
381 " 98: 10,\n",
382 " 99: 15,\n",
383 " 100: 10,\n",
384 " 101: 17,\n",
385 " 102: 15,\n",
386 " 103: 6,\n",
387 " 104: 9,\n",
388 " 105: 6,\n",
389 " 106: 8,\n",
390 " 107: 13,\n",
391 " 108: 12,\n",
392 " 109: 9,\n",
393 " 110: 10,\n",
394 " 111: 7,\n",
395 " 112: 4,\n",
396 " 113: 8,\n",
397 " 114: 13,\n",
398 " 115: 6,\n",
399 " 116: 6,\n",
400 " 117: 6,\n",
401 " 118: 6,\n",
402 " 119: 7,\n",
403 " 120: 4,\n",
404 " 121: 3,\n",
405 " 122: 5,\n",
406 " 123: 8,\n",
407 " 124: 6,\n",
408 " 125: 7,\n",
409 " 126: 6,\n",
410 " 127: 4,\n",
411 " 128: 3,\n",
412 " 129: 5,\n",
413 " 130: 6,\n",
414 " 131: 6,\n",
415 " 132: 8,\n",
416 " 133: 5,\n",
417 " 134: 13,\n",
418 " 135: 13,\n",
419 " 136: 14,\n",
420 " 137: 9,\n",
421 " 138: 16,\n",
422 " 139: 12,\n",
423 " 140: 12,\n",
424 " 141: 11,\n",
425 " 142: 15,\n",
426 " 143: 16,\n",
427 " 144: 3,\n",
428 " 145: 5,\n",
429 " 146: 10,\n",
430 " 147: 3,\n",
431 " 148: 3,\n",
432 " 149: 2,\n",
433 " 150: 3,\n",
434 " 151: 6,\n",
435 " 152: 7,\n",
436 " 153: 5,\n",
437 " 154: 7,\n",
438 " 155: 5,\n",
439 " 156: 5,\n",
440 " 157: 1,\n",
441 " 158: 5,\n",
442 " 159: 5,\n",
443 " 160: 6,\n",
444 " 161: 2,\n",
445 " 162: 2,\n",
446 " 164: 7,\n",
447 " 165: 2,\n",
448 " 166: 2,\n",
449 " 167: 3,\n",
450 " 168: 1,\n",
451 " 169: 4,\n",
452 " 170: 3,\n",
453 " 171: 1,\n",
454 " 173: 1,\n",
455 " 178: 1,\n",
456 " 179: 1,\n",
457 " 181: 1,\n",
458 " 185: 1,\n",
459 " 187: 1,\n",
460 " 188: 1,\n",
461 " 189: 1,\n",
462 " 192: 2,\n",
463 " 193: 1,\n",
464 " 194: 3,\n",
465 " 195: 1,\n",
466 " 196: 2,\n",
467 " 199: 1,\n",
468 " 200: 2,\n",
469 " 201: 1,\n",
470 " 204: 1,\n",
471 " 205: 1,\n",
472 " 207: 2,\n",
473 " 208: 3,\n",
474 " 209: 11,\n",
475 " 210: 2,\n",
476 " 211: 1,\n",
477 " 212: 1,\n",
478 " 213: 2,\n",
479 " 215: 2})"
480 ]
481 },
482 "execution_count": 14,
483 "metadata": {},
484 "output_type": "execute_result"
485 }
486 ],
487 "source": [
488 "import collections\n",
489 "collections.Counter(exits(instructions))"
490 ]
491 },
492 {
493 "cell_type": "code",
494 "execution_count": null,
495 "metadata": {
496 "collapsed": true
497 },
498 "outputs": [],
499 "source": []
500 }
501 ],
502 "metadata": {
503 "kernelspec": {
504 "display_name": "Python 3",
505 "language": "python",
506 "name": "python3"
507 },
508 "language_info": {
509 "codemirror_mode": {
510 "name": "ipython",
511 "version": 3
512 },
513 "file_extension": ".py",
514 "mimetype": "text/x-python",
515 "name": "python",
516 "nbconvert_exporter": "python",
517 "pygments_lexer": "ipython3",
518 "version": "3.5.2+"
519 }
520 },
521 "nbformat": 4,
522 "nbformat_minor": 2
523 }