Removing files from data analysis directory
[ou-summer-of-code-2017.git] / 05-display-board / display-board-creation.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": true
8 },
9 "outputs": [],
10 "source": [
11 "import itertools\n",
12 "import time\n",
13 "import re\n",
14 "from IPython.display import clear_output\n",
15 "import random"
16 ]
17 },
18 {
19 "cell_type": "code",
20 "execution_count": 2,
21 "metadata": {
22 "collapsed": true
23 },
24 "outputs": [],
25 "source": [
26 "WIDTH = 50\n",
27 "HEIGHT = 8"
28 ]
29 },
30 {
31 "cell_type": "code",
32 "execution_count": 3,
33 "metadata": {
34 "collapsed": true
35 },
36 "outputs": [],
37 "source": [
38 "def new_grid(w=WIDTH, h=HEIGHT):\n",
39 " return ['.' * w for r in range(1, h+1)]"
40 ]
41 },
42 {
43 "cell_type": "code",
44 "execution_count": 4,
45 "metadata": {
46 "collapsed": true
47 },
48 "outputs": [],
49 "source": [
50 "def print_grid(grid, md=False, suppress_dots=False):\n",
51 " if md:\n",
52 " print('```')\n",
53 " for row in grid:\n",
54 " if suppress_dots:\n",
55 " print(re.sub(r'\\.', ' ', row))\n",
56 " else:\n",
57 " print(row) \n",
58 " if md:\n",
59 " print('```')"
60 ]
61 },
62 {
63 "cell_type": "code",
64 "execution_count": 5,
65 "metadata": {
66 "collapsed": true
67 },
68 "outputs": [],
69 "source": [
70 "def top(grid, l, r):\n",
71 " new_segment = ''\n",
72 " for i in range(l-1, r):\n",
73 " if grid[0][i] == '.':\n",
74 " new_segment += '*'\n",
75 " else:\n",
76 " new_segment += '.'\n",
77 " grid[0] = grid[0][:l-1] + new_segment + grid[0][r:]\n",
78 " return grid"
79 ]
80 },
81 {
82 "cell_type": "code",
83 "execution_count": 6,
84 "metadata": {
85 "collapsed": true
86 },
87 "outputs": [],
88 "source": [
89 "def left(grid, t, b):\n",
90 " for i in range(t-1, b):\n",
91 " if grid[i][0] == '.':\n",
92 " grid[i] = '*' + grid[i][1:]\n",
93 " else:\n",
94 " grid[i] = '.' + grid[i][1:]\n",
95 " return grid"
96 ]
97 },
98 {
99 "cell_type": "code",
100 "execution_count": 7,
101 "metadata": {
102 "collapsed": true
103 },
104 "outputs": [],
105 "source": [
106 "def rotate_column(grid, c, raw_n):\n",
107 " n = raw_n % len(grid)\n",
108 " col = [row[c-1] for row in grid]\n",
109 " new_col = col[-n:] + col[:-n]\n",
110 " for i in range(len(grid)):\n",
111 " grid[i] = grid[i][:c-1] + new_col[i] + grid[i][c:]\n",
112 " return grid"
113 ]
114 },
115 {
116 "cell_type": "code",
117 "execution_count": 8,
118 "metadata": {
119 "collapsed": true
120 },
121 "outputs": [],
122 "source": [
123 "def rotate_row(grid, r, raw_n):\n",
124 " n = raw_n % len(grid[0])\n",
125 " grid[r-1] = grid[r-1][-n:] + grid[r-1][:-n]\n",
126 " return grid"
127 ]
128 },
129 {
130 "cell_type": "code",
131 "execution_count": 9,
132 "metadata": {
133 "collapsed": true
134 },
135 "outputs": [],
136 "source": [
137 "command_dispatch = {'left': left, 'top': top,\n",
138 " 'rotate row': rotate_row,\n",
139 " 'rotate column': rotate_column}"
140 ]
141 },
142 {
143 "cell_type": "code",
144 "execution_count": 10,
145 "metadata": {
146 "collapsed": true
147 },
148 "outputs": [],
149 "source": [
150 "def parse(command):\n",
151 " cmd, a, b = command.rsplit(maxsplit=2)\n",
152 " return cmd, int(a), int(b) "
153 ]
154 },
155 {
156 "cell_type": "code",
157 "execution_count": 11,
158 "metadata": {
159 "collapsed": true
160 },
161 "outputs": [],
162 "source": [
163 "def interpret(commands, grid=None, w=WIDTH, h=HEIGHT, \n",
164 " uninterpret=False,\n",
165 " show_each_step=False, md=False, overprint=False,\n",
166 " suppress_dots=False):\n",
167 " if grid is None:\n",
168 " grid = new_grid(w, h)\n",
169 " if uninterpret:\n",
170 " ordered_commands = reversed(commands)\n",
171 " else:\n",
172 " ordered_commands = commands\n",
173 " for c in ordered_commands:\n",
174 " cmd, a, b = parse(c)\n",
175 " if uninterpret and cmd in uncommand_dispatch:\n",
176 " uncommand_dispatch[cmd](grid, a, b)\n",
177 " elif not uninterpret and cmd in command_dispatch:\n",
178 " command_dispatch[cmd](grid, a, b)\n",
179 " else:\n",
180 " raise ValueError('Unknown command')\n",
181 " if show_each_step:\n",
182 " if overprint:\n",
183 " time.sleep(0.25)\n",
184 " if md: \n",
185 " print('`{}`'.format(c))\n",
186 " else:\n",
187 " print(c)\n",
188 " print_grid(grid, md=md, suppress_dots=suppress_dots)\n",
189 " print()\n",
190 " if overprint:\n",
191 " clear_output(wait=True)\n",
192 " if show_each_step: \n",
193 " print('Final')\n",
194 " print_grid(grid, md=md, suppress_dots=suppress_dots)\n",
195 " return grid\n",
196 " \n",
197 " \n",
198 "# for i in range(10):\n",
199 "# time.sleep(0.25)\n",
200 "# print(i)\n",
201 "# clear_output(wait=True)"
202 ]
203 },
204 {
205 "cell_type": "markdown",
206 "metadata": {},
207 "source": [
208 "For instance, with a smaller grid that is 10 pixels wide and 4 tall, this is what a sample sequence of instructions would do.\n",
209 "\n",
210 "* `toggle 1 6` turns on the first six pixels on the top row.\n",
211 "```\n",
212 "******....\n",
213 "..........\n",
214 "..........\n",
215 "..........\n",
216 "```\n",
217 "\n",
218 "* `rotate column 2 3` moves the lit pixel on the second column to the bottom row.\n",
219 "```\n",
220 "*.****....\n",
221 "..........\n",
222 "..........\n",
223 ".*........\n",
224 "```\n",
225 "\n",
226 "* `toggle 3 10` turns off the pixels in columns 4, 5, and 6, and turns on the pixels in columns 7 to 10.\n",
227 "\n",
228 "```\n",
229 "*.....****\n",
230 "..........\n",
231 "..........\n",
232 ".*........\n",
233 "```\n",
234 "\n",
235 "* `rotate column 8 1` moves the one lit pixel in column 8 down one row.\n",
236 "```\n",
237 "*.....*.**\n",
238 ".......*..\n",
239 "..........\n",
240 ".*........\n",
241 "```\n",
242 "\n",
243 "* `rotate row 2 6` moves that pixel off the right edge of the display, to it wraps around to appear in column 4.\n",
244 "```\n",
245 "*.....*.**\n",
246 "...*......\n",
247 "..........\n",
248 ".*........\n",
249 "```\n",
250 "\n",
251 "* `left 1 3` toggles the pixels in rows 1, 2, and 3 of the first column. The top left pixel (previously on) turns off, while the pixels in rows 2 and 3 come on.\n",
252 "\n",
253 "```\n",
254 "......*.**\n",
255 "*..*......\n",
256 "*.........\n",
257 ".*........\n",
258 "```"
259 ]
260 },
261 {
262 "cell_type": "code",
263 "execution_count": 12,
264 "metadata": {},
265 "outputs": [
266 {
267 "name": "stdout",
268 "output_type": "stream",
269 "text": [
270 "`top 1 6`\n",
271 "```\n",
272 "******....\n",
273 "..........\n",
274 "..........\n",
275 "..........\n",
276 "```\n",
277 "\n",
278 "`rotate column 2 3`\n",
279 "```\n",
280 "*.****....\n",
281 "..........\n",
282 "..........\n",
283 ".*........\n",
284 "```\n",
285 "\n",
286 "`top 3 10`\n",
287 "```\n",
288 "*.....****\n",
289 "..........\n",
290 "..........\n",
291 ".*........\n",
292 "```\n",
293 "\n",
294 "`rotate column 8 1`\n",
295 "```\n",
296 "*.....*.**\n",
297 ".......*..\n",
298 "..........\n",
299 ".*........\n",
300 "```\n",
301 "\n",
302 "`rotate row 2 6`\n",
303 "```\n",
304 "*.....*.**\n",
305 "...*......\n",
306 "..........\n",
307 ".*........\n",
308 "```\n",
309 "\n",
310 "`left 1 3`\n",
311 "```\n",
312 "......*.**\n",
313 "*..*......\n",
314 "*.........\n",
315 ".*........\n",
316 "```\n",
317 "\n",
318 "Final\n",
319 "```\n",
320 "......*.**\n",
321 "*..*......\n",
322 "*.........\n",
323 ".*........\n",
324 "```\n"
325 ]
326 },
327 {
328 "data": {
329 "text/plain": [
330 "['......*.**', '*..*......', '*.........', '.*........']"
331 ]
332 },
333 "execution_count": 12,
334 "metadata": {},
335 "output_type": "execute_result"
336 }
337 ],
338 "source": [
339 "cmds = '''\n",
340 "top 1 6\n",
341 "rotate column 2 3\n",
342 "top 3 10\n",
343 "rotate column 8 1\n",
344 "rotate row 2 6\n",
345 "left 1 3\n",
346 "'''.split('\\n')[1:-1]\n",
347 "interpret(cmds, w=10, h=4, show_each_step=True, md=True)"
348 ]
349 },
350 {
351 "cell_type": "code",
352 "execution_count": 13,
353 "metadata": {
354 "collapsed": true
355 },
356 "outputs": [],
357 "source": [
358 "def unrotate_column(grid, c, raw_n):\n",
359 " return rotate_column(grid, c, (-1 * raw_n) % len(grid))\n",
360 "\n",
361 "def unrotate_row(grid, r, raw_n):\n",
362 " return rotate_row(grid, r, (-1 * raw_n) % len(grid[0]))\n"
363 ]
364 },
365 {
366 "cell_type": "code",
367 "execution_count": 14,
368 "metadata": {
369 "collapsed": true
370 },
371 "outputs": [],
372 "source": [
373 "uncommand_dispatch = {'left': left, 'top': top,\n",
374 " 'rotate row': unrotate_row,\n",
375 " 'rotate column': unrotate_column}"
376 ]
377 },
378 {
379 "cell_type": "code",
380 "execution_count": 15,
381 "metadata": {},
382 "outputs": [
383 {
384 "data": {
385 "text/plain": [
386 "['..................................................',\n",
387 " '..*****..****....***...*...*..*****...***...****..',\n",
388 " '....*....*...*..*...*..**..*....*....*...*..*...*.',\n",
389 " '....*....*..*...*...*..*.*.*....*....*...*..*..*..',\n",
390 " '....*....****...*****..*.*.*....*....*...*..****..',\n",
391 " '....*....*...*..*...*..*..**....*....*...*..*...*.',\n",
392 " '....*....*...*..*...*..*...*....*.....***...*...*.',\n",
393 " '..................................................']"
394 ]
395 },
396 "execution_count": 15,
397 "metadata": {},
398 "output_type": "execute_result"
399 }
400 ],
401 "source": [
402 "trantor_grid = '''\n",
403 "..................................................\n",
404 "..*****..****....***...*...*..*****...***...****..\n",
405 "....*....*...*..*...*..**..*....*....*...*..*...*.\n",
406 "....*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
407 "....*....****...*****..*.*.*....*....*...*..****..\n",
408 "....*....*...*..*...*..*..**....*....*...*..*...*.\n",
409 "....*....*...*..*...*..*...*....*.....***...*...*.\n",
410 "..................................................\n",
411 "'''.split('\\n')[1:-1]\n",
412 "trantor_grid"
413 ]
414 },
415 {
416 "cell_type": "code",
417 "execution_count": 16,
418 "metadata": {
419 "collapsed": true
420 },
421 "outputs": [],
422 "source": [
423 "def tg():\n",
424 " return '''\n",
425 "..................................................\n",
426 "..*****..****....***...*...*..*****...***...****..\n",
427 "....*....*...*..*...*..**..*....*....*...*..*...*.\n",
428 "....*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
429 "....*....****...*****..*.*.*....*....*...*..****..\n",
430 "....*....*...*..*...*..*..**....*....*...*..*...*.\n",
431 "....*....*...*..*...*..*...*....*.....***...*...*.\n",
432 "..................................................\n",
433 "'''.split('\\n')[1:-1]"
434 ]
435 },
436 {
437 "cell_type": "code",
438 "execution_count": 17,
439 "metadata": {},
440 "outputs": [
441 {
442 "name": "stdout",
443 "output_type": "stream",
444 "text": [
445 "....................................................................................................\n",
446 "....................................................................................................\n",
447 "....................................................................................................\n",
448 "....................................................................................................\n",
449 "....................................................................................................\n",
450 "....................................................................................................\n",
451 "....................................................................................................\n",
452 "....................................................................................................\n"
453 ]
454 }
455 ],
456 "source": [
457 "print_grid(new_grid(100, 8))"
458 ]
459 },
460 {
461 "cell_type": "code",
462 "execution_count": 18,
463 "metadata": {
464 "collapsed": true
465 },
466 "outputs": [],
467 "source": [
468 "def jantar_mantar():\n",
469 " return '''\n",
470 "..*****.............................................................................................\n",
471 ".....*..............................................................................................\n",
472 ".....*..............................................................................................\n",
473 ".....*..............................................................................................\n",
474 ".....*..............................................................................................\n",
475 ".....*..............................................................................................\n",
476 ".*...*..............................................................................................\n",
477 "..***...............................................................................................\n",
478 "'''.split('\\n')[1:-1]"
479 ]
480 },
481 {
482 "cell_type": "code",
483 "execution_count": 19,
484 "metadata": {
485 "collapsed": true
486 },
487 "outputs": [],
488 "source": [
489 "def jantar_mantar():\n",
490 " return '''\n",
491 "...****..............*...................*.....*..............*.................\n",
492 "......*..............*...................***..**..............*.................\n",
493 "......*.*****.*****.****.*****..****.....*.*.***.*****.*****.****.*****..****...\n",
494 "......*.....*.*...*..*.......*..*........*..**.*.....*.*...*..*.......*..*......\n",
495 "......*.*****.*...*..*...*****..*........*..*..*.*****.*...*..*...*****..*......\n",
496 "......*.*...*.*...*..*...*...*..*........*.....*.*...*.*...*..*...*...*..*......\n",
497 "...*..*.*..**.*...*..**..*..**..*........*.....*.*..**.*...*..**..*..**..*......\n",
498 "....**...**.*.*...*...**..**.*..*........*.....*..**.*.*...*...**..**.*..*......\n",
499 "'''.split('\\n')[1:-1]"
500 ]
501 },
502 {
503 "cell_type": "code",
504 "execution_count": 20,
505 "metadata": {},
506 "outputs": [
507 {
508 "name": "stdout",
509 "output_type": "stream",
510 "text": [
511 "left 3 7\n",
512 "..................................................\n",
513 "..*****..****....***...*...*..*****...***...****..\n",
514 "*...*....*...*..*...*..**..*....*....*...*..*...*.\n",
515 "*...*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
516 "*...*....****...*****..*.*.*....*....*...*..****..\n",
517 "*...*....*...*..*...*..*..**....*....*...*..*...*.\n",
518 "*...*....*...*..*...*..*...*....*.....***...*...*.\n",
519 "..................................................\n",
520 "\n",
521 "rotate column 1 1\n",
522 "..................................................\n",
523 "*.*****..****....***...*...*..*****...***...****..\n",
524 "*...*....*...*..*...*..**..*....*....*...*..*...*.\n",
525 "*...*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
526 "*...*....****...*****..*.*.*....*....*...*..****..\n",
527 "*...*....*...*..*...*..*..**....*....*...*..*...*.\n",
528 "....*....*...*..*...*..*...*....*.....***...*...*.\n",
529 "..................................................\n",
530 "\n",
531 "left 3 7\n",
532 "..................................................\n",
533 "*.*****..****....***...*...*..*****...***...****..\n",
534 "....*....*...*..*...*..**..*....*....*...*..*...*.\n",
535 "....*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
536 "....*....****...*****..*.*.*....*....*...*..****..\n",
537 "....*....*...*..*...*..*..**....*....*...*..*...*.\n",
538 "*...*....*...*..*...*..*...*....*.....***...*...*.\n",
539 "..................................................\n",
540 "\n",
541 "rotate column 3 1\n",
542 "..*...............................................\n",
543 "*..****..****....***...*...*..*****...***...****..\n",
544 "....*....*...*..*...*..**..*....*....*...*..*...*.\n",
545 "....*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
546 "....*....****...*****..*.*.*....*....*...*..****..\n",
547 "....*....*...*..*...*..*..**....*....*...*..*...*.\n",
548 "*...*....*...*..*...*..*...*....*.....***...*...*.\n",
549 "..................................................\n",
550 "\n",
551 "Final\n",
552 "..*...............................................\n",
553 "*..****..****....***...*...*..*****...***...****..\n",
554 "....*....*...*..*...*..**..*....*....*...*..*...*.\n",
555 "....*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
556 "....*....****...*****..*.*.*....*....*...*..****..\n",
557 "....*....*...*..*...*..*..**....*....*...*..*...*.\n",
558 "*...*....*...*..*...*..*...*....*.....***...*...*.\n",
559 "..................................................\n"
560 ]
561 },
562 {
563 "data": {
564 "text/plain": [
565 "['..*...............................................',\n",
566 " '*..****..****....***...*...*..*****...***...****..',\n",
567 " '....*....*...*..*...*..**..*....*....*...*..*...*.',\n",
568 " '....*....*..*...*...*..*.*.*....*....*...*..*..*..',\n",
569 " '....*....****...*****..*.*.*....*....*...*..****..',\n",
570 " '....*....*...*..*...*..*..**....*....*...*..*...*.',\n",
571 " '*...*....*...*..*...*..*...*....*.....***...*...*.',\n",
572 " '..................................................']"
573 ]
574 },
575 "execution_count": 20,
576 "metadata": {},
577 "output_type": "execute_result"
578 }
579 ],
580 "source": [
581 "trantor_grid = '''\n",
582 "..................................................\n",
583 "..*****..****....***...*...*..*****...***...****..\n",
584 "....*....*...*..*...*..**..*....*....*...*..*...*.\n",
585 "....*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
586 "....*....****...*****..*.*.*....*....*...*..****..\n",
587 "....*....*...*..*...*..*..**....*....*...*..*...*.\n",
588 "....*....*...*..*...*..*...*....*.....***...*...*.\n",
589 "..................................................\n",
590 "'''.split('\\n')[1:-1]\n",
591 "\n",
592 "cmds = '''\n",
593 "rotate column 3 1\n",
594 "left 3 7\n",
595 "rotate column 1 1\n",
596 "left 3 7\n",
597 "'''.split('\\n')[1:-1]\n",
598 "interpret(cmds, trantor_grid, show_each_step=True, uninterpret=True)"
599 ]
600 },
601 {
602 "cell_type": "code",
603 "execution_count": 21,
604 "metadata": {
605 "collapsed": true
606 },
607 "outputs": [],
608 "source": [
609 "def transpose(grid):\n",
610 " return list(zip(*grid))"
611 ]
612 },
613 {
614 "cell_type": "code",
615 "execution_count": 22,
616 "metadata": {
617 "scrolled": true
618 },
619 "outputs": [
620 {
621 "data": {
622 "text/plain": [
623 "[('.', '*', '.', '.', '.', '.', '*', '.'),\n",
624 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
625 " ('*', '.', '.', '.', '.', '.', '.', '.'),\n",
626 " ('.', '*', '.', '.', '.', '.', '.', '.'),\n",
627 " ('.', '*', '*', '*', '*', '*', '*', '.'),\n",
628 " ('.', '*', '.', '.', '.', '.', '.', '.'),\n",
629 " ('.', '*', '.', '.', '.', '.', '.', '.'),\n",
630 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
631 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
632 " ('.', '*', '*', '*', '*', '*', '*', '.'),\n",
633 " ('.', '*', '.', '.', '*', '.', '.', '.'),\n",
634 " ('.', '*', '.', '.', '*', '.', '.', '.'),\n",
635 " ('.', '*', '.', '*', '*', '.', '.', '.'),\n",
636 " ('.', '.', '*', '.', '.', '*', '*', '.'),\n",
637 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
638 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
639 " ('.', '.', '*', '*', '*', '*', '*', '.'),\n",
640 " ('.', '*', '.', '.', '*', '.', '.', '.'),\n",
641 " ('.', '*', '.', '.', '*', '.', '.', '.'),\n",
642 " ('.', '*', '.', '.', '*', '.', '.', '.'),\n",
643 " ('.', '.', '*', '*', '*', '*', '*', '.'),\n",
644 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
645 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
646 " ('.', '*', '*', '*', '*', '*', '*', '.'),\n",
647 " ('.', '.', '*', '.', '.', '.', '.', '.'),\n",
648 " ('.', '.', '.', '*', '*', '.', '.', '.'),\n",
649 " ('.', '.', '.', '.', '.', '*', '.', '.'),\n",
650 " ('.', '*', '*', '*', '*', '*', '*', '.'),\n",
651 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
652 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
653 " ('.', '*', '.', '.', '.', '.', '.', '.'),\n",
654 " ('.', '*', '.', '.', '.', '.', '.', '.'),\n",
655 " ('.', '*', '*', '*', '*', '*', '*', '.'),\n",
656 " ('.', '*', '.', '.', '.', '.', '.', '.'),\n",
657 " ('.', '*', '.', '.', '.', '.', '.', '.'),\n",
658 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
659 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
660 " ('.', '.', '*', '*', '*', '*', '.', '.'),\n",
661 " ('.', '*', '.', '.', '.', '.', '*', '.'),\n",
662 " ('.', '*', '.', '.', '.', '.', '*', '.'),\n",
663 " ('.', '*', '.', '.', '.', '.', '*', '.'),\n",
664 " ('.', '.', '*', '*', '*', '*', '.', '.'),\n",
665 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
666 " ('.', '.', '.', '.', '.', '.', '.', '.'),\n",
667 " ('.', '*', '*', '*', '*', '*', '*', '.'),\n",
668 " ('.', '*', '.', '.', '*', '.', '.', '.'),\n",
669 " ('.', '*', '.', '.', '*', '.', '.', '.'),\n",
670 " ('.', '*', '.', '*', '*', '.', '.', '.'),\n",
671 " ('.', '.', '*', '.', '.', '*', '*', '.'),\n",
672 " ('.', '.', '.', '.', '.', '.', '.', '.')]"
673 ]
674 },
675 "execution_count": 22,
676 "metadata": {},
677 "output_type": "execute_result"
678 }
679 ],
680 "source": [
681 "transpose(trantor_grid)"
682 ]
683 },
684 {
685 "cell_type": "code",
686 "execution_count": 23,
687 "metadata": {
688 "collapsed": true
689 },
690 "outputs": [],
691 "source": [
692 "def count_empty_rows(grid, col):\n",
693 " return len(list(itertools.takewhile(lambda c: c == '.', transpose(grid)[col-1])))"
694 ]
695 },
696 {
697 "cell_type": "code",
698 "execution_count": 24,
699 "metadata": {
700 "scrolled": true
701 },
702 "outputs": [
703 {
704 "data": {
705 "text/plain": [
706 "[1,\n",
707 " 8,\n",
708 " 0,\n",
709 " 1,\n",
710 " 1,\n",
711 " 1,\n",
712 " 1,\n",
713 " 8,\n",
714 " 8,\n",
715 " 1,\n",
716 " 1,\n",
717 " 1,\n",
718 " 1,\n",
719 " 2,\n",
720 " 8,\n",
721 " 8,\n",
722 " 2,\n",
723 " 1,\n",
724 " 1,\n",
725 " 1,\n",
726 " 2,\n",
727 " 8,\n",
728 " 8,\n",
729 " 1,\n",
730 " 2,\n",
731 " 3,\n",
732 " 5,\n",
733 " 1,\n",
734 " 8,\n",
735 " 8,\n",
736 " 1,\n",
737 " 1,\n",
738 " 1,\n",
739 " 1,\n",
740 " 1,\n",
741 " 8,\n",
742 " 8,\n",
743 " 2,\n",
744 " 1,\n",
745 " 1,\n",
746 " 1,\n",
747 " 2,\n",
748 " 8,\n",
749 " 8,\n",
750 " 1,\n",
751 " 1,\n",
752 " 1,\n",
753 " 1,\n",
754 " 2,\n",
755 " 8]"
756 ]
757 },
758 "execution_count": 24,
759 "metadata": {},
760 "output_type": "execute_result"
761 }
762 ],
763 "source": [
764 "[count_empty_rows(trantor_grid, i) for i in range(1, WIDTH+1)]"
765 ]
766 },
767 {
768 "cell_type": "code",
769 "execution_count": 25,
770 "metadata": {
771 "collapsed": true
772 },
773 "outputs": [],
774 "source": [
775 "def lift_cols(grid, randomise=False):\n",
776 " commands = []\n",
777 " h = len(grid)\n",
778 " for i in range(1, len(grid[0])+1):\n",
779 " n = count_empty_rows(grid, i)\n",
780 " if n != h:\n",
781 " commands.append('rotate column {} {}'.format(i, n))\n",
782 " if randomise:\n",
783 " random.shuffle(commands)\n",
784 " return commands"
785 ]
786 },
787 {
788 "cell_type": "code",
789 "execution_count": 26,
790 "metadata": {
791 "scrolled": true
792 },
793 "outputs": [
794 {
795 "data": {
796 "text/plain": [
797 "['rotate column 3 1',\n",
798 " 'rotate column 4 1',\n",
799 " 'rotate column 5 1',\n",
800 " 'rotate column 6 1',\n",
801 " 'rotate column 7 1',\n",
802 " 'rotate column 10 1',\n",
803 " 'rotate column 11 1',\n",
804 " 'rotate column 12 1',\n",
805 " 'rotate column 13 1',\n",
806 " 'rotate column 14 2',\n",
807 " 'rotate column 17 2',\n",
808 " 'rotate column 18 1',\n",
809 " 'rotate column 19 1',\n",
810 " 'rotate column 20 1',\n",
811 " 'rotate column 21 2',\n",
812 " 'rotate column 24 1',\n",
813 " 'rotate column 25 2',\n",
814 " 'rotate column 26 3',\n",
815 " 'rotate column 27 5',\n",
816 " 'rotate column 28 1',\n",
817 " 'rotate column 31 1',\n",
818 " 'rotate column 32 1',\n",
819 " 'rotate column 33 1',\n",
820 " 'rotate column 34 1',\n",
821 " 'rotate column 35 1',\n",
822 " 'rotate column 38 2',\n",
823 " 'rotate column 39 1',\n",
824 " 'rotate column 40 1',\n",
825 " 'rotate column 41 1',\n",
826 " 'rotate column 42 2',\n",
827 " 'rotate column 45 1',\n",
828 " 'rotate column 46 1',\n",
829 " 'rotate column 47 1',\n",
830 " 'rotate column 48 1',\n",
831 " 'rotate column 49 2']"
832 ]
833 },
834 "execution_count": 26,
835 "metadata": {},
836 "output_type": "execute_result"
837 }
838 ],
839 "source": [
840 "lift_cols(tg())"
841 ]
842 },
843 {
844 "cell_type": "code",
845 "execution_count": 27,
846 "metadata": {
847 "scrolled": true
848 },
849 "outputs": [
850 {
851 "data": {
852 "text/plain": [
853 "['rotate column 24 1',\n",
854 " 'rotate column 41 1',\n",
855 " 'rotate column 10 1',\n",
856 " 'rotate column 18 1',\n",
857 " 'rotate column 39 1',\n",
858 " 'rotate column 3 1',\n",
859 " 'rotate column 6 1',\n",
860 " 'rotate column 14 2',\n",
861 " 'rotate column 42 2',\n",
862 " 'rotate column 32 1',\n",
863 " 'rotate column 20 1',\n",
864 " 'rotate column 38 2',\n",
865 " 'rotate column 34 1',\n",
866 " 'rotate column 31 1',\n",
867 " 'rotate column 35 1',\n",
868 " 'rotate column 28 1',\n",
869 " 'rotate column 45 1',\n",
870 " 'rotate column 13 1',\n",
871 " 'rotate column 40 1',\n",
872 " 'rotate column 5 1',\n",
873 " 'rotate column 4 1',\n",
874 " 'rotate column 12 1',\n",
875 " 'rotate column 47 1',\n",
876 " 'rotate column 25 2',\n",
877 " 'rotate column 7 1',\n",
878 " 'rotate column 49 2',\n",
879 " 'rotate column 46 1',\n",
880 " 'rotate column 26 3',\n",
881 " 'rotate column 27 5',\n",
882 " 'rotate column 19 1',\n",
883 " 'rotate column 11 1',\n",
884 " 'rotate column 17 2',\n",
885 " 'rotate column 48 1',\n",
886 " 'rotate column 33 1',\n",
887 " 'rotate column 21 2']"
888 ]
889 },
890 "execution_count": 27,
891 "metadata": {},
892 "output_type": "execute_result"
893 }
894 ],
895 "source": [
896 "lift_cols(tg(), randomise=True)"
897 ]
898 },
899 {
900 "cell_type": "code",
901 "execution_count": 28,
902 "metadata": {
903 "collapsed": true
904 },
905 "outputs": [],
906 "source": [
907 "def count_empty_cols(grid, row):\n",
908 " return len(list(itertools.takewhile(lambda r: r == '.', grid[row-1])))"
909 ]
910 },
911 {
912 "cell_type": "code",
913 "execution_count": 29,
914 "metadata": {},
915 "outputs": [
916 {
917 "data": {
918 "text/plain": [
919 "[2, 0, 4, 4, 4, 4, 0, 50]"
920 ]
921 },
922 "execution_count": 29,
923 "metadata": {},
924 "output_type": "execute_result"
925 }
926 ],
927 "source": [
928 "[count_empty_cols(trantor_grid, i) for i in range(1, HEIGHT+1)]"
929 ]
930 },
931 {
932 "cell_type": "code",
933 "execution_count": 30,
934 "metadata": {
935 "collapsed": true
936 },
937 "outputs": [],
938 "source": [
939 "def slide_rows(grid, randomise=False):\n",
940 " commands = []\n",
941 " w = len(grid[0])\n",
942 " for i in range(1, len(grid)+1):\n",
943 " n = count_empty_cols(grid, i)\n",
944 " if n != w:\n",
945 " commands.append('rotate row {} {}'.format(i, n))\n",
946 " if randomise:\n",
947 " random.shuffle(commands)\n",
948 " return commands"
949 ]
950 },
951 {
952 "cell_type": "code",
953 "execution_count": 31,
954 "metadata": {
955 "collapsed": true
956 },
957 "outputs": [],
958 "source": [
959 "def untop(grid, randomise=False):\n",
960 " groups = [(k, len(list(g))) for k, g in itertools.groupby(grid[0])]\n",
961 " commands = []\n",
962 " col = 1\n",
963 " for c, l in groups:\n",
964 " if c == '*':\n",
965 " commands.append('top {} {}'.format(col, col + l - 1))\n",
966 " col += l\n",
967 " if randomise:\n",
968 " random.shuffle(commands)\n",
969 " return commands"
970 ]
971 },
972 {
973 "cell_type": "code",
974 "execution_count": 32,
975 "metadata": {
976 "collapsed": true
977 },
978 "outputs": [],
979 "source": [
980 "def unleft(grid, randomise=False):\n",
981 " groups = [(k, len(list(g))) for k, g in itertools.groupby(transpose(grid)[0])]\n",
982 " commands = []\n",
983 " row = 1\n",
984 " for c, l in groups:\n",
985 " if c == '*':\n",
986 " commands.append('left {} {}'.format(row, row + l - 1))\n",
987 " row += l\n",
988 " if randomise:\n",
989 " random.shuffle(commands)\n",
990 " return commands"
991 ]
992 },
993 {
994 "cell_type": "code",
995 "execution_count": 33,
996 "metadata": {},
997 "outputs": [
998 {
999 "data": {
1000 "text/plain": [
1001 "['***********.******.*****.*..***********.******....',\n",
1002 " '*....*...*..*...*..*...*....*....*...*..*...*.....',\n",
1003 " '*....*..*...*...*..*.*.*....*....*...*..*..*......',\n",
1004 " '*....*.**...*..**..*.*.*....*....*...*..*.**......',\n",
1005 " '*....*...*..*...*..*...*....*........*..*...*.....',\n",
1006 " '*........*......*...........*.......*.......*.....',\n",
1007 " '..................................................',\n",
1008 " '..................................................']"
1009 ]
1010 },
1011 "execution_count": 33,
1012 "metadata": {},
1013 "output_type": "execute_result"
1014 }
1015 ],
1016 "source": [
1017 "trantor_grid = tg() \n",
1018 "\n",
1019 "cmds = []\n",
1020 "\n",
1021 "c = slide_rows(trantor_grid)\n",
1022 "cmds = c + cmds\n",
1023 "\n",
1024 "interpret(c, trantor_grid, uninterpret=True)\n",
1025 "\n",
1026 "c = lift_cols(trantor_grid)\n",
1027 "cmds = c + cmds\n",
1028 "\n",
1029 "interpret(c, trantor_grid, uninterpret=True)"
1030 ]
1031 },
1032 {
1033 "cell_type": "code",
1034 "execution_count": 34,
1035 "metadata": {},
1036 "outputs": [
1037 {
1038 "data": {
1039 "text/plain": [
1040 "['top 1 11', 'top 13 18', 'top 20 24', 'top 26 26', 'top 29 39', 'top 41 46']"
1041 ]
1042 },
1043 "execution_count": 34,
1044 "metadata": {},
1045 "output_type": "execute_result"
1046 }
1047 ],
1048 "source": [
1049 "g = interpret(cmds, tg(), uninterpret=True)\n",
1050 "untop(g)"
1051 ]
1052 },
1053 {
1054 "cell_type": "code",
1055 "execution_count": 35,
1056 "metadata": {},
1057 "outputs": [
1058 {
1059 "name": "stdout",
1060 "output_type": "stream",
1061 "text": [
1062 "..................................................\n",
1063 ".....*...*..*...*..*...*....*....*...*..*...*.....\n",
1064 ".....*..*...*...*..*.*.*....*....*...*..*..*......\n",
1065 ".....*.**...*..**..*.*.*....*....*...*..*.**......\n",
1066 ".....*...*..*...*..*...*....*........*..*...*.....\n",
1067 ".........*......*...........*.......*.......*.....\n",
1068 "..................................................\n",
1069 "..................................................\n"
1070 ]
1071 }
1072 ],
1073 "source": [
1074 "g = tg() \n",
1075 "\n",
1076 "cmds = []\n",
1077 "\n",
1078 "c = slide_rows(g, randomise=True)\n",
1079 "interpret(c, g, uninterpret=True)\n",
1080 "cmds = c + cmds\n",
1081 "\n",
1082 "c = lift_cols(g, randomise=True)\n",
1083 "interpret(c, g, uninterpret=True)\n",
1084 "cmds = c + cmds\n",
1085 "\n",
1086 "c = untop(g, randomise=True)\n",
1087 "interpret(c, g, uninterpret=True)\n",
1088 "cmds = c + cmds\n",
1089 "\n",
1090 "c = unleft(g, randomise=True)\n",
1091 "interpret(c, g, uninterpret=True)\n",
1092 "cmds = c + cmds\n",
1093 "\n",
1094 "print_grid(g)"
1095 ]
1096 },
1097 {
1098 "cell_type": "code",
1099 "execution_count": 36,
1100 "metadata": {},
1101 "outputs": [
1102 {
1103 "name": "stdout",
1104 "output_type": "stream",
1105 "text": [
1106 "..................................................\n",
1107 "..................................................\n",
1108 "..................................................\n",
1109 "..................................................\n",
1110 "..................................................\n",
1111 "..................................................\n",
1112 "..................................................\n",
1113 "..................................................\n"
1114 ]
1115 },
1116 {
1117 "data": {
1118 "text/plain": [
1119 "139"
1120 ]
1121 },
1122 "execution_count": 36,
1123 "metadata": {},
1124 "output_type": "execute_result"
1125 }
1126 ],
1127 "source": [
1128 "g = tg() \n",
1129 "cmds = []\n",
1130 "\n",
1131 "while '*' in ''.join(g):\n",
1132 " if random.choice([True, False]):\n",
1133 " c = slide_rows(g, randomise=True)\n",
1134 " interpret(c, g, uninterpret=True)\n",
1135 " cmds = c + cmds\n",
1136 " \n",
1137 " c = unleft(g, randomise=True)\n",
1138 " interpret(c, g, uninterpret=True)\n",
1139 " cmds = c + cmds\n",
1140 " \n",
1141 " else:\n",
1142 " c = lift_cols(g, randomise=True)\n",
1143 " interpret(c, g, uninterpret=True)\n",
1144 " cmds = c + cmds\n",
1145 " \n",
1146 " c = untop(g, randomise=True)\n",
1147 " interpret(c, g, uninterpret=True)\n",
1148 " cmds = c + cmds\n",
1149 " \n",
1150 "print_grid(g)\n",
1151 "len(cmds)"
1152 ]
1153 },
1154 {
1155 "cell_type": "code",
1156 "execution_count": 37,
1157 "metadata": {},
1158 "outputs": [
1159 {
1160 "data": {
1161 "text/plain": [
1162 "['..................................................',\n",
1163 " '..................................................',\n",
1164 " '..................................................',\n",
1165 " '..................................................',\n",
1166 " '..................................................',\n",
1167 " '..................................................',\n",
1168 " '..................................................',\n",
1169 " '..................................................']"
1170 ]
1171 },
1172 "execution_count": 37,
1173 "metadata": {},
1174 "output_type": "execute_result"
1175 }
1176 ],
1177 "source": [
1178 "g = tg()\n",
1179 "interpret(cmds, g, uninterpret=True)"
1180 ]
1181 },
1182 {
1183 "cell_type": "code",
1184 "execution_count": 38,
1185 "metadata": {},
1186 "outputs": [
1187 {
1188 "data": {
1189 "text/plain": [
1190 "['..................................................',\n",
1191 " '..*****..****....***...*...*..*****...***...****..',\n",
1192 " '....*....*...*..*...*..**..*....*....*...*..*...*.',\n",
1193 " '....*....*..*...*...*..*.*.*....*....*...*..*..*..',\n",
1194 " '....*....****...*****..*.*.*....*....*...*..****..',\n",
1195 " '....*....*...*..*...*..*..**....*....*...*..*...*.',\n",
1196 " '....*....*...*..*...*..*...*....*.....***...*...*.',\n",
1197 " '..................................................']"
1198 ]
1199 },
1200 "execution_count": 38,
1201 "metadata": {},
1202 "output_type": "execute_result"
1203 }
1204 ],
1205 "source": [
1206 "interpret(cmds)"
1207 ]
1208 },
1209 {
1210 "cell_type": "code",
1211 "execution_count": 39,
1212 "metadata": {},
1213 "outputs": [
1214 {
1215 "data": {
1216 "text/plain": [
1217 "['top 5 5',\n",
1218 " 'top 8 8',\n",
1219 " 'top 29 29',\n",
1220 " 'top 12 12',\n",
1221 " 'rotate column 29 1',\n",
1222 " 'rotate column 8 1',\n",
1223 " 'rotate column 12 1',\n",
1224 " 'rotate column 5 1',\n",
1225 " 'top 12 12',\n",
1226 " 'top 22 22',\n",
1227 " 'top 17 17',\n",
1228 " 'top 29 29',\n",
1229 " 'top 5 5',\n",
1230 " 'top 26 26',\n",
1231 " 'top 8 8',\n",
1232 " 'rotate column 5 1',\n",
1233 " 'rotate column 26 1',\n",
1234 " 'rotate column 12 1',\n",
1235 " 'rotate column 29 1',\n",
1236 " 'rotate column 8 1',\n",
1237 " 'rotate column 22 1',\n",
1238 " 'rotate column 17 1',\n",
1239 " 'top 21 22',\n",
1240 " 'top 29 30',\n",
1241 " 'top 5 5',\n",
1242 " 'top 2 2',\n",
1243 " 'top 33 33',\n",
1244 " 'top 14 14',\n",
1245 " 'top 16 17',\n",
1246 " 'top 37 37',\n",
1247 " 'top 12 12',\n",
1248 " 'top 26 26',\n",
1249 " 'top 8 9',\n",
1250 " 'rotate column 30 1',\n",
1251 " 'rotate column 5 1',\n",
1252 " 'rotate column 22 2',\n",
1253 " 'rotate column 33 1',\n",
1254 " 'rotate column 12 1',\n",
1255 " 'rotate column 21 1',\n",
1256 " 'rotate column 16 1',\n",
1257 " 'rotate column 2 1',\n",
1258 " 'rotate column 14 1',\n",
1259 " 'rotate column 9 1',\n",
1260 " 'rotate column 37 1',\n",
1261 " 'rotate column 26 1',\n",
1262 " 'rotate column 8 2',\n",
1263 " 'rotate column 17 2',\n",
1264 " 'rotate column 29 2',\n",
1265 " 'left 2 5',\n",
1266 " 'rotate row 2 3',\n",
1267 " 'rotate row 4 7',\n",
1268 " 'rotate row 5 7',\n",
1269 " 'rotate row 3 7',\n",
1270 " 'top 19 19',\n",
1271 " 'top 29 33',\n",
1272 " 'top 12 12',\n",
1273 " 'top 24 24',\n",
1274 " 'top 8 10',\n",
1275 " 'top 15 17',\n",
1276 " 'top 2 5',\n",
1277 " 'top 36 40',\n",
1278 " 'rotate column 9 3',\n",
1279 " 'rotate column 30 5',\n",
1280 " 'rotate column 37 3',\n",
1281 " 'rotate column 8 1',\n",
1282 " 'rotate column 4 2',\n",
1283 " 'rotate column 3 3',\n",
1284 " 'rotate column 19 1',\n",
1285 " 'rotate column 32 5',\n",
1286 " 'rotate column 40 3',\n",
1287 " 'rotate column 2 3',\n",
1288 " 'rotate column 29 1',\n",
1289 " 'rotate column 36 1',\n",
1290 " 'rotate column 24 1',\n",
1291 " 'rotate column 5 3',\n",
1292 " 'rotate column 31 5',\n",
1293 " 'rotate column 39 1',\n",
1294 " 'rotate column 15 1',\n",
1295 " 'rotate column 17 2',\n",
1296 " 'rotate column 38 3',\n",
1297 " 'rotate column 33 1',\n",
1298 " 'rotate column 12 1',\n",
1299 " 'rotate column 16 1',\n",
1300 " 'rotate column 10 3',\n",
1301 " 'top 15 19',\n",
1302 " 'top 24 25',\n",
1303 " 'top 7 12',\n",
1304 " 'top 21 21',\n",
1305 " 'top 36 40',\n",
1306 " 'top 2 5',\n",
1307 " 'top 28 33',\n",
1308 " 'top 42 45',\n",
1309 " 'rotate column 33 2',\n",
1310 " 'rotate column 3 1',\n",
1311 " 'rotate column 28 1',\n",
1312 " 'rotate column 24 2',\n",
1313 " 'rotate column 12 2',\n",
1314 " 'rotate column 36 1',\n",
1315 " 'rotate column 11 4',\n",
1316 " 'rotate column 10 1',\n",
1317 " 'rotate column 31 1',\n",
1318 " 'rotate column 5 2',\n",
1319 " 'rotate column 39 3',\n",
1320 " 'rotate column 32 1',\n",
1321 " 'rotate column 43 1',\n",
1322 " 'rotate column 18 5',\n",
1323 " 'rotate column 16 1',\n",
1324 " 'rotate column 45 1',\n",
1325 " 'rotate column 9 1',\n",
1326 " 'rotate column 40 2',\n",
1327 " 'rotate column 42 1',\n",
1328 " 'rotate column 4 1',\n",
1329 " 'rotate column 2 1',\n",
1330 " 'rotate column 44 1',\n",
1331 " 'rotate column 30 1',\n",
1332 " 'rotate column 19 2',\n",
1333 " 'rotate column 7 1',\n",
1334 " 'rotate column 38 1',\n",
1335 " 'rotate column 25 1',\n",
1336 " 'rotate column 21 1',\n",
1337 " 'rotate column 37 1',\n",
1338 " 'rotate column 8 1',\n",
1339 " 'rotate column 15 1',\n",
1340 " 'rotate column 17 1',\n",
1341 " 'rotate column 29 1',\n",
1342 " 'left 2 7',\n",
1343 " 'rotate row 7 5',\n",
1344 " 'rotate row 6 5',\n",
1345 " 'rotate row 5 5',\n",
1346 " 'rotate row 3 5',\n",
1347 " 'rotate row 4 5',\n",
1348 " 'rotate row 2 1',\n",
1349 " 'left 2 7',\n",
1350 " 'rotate row 2 2',\n",
1351 " 'rotate row 5 4',\n",
1352 " 'rotate row 7 4',\n",
1353 " 'rotate row 6 4',\n",
1354 " 'rotate row 3 4',\n",
1355 " 'rotate row 4 4']"
1356 ]
1357 },
1358 "execution_count": 39,
1359 "metadata": {},
1360 "output_type": "execute_result"
1361 }
1362 ],
1363 "source": [
1364 "cmds"
1365 ]
1366 },
1367 {
1368 "cell_type": "code",
1369 "execution_count": 40,
1370 "metadata": {},
1371 "outputs": [
1372 {
1373 "name": "stdout",
1374 "output_type": "stream",
1375 "text": [
1376 "Final\n",
1377 "..................................................\n",
1378 "..*****..****....***...*...*..*****...***...****..\n",
1379 "....*....*...*..*...*..**..*....*....*...*..*...*.\n",
1380 "....*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
1381 "....*....****...*****..*.*.*....*....*...*..****..\n",
1382 "....*....*...*..*...*..*..**....*....*...*..*...*.\n",
1383 "....*....*...*..*...*..*...*....*.....***...*...*.\n",
1384 "..................................................\n"
1385 ]
1386 },
1387 {
1388 "data": {
1389 "text/plain": [
1390 "['..................................................',\n",
1391 " '..*****..****....***...*...*..*****...***...****..',\n",
1392 " '....*....*...*..*...*..**..*....*....*...*..*...*.',\n",
1393 " '....*....*..*...*...*..*.*.*....*....*...*..*..*..',\n",
1394 " '....*....****...*****..*.*.*....*....*...*..****..',\n",
1395 " '....*....*...*..*...*..*..**....*....*...*..*...*.',\n",
1396 " '....*....*...*..*...*..*...*....*.....***...*...*.',\n",
1397 " '..................................................']"
1398 ]
1399 },
1400 "execution_count": 40,
1401 "metadata": {},
1402 "output_type": "execute_result"
1403 }
1404 ],
1405 "source": [
1406 "g = new_grid()\n",
1407 "interpret(cmds, g, show_each_step=True, overprint=True)"
1408 ]
1409 },
1410 {
1411 "cell_type": "code",
1412 "execution_count": 41,
1413 "metadata": {},
1414 "outputs": [
1415 {
1416 "name": "stdout",
1417 "output_type": "stream",
1418 "text": [
1419 "Final\n",
1420 "..................................................\n",
1421 "..................................................\n",
1422 "..................................................\n",
1423 "..................................................\n",
1424 "..................................................\n",
1425 "..................................................\n",
1426 "..................................................\n",
1427 "..................................................\n"
1428 ]
1429 },
1430 {
1431 "data": {
1432 "text/plain": [
1433 "['..................................................',\n",
1434 " '..................................................',\n",
1435 " '..................................................',\n",
1436 " '..................................................',\n",
1437 " '..................................................',\n",
1438 " '..................................................',\n",
1439 " '..................................................',\n",
1440 " '..................................................']"
1441 ]
1442 },
1443 "execution_count": 41,
1444 "metadata": {},
1445 "output_type": "execute_result"
1446 }
1447 ],
1448 "source": [
1449 "g = tg()\n",
1450 "interpret(cmds, g, show_each_step=True, overprint=True, uninterpret=True)"
1451 ]
1452 },
1453 {
1454 "cell_type": "code",
1455 "execution_count": 42,
1456 "metadata": {},
1457 "outputs": [
1458 {
1459 "data": {
1460 "text/plain": [
1461 "98"
1462 ]
1463 },
1464 "execution_count": 42,
1465 "metadata": {},
1466 "output_type": "execute_result"
1467 }
1468 ],
1469 "source": [
1470 "sum(1 for c in ''.join(tg()) if c == '*')"
1471 ]
1472 },
1473 {
1474 "cell_type": "code",
1475 "execution_count": 43,
1476 "metadata": {},
1477 "outputs": [
1478 {
1479 "data": {
1480 "text/plain": [
1481 "2163"
1482 ]
1483 },
1484 "execution_count": 43,
1485 "metadata": {},
1486 "output_type": "execute_result"
1487 }
1488 ],
1489 "source": [
1490 "open('05-pixels.txt', 'w').write('\\n'.join(cmds))"
1491 ]
1492 },
1493 {
1494 "cell_type": "code",
1495 "execution_count": 44,
1496 "metadata": {},
1497 "outputs": [
1498 {
1499 "name": "stdout",
1500 "output_type": "stream",
1501 "text": [
1502 "................................................................................\n",
1503 "................................................................................\n",
1504 "................................................................................\n",
1505 "................................................................................\n",
1506 "................................................................................\n",
1507 "................................................................................\n",
1508 "................................................................................\n",
1509 "................................................................................\n"
1510 ]
1511 },
1512 {
1513 "data": {
1514 "text/plain": [
1515 "237"
1516 ]
1517 },
1518 "execution_count": 44,
1519 "metadata": {},
1520 "output_type": "execute_result"
1521 }
1522 ],
1523 "source": [
1524 "g = jantar_mantar() \n",
1525 "cmds = []\n",
1526 "\n",
1527 "while '*' in ''.join(g):\n",
1528 " if random.choice([True, False]):\n",
1529 " c = slide_rows(g, randomise=True)\n",
1530 " interpret(c, g, uninterpret=True)\n",
1531 " cmds = c + cmds\n",
1532 " \n",
1533 " c = unleft(g, randomise=True)\n",
1534 " interpret(c, g, uninterpret=True)\n",
1535 " cmds = c + cmds\n",
1536 " \n",
1537 " else:\n",
1538 " c = lift_cols(g, randomise=True)\n",
1539 " interpret(c, g, uninterpret=True)\n",
1540 " cmds = c + cmds\n",
1541 " \n",
1542 " c = untop(g, randomise=True)\n",
1543 " interpret(c, g, uninterpret=True)\n",
1544 " cmds = c + cmds\n",
1545 " \n",
1546 "print_grid(g)\n",
1547 "len(cmds)"
1548 ]
1549 },
1550 {
1551 "cell_type": "code",
1552 "execution_count": 45,
1553 "metadata": {},
1554 "outputs": [
1555 {
1556 "name": "stdout",
1557 "output_type": "stream",
1558 "text": [
1559 "Final\n",
1560 "...****..............*...................*.....*..............*.................\n",
1561 "......*..............*...................***..**..............*.................\n",
1562 "......*.*****.*****.****.*****..****.....*.*.***.*****.*****.****.*****..****...\n",
1563 "......*.....*.*...*..*.......*..*........*..**.*.....*.*...*..*.......*..*......\n",
1564 "......*.*****.*...*..*...*****..*........*..*..*.*****.*...*..*...*****..*......\n",
1565 "......*.*...*.*...*..*...*...*..*........*.....*.*...*.*...*..*...*...*..*......\n",
1566 "...*..*.*..**.*...*..**..*..**..*........*.....*.*..**.*...*..**..*..**..*......\n",
1567 "....**...**.*.*...*...**..**.*..*........*.....*..**.*.*...*...**..**.*..*......\n"
1568 ]
1569 },
1570 {
1571 "data": {
1572 "text/plain": [
1573 "['...****..............*...................*.....*..............*.................',\n",
1574 " '......*..............*...................***..**..............*.................',\n",
1575 " '......*.*****.*****.****.*****..****.....*.*.***.*****.*****.****.*****..****...',\n",
1576 " '......*.....*.*...*..*.......*..*........*..**.*.....*.*...*..*.......*..*......',\n",
1577 " '......*.*****.*...*..*...*****..*........*..*..*.*****.*...*..*...*****..*......',\n",
1578 " '......*.*...*.*...*..*...*...*..*........*.....*.*...*.*...*..*...*...*..*......',\n",
1579 " '...*..*.*..**.*...*..**..*..**..*........*.....*.*..**.*...*..**..*..**..*......',\n",
1580 " '....**...**.*.*...*...**..**.*..*........*.....*..**.*.*...*...**..**.*..*......']"
1581 ]
1582 },
1583 "execution_count": 45,
1584 "metadata": {},
1585 "output_type": "execute_result"
1586 }
1587 ],
1588 "source": [
1589 "g = new_grid(w=80)\n",
1590 "interpret(cmds, g, show_each_step=True, overprint=True)"
1591 ]
1592 },
1593 {
1594 "cell_type": "code",
1595 "execution_count": 46,
1596 "metadata": {},
1597 "outputs": [
1598 {
1599 "data": {
1600 "text/plain": [
1601 "80"
1602 ]
1603 },
1604 "execution_count": 46,
1605 "metadata": {},
1606 "output_type": "execute_result"
1607 }
1608 ],
1609 "source": [
1610 "len(jantar_mantar()[0])"
1611 ]
1612 },
1613 {
1614 "cell_type": "code",
1615 "execution_count": 47,
1616 "metadata": {},
1617 "outputs": [
1618 {
1619 "data": {
1620 "text/plain": [
1621 "3900"
1622 ]
1623 },
1624 "execution_count": 47,
1625 "metadata": {},
1626 "output_type": "execute_result"
1627 }
1628 ],
1629 "source": [
1630 "open('05-pixels.txt', 'w').write('\\n'.join(cmds))"
1631 ]
1632 },
1633 {
1634 "cell_type": "code",
1635 "execution_count": 48,
1636 "metadata": {},
1637 "outputs": [
1638 {
1639 "name": "stdout",
1640 "output_type": "stream",
1641 "text": [
1642 " **** * * * * \n",
1643 " * * *** ** * \n",
1644 " * ***** ***** **** ***** **** * * *** ***** ***** **** ***** **** \n",
1645 " * * * * * * * * ** * * * * * * * \n",
1646 " * ***** * * * ***** * * * * ***** * * * ***** * \n",
1647 " * * * * * * * * * * * * * * * * * * * \n",
1648 " * * * ** * * ** * ** * * * * ** * * ** * ** * \n",
1649 " ** ** * * * ** ** * * * * ** * * * ** ** * * \n"
1650 ]
1651 }
1652 ],
1653 "source": [
1654 "print_grid(jantar_mantar(), suppress_dots=True)"
1655 ]
1656 },
1657 {
1658 "cell_type": "code",
1659 "execution_count": 49,
1660 "metadata": {},
1661 "outputs": [
1662 {
1663 "name": "stdout",
1664 "output_type": "stream",
1665 "text": [
1666 "Final\n",
1667 " **** * * * * \n",
1668 " * * *** ** * \n",
1669 " * ***** ***** **** ***** **** * * *** ***** ***** **** ***** **** \n",
1670 " * * * * * * * * ** * * * * * * * \n",
1671 " * ***** * * * ***** * * * * ***** * * * ***** * \n",
1672 " * * * * * * * * * * * * * * * * * * * \n",
1673 " * * * ** * * ** * ** * * * * ** * * ** * ** * \n",
1674 " ** ** * * * ** ** * * * * ** * * * ** ** * * \n"
1675 ]
1676 }
1677 ],
1678 "source": [
1679 "g = new_grid(w=80)\n",
1680 "interpret(cmds, g, show_each_step=True, overprint=True, suppress_dots=True);"
1681 ]
1682 },
1683 {
1684 "cell_type": "code",
1685 "execution_count": null,
1686 "metadata": {
1687 "collapsed": true
1688 },
1689 "outputs": [],
1690 "source": []
1691 }
1692 ],
1693 "metadata": {
1694 "kernelspec": {
1695 "display_name": "Python 3",
1696 "language": "python",
1697 "name": "python3"
1698 },
1699 "language_info": {
1700 "codemirror_mode": {
1701 "name": "ipython",
1702 "version": 3
1703 },
1704 "file_extension": ".py",
1705 "mimetype": "text/x-python",
1706 "name": "python",
1707 "nbconvert_exporter": "python",
1708 "pygments_lexer": "ipython3",
1709 "version": "3.5.2+"
1710 }
1711 },
1712 "nbformat": 4,
1713 "nbformat_minor": 2
1714 }