c770246dd3c871389ba072a99177b8602cbbcb68
[ou-summer-of-code-2017.git] / 03-display-board / display-board-creation.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 52,
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": 56,
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": 59,
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": 43,
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": 45,
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": 47,
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": 17,
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": 17,
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": 18,
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": 19,
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": 19,
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": 20,
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": 21,
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": 21,
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": 22,
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": 23,
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": 23,
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": 24,
846 "metadata": {
847 "scrolled": true
848 },
849 "outputs": [
850 {
851 "data": {
852 "text/plain": [
853 "['rotate column 19 1',\n",
854 " 'rotate column 46 1',\n",
855 " 'rotate column 10 1',\n",
856 " 'rotate column 25 2',\n",
857 " 'rotate column 39 1',\n",
858 " 'rotate column 3 1',\n",
859 " 'rotate column 4 1',\n",
860 " 'rotate column 41 1',\n",
861 " 'rotate column 47 1',\n",
862 " 'rotate column 14 2',\n",
863 " 'rotate column 21 2',\n",
864 " 'rotate column 48 1',\n",
865 " 'rotate column 34 1',\n",
866 " 'rotate column 49 2',\n",
867 " 'rotate column 28 1',\n",
868 " 'rotate column 32 1',\n",
869 " 'rotate column 18 1',\n",
870 " 'rotate column 45 1',\n",
871 " 'rotate column 12 1',\n",
872 " 'rotate column 38 2',\n",
873 " 'rotate column 24 1',\n",
874 " 'rotate column 40 1',\n",
875 " 'rotate column 17 2',\n",
876 " 'rotate column 7 1',\n",
877 " 'rotate column 31 1',\n",
878 " 'rotate column 5 1',\n",
879 " 'rotate column 26 3',\n",
880 " 'rotate column 6 1',\n",
881 " 'rotate column 35 1',\n",
882 " 'rotate column 20 1',\n",
883 " 'rotate column 42 2',\n",
884 " 'rotate column 11 1',\n",
885 " 'rotate column 33 1',\n",
886 " 'rotate column 13 1',\n",
887 " 'rotate column 27 5']"
888 ]
889 },
890 "execution_count": 24,
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": 25,
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": 26,
914 "metadata": {},
915 "outputs": [
916 {
917 "data": {
918 "text/plain": [
919 "[2, 0, 4, 4, 4, 4, 0, 50]"
920 ]
921 },
922 "execution_count": 26,
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": 27,
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": 28,
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": 29,
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": 30,
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": 30,
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": 31,
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": 31,
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": 32,
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": 33,
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 "127"
1120 ]
1121 },
1122 "execution_count": 33,
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": 34,
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": 34,
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": 35,
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": 35,
1201 "metadata": {},
1202 "output_type": "execute_result"
1203 }
1204 ],
1205 "source": [
1206 "interpret(cmds)"
1207 ]
1208 },
1209 {
1210 "cell_type": "code",
1211 "execution_count": 36,
1212 "metadata": {},
1213 "outputs": [
1214 {
1215 "data": {
1216 "text/plain": [
1217 "['top 25 25',\n",
1218 " 'top 2 2',\n",
1219 " 'rotate column 2 2',\n",
1220 " 'rotate column 25 2',\n",
1221 " 'top 18 18',\n",
1222 " 'top 13 13',\n",
1223 " 'top 2 2',\n",
1224 " 'top 4 8',\n",
1225 " 'top 22 22',\n",
1226 " 'top 25 25',\n",
1227 " 'rotate column 7 2',\n",
1228 " 'rotate column 5 2',\n",
1229 " 'rotate column 4 2',\n",
1230 " 'rotate column 6 1',\n",
1231 " 'rotate column 8 2',\n",
1232 " 'rotate column 13 2',\n",
1233 " 'rotate column 22 1',\n",
1234 " 'rotate column 2 2',\n",
1235 " 'rotate column 18 2',\n",
1236 " 'rotate column 25 1',\n",
1237 " 'top 33 35',\n",
1238 " 'top 2 10',\n",
1239 " 'top 25 26',\n",
1240 " 'top 18 19',\n",
1241 " 'top 29 30',\n",
1242 " 'top 21 22',\n",
1243 " 'top 12 16',\n",
1244 " 'top 37 37',\n",
1245 " 'rotate column 19 1',\n",
1246 " 'rotate column 9 3',\n",
1247 " 'rotate column 25 1',\n",
1248 " 'rotate column 3 5',\n",
1249 " 'rotate column 33 3',\n",
1250 " 'rotate column 5 1',\n",
1251 " 'rotate column 2 1',\n",
1252 " 'rotate column 12 3',\n",
1253 " 'rotate column 21 3',\n",
1254 " 'rotate column 14 3',\n",
1255 " 'rotate column 22 1',\n",
1256 " 'rotate column 6 2',\n",
1257 " 'rotate column 26 3',\n",
1258 " 'rotate column 10 1',\n",
1259 " 'rotate column 35 3',\n",
1260 " 'rotate column 16 3',\n",
1261 " 'rotate column 18 2',\n",
1262 " 'rotate column 30 3',\n",
1263 " 'rotate column 13 2',\n",
1264 " 'rotate column 15 1',\n",
1265 " 'rotate column 37 3',\n",
1266 " 'rotate column 8 2',\n",
1267 " 'rotate column 4 2',\n",
1268 " 'rotate column 29 4',\n",
1269 " 'rotate column 7 3',\n",
1270 " 'rotate column 34 3',\n",
1271 " 'left 2 6',\n",
1272 " 'rotate row 5 4',\n",
1273 " 'rotate row 6 15',\n",
1274 " 'rotate row 2 3',\n",
1275 " 'rotate row 4 1',\n",
1276 " 'rotate row 3 4',\n",
1277 " 'left 2 6',\n",
1278 " 'rotate row 4 1',\n",
1279 " 'rotate row 2 4',\n",
1280 " 'rotate row 3 4',\n",
1281 " 'rotate row 6 7',\n",
1282 " 'rotate row 5 3',\n",
1283 " 'left 2 6',\n",
1284 " 'rotate row 2 7',\n",
1285 " 'rotate row 4 1',\n",
1286 " 'rotate row 6 7',\n",
1287 " 'rotate row 3 3',\n",
1288 " 'rotate row 5 4',\n",
1289 " 'top 42 45',\n",
1290 " 'top 21 21',\n",
1291 " 'top 24 25',\n",
1292 " 'top 28 33',\n",
1293 " 'top 36 40',\n",
1294 " 'top 2 5',\n",
1295 " 'top 15 19',\n",
1296 " 'top 7 12',\n",
1297 " 'rotate column 38 1',\n",
1298 " 'rotate column 33 2',\n",
1299 " 'rotate column 25 1',\n",
1300 " 'rotate column 3 1',\n",
1301 " 'rotate column 40 2',\n",
1302 " 'rotate column 31 1',\n",
1303 " 'rotate column 16 1',\n",
1304 " 'rotate column 42 1',\n",
1305 " 'rotate column 32 1',\n",
1306 " 'rotate column 19 2',\n",
1307 " 'rotate column 37 1',\n",
1308 " 'rotate column 10 1',\n",
1309 " 'rotate column 21 1',\n",
1310 " 'rotate column 18 5',\n",
1311 " 'rotate column 30 1',\n",
1312 " 'rotate column 44 1',\n",
1313 " 'rotate column 8 1',\n",
1314 " 'rotate column 4 1',\n",
1315 " 'rotate column 5 2',\n",
1316 " 'rotate column 36 1',\n",
1317 " 'rotate column 24 2',\n",
1318 " 'rotate column 29 1',\n",
1319 " 'rotate column 2 1',\n",
1320 " 'rotate column 15 1',\n",
1321 " 'rotate column 12 2',\n",
1322 " 'rotate column 9 1',\n",
1323 " 'rotate column 43 1',\n",
1324 " 'rotate column 45 1',\n",
1325 " 'rotate column 17 1',\n",
1326 " 'rotate column 11 4',\n",
1327 " 'rotate column 28 1',\n",
1328 " 'rotate column 7 1',\n",
1329 " 'rotate column 39 3',\n",
1330 " 'left 2 7',\n",
1331 " 'rotate row 2 1',\n",
1332 " 'rotate row 6 5',\n",
1333 " 'rotate row 5 5',\n",
1334 " 'rotate row 3 5',\n",
1335 " 'rotate row 4 5',\n",
1336 " 'rotate row 7 5',\n",
1337 " 'left 2 7',\n",
1338 " 'rotate row 4 4',\n",
1339 " 'rotate row 2 2',\n",
1340 " 'rotate row 3 4',\n",
1341 " 'rotate row 5 4',\n",
1342 " 'rotate row 7 4',\n",
1343 " 'rotate row 6 4']"
1344 ]
1345 },
1346 "execution_count": 36,
1347 "metadata": {},
1348 "output_type": "execute_result"
1349 }
1350 ],
1351 "source": [
1352 "cmds"
1353 ]
1354 },
1355 {
1356 "cell_type": "code",
1357 "execution_count": 37,
1358 "metadata": {},
1359 "outputs": [
1360 {
1361 "name": "stdout",
1362 "output_type": "stream",
1363 "text": [
1364 "Final\n",
1365 "..................................................\n",
1366 "..*****..****....***...*...*..*****...***...****..\n",
1367 "....*....*...*..*...*..**..*....*....*...*..*...*.\n",
1368 "....*....*..*...*...*..*.*.*....*....*...*..*..*..\n",
1369 "....*....****...*****..*.*.*....*....*...*..****..\n",
1370 "....*....*...*..*...*..*..**....*....*...*..*...*.\n",
1371 "....*....*...*..*...*..*...*....*.....***...*...*.\n",
1372 "..................................................\n"
1373 ]
1374 },
1375 {
1376 "data": {
1377 "text/plain": [
1378 "['..................................................',\n",
1379 " '..*****..****....***...*...*..*****...***...****..',\n",
1380 " '....*....*...*..*...*..**..*....*....*...*..*...*.',\n",
1381 " '....*....*..*...*...*..*.*.*....*....*...*..*..*..',\n",
1382 " '....*....****...*****..*.*.*....*....*...*..****..',\n",
1383 " '....*....*...*..*...*..*..**....*....*...*..*...*.',\n",
1384 " '....*....*...*..*...*..*...*....*.....***...*...*.',\n",
1385 " '..................................................']"
1386 ]
1387 },
1388 "execution_count": 37,
1389 "metadata": {},
1390 "output_type": "execute_result"
1391 }
1392 ],
1393 "source": [
1394 "g = new_grid()\n",
1395 "interpret(cmds, g, show_each_step=True, overprint=True)"
1396 ]
1397 },
1398 {
1399 "cell_type": "code",
1400 "execution_count": 38,
1401 "metadata": {},
1402 "outputs": [
1403 {
1404 "name": "stdout",
1405 "output_type": "stream",
1406 "text": [
1407 "Final\n",
1408 "..................................................\n",
1409 "..................................................\n",
1410 "..................................................\n",
1411 "..................................................\n",
1412 "..................................................\n",
1413 "..................................................\n",
1414 "..................................................\n",
1415 "..................................................\n"
1416 ]
1417 },
1418 {
1419 "data": {
1420 "text/plain": [
1421 "['..................................................',\n",
1422 " '..................................................',\n",
1423 " '..................................................',\n",
1424 " '..................................................',\n",
1425 " '..................................................',\n",
1426 " '..................................................',\n",
1427 " '..................................................',\n",
1428 " '..................................................']"
1429 ]
1430 },
1431 "execution_count": 38,
1432 "metadata": {},
1433 "output_type": "execute_result"
1434 }
1435 ],
1436 "source": [
1437 "g = tg()\n",
1438 "interpret(cmds, g, show_each_step=True, overprint=True, uninterpret=True)"
1439 ]
1440 },
1441 {
1442 "cell_type": "code",
1443 "execution_count": 39,
1444 "metadata": {},
1445 "outputs": [
1446 {
1447 "data": {
1448 "text/plain": [
1449 "98"
1450 ]
1451 },
1452 "execution_count": 39,
1453 "metadata": {},
1454 "output_type": "execute_result"
1455 }
1456 ],
1457 "source": [
1458 "sum(1 for c in ''.join(tg()) if c == '*')"
1459 ]
1460 },
1461 {
1462 "cell_type": "code",
1463 "execution_count": 41,
1464 "metadata": {},
1465 "outputs": [
1466 {
1467 "data": {
1468 "text/plain": [
1469 "2007"
1470 ]
1471 },
1472 "execution_count": 41,
1473 "metadata": {},
1474 "output_type": "execute_result"
1475 }
1476 ],
1477 "source": [
1478 "open('03-pixels.txt', 'w').write('\\n'.join(cmds))"
1479 ]
1480 },
1481 {
1482 "cell_type": "code",
1483 "execution_count": 48,
1484 "metadata": {},
1485 "outputs": [
1486 {
1487 "name": "stdout",
1488 "output_type": "stream",
1489 "text": [
1490 "................................................................................\n",
1491 "................................................................................\n",
1492 "................................................................................\n",
1493 "................................................................................\n",
1494 "................................................................................\n",
1495 "................................................................................\n",
1496 "................................................................................\n",
1497 "................................................................................\n"
1498 ]
1499 },
1500 {
1501 "data": {
1502 "text/plain": [
1503 "255"
1504 ]
1505 },
1506 "execution_count": 48,
1507 "metadata": {},
1508 "output_type": "execute_result"
1509 }
1510 ],
1511 "source": [
1512 "g = jantar_mantar() \n",
1513 "cmds = []\n",
1514 "\n",
1515 "while '*' in ''.join(g):\n",
1516 " if random.choice([True, False]):\n",
1517 " c = slide_rows(g, randomise=True)\n",
1518 " interpret(c, g, uninterpret=True)\n",
1519 " cmds = c + cmds\n",
1520 " \n",
1521 " c = unleft(g, randomise=True)\n",
1522 " interpret(c, g, uninterpret=True)\n",
1523 " cmds = c + cmds\n",
1524 " \n",
1525 " else:\n",
1526 " c = lift_cols(g, randomise=True)\n",
1527 " interpret(c, g, uninterpret=True)\n",
1528 " cmds = c + cmds\n",
1529 " \n",
1530 " c = untop(g, randomise=True)\n",
1531 " interpret(c, g, uninterpret=True)\n",
1532 " cmds = c + cmds\n",
1533 " \n",
1534 "print_grid(g)\n",
1535 "len(cmds)"
1536 ]
1537 },
1538 {
1539 "cell_type": "code",
1540 "execution_count": 61,
1541 "metadata": {},
1542 "outputs": [
1543 {
1544 "name": "stdout",
1545 "output_type": "stream",
1546 "text": [
1547 "Final\n",
1548 "...****..............*...................*.....*..............*.................\n",
1549 "......*..............*...................***..**..............*.................\n",
1550 "......*.*****.*****.****.*****..****.....*.*.***.*****.*****.****.*****..****...\n",
1551 "......*.....*.*...*..*.......*..*........*..**.*.....*.*...*..*.......*..*......\n",
1552 "......*.*****.*...*..*...*****..*........*..*..*.*****.*...*..*...*****..*......\n",
1553 "......*.*...*.*...*..*...*...*..*........*.....*.*...*.*...*..*...*...*..*......\n",
1554 "...*..*.*..**.*...*..**..*..**..*........*.....*.*..**.*...*..**..*..**..*......\n",
1555 "....**...**.*.*...*...**..**.*..*........*.....*..**.*.*...*...**..**.*..*......\n"
1556 ]
1557 },
1558 {
1559 "data": {
1560 "text/plain": [
1561 "['...****..............*...................*.....*..............*.................',\n",
1562 " '......*..............*...................***..**..............*.................',\n",
1563 " '......*.*****.*****.****.*****..****.....*.*.***.*****.*****.****.*****..****...',\n",
1564 " '......*.....*.*...*..*.......*..*........*..**.*.....*.*...*..*.......*..*......',\n",
1565 " '......*.*****.*...*..*...*****..*........*..*..*.*****.*...*..*...*****..*......',\n",
1566 " '......*.*...*.*...*..*...*...*..*........*.....*.*...*.*...*..*...*...*..*......',\n",
1567 " '...*..*.*..**.*...*..**..*..**..*........*.....*.*..**.*...*..**..*..**..*......',\n",
1568 " '....**...**.*.*...*...**..**.*..*........*.....*..**.*.*...*...**..**.*..*......']"
1569 ]
1570 },
1571 "execution_count": 61,
1572 "metadata": {},
1573 "output_type": "execute_result"
1574 }
1575 ],
1576 "source": [
1577 "g = new_grid(w=80)\n",
1578 "interpret(cmds, g, show_each_step=True, overprint=True)"
1579 ]
1580 },
1581 {
1582 "cell_type": "code",
1583 "execution_count": 50,
1584 "metadata": {},
1585 "outputs": [
1586 {
1587 "data": {
1588 "text/plain": [
1589 "80"
1590 ]
1591 },
1592 "execution_count": 50,
1593 "metadata": {},
1594 "output_type": "execute_result"
1595 }
1596 ],
1597 "source": [
1598 "len(jantar_mantar()[0])"
1599 ]
1600 },
1601 {
1602 "cell_type": "code",
1603 "execution_count": 51,
1604 "metadata": {},
1605 "outputs": [
1606 {
1607 "data": {
1608 "text/plain": [
1609 "4123"
1610 ]
1611 },
1612 "execution_count": 51,
1613 "metadata": {},
1614 "output_type": "execute_result"
1615 }
1616 ],
1617 "source": [
1618 "open('03-pixels.txt', 'w').write('\\n'.join(cmds))"
1619 ]
1620 },
1621 {
1622 "cell_type": "code",
1623 "execution_count": 57,
1624 "metadata": {},
1625 "outputs": [
1626 {
1627 "name": "stdout",
1628 "output_type": "stream",
1629 "text": [
1630 " **** * * * * \n",
1631 " * * *** ** * \n",
1632 " * ***** ***** **** ***** **** * * *** ***** ***** **** ***** **** \n",
1633 " * * * * * * * * ** * * * * * * * \n",
1634 " * ***** * * * ***** * * * * ***** * * * ***** * \n",
1635 " * * * * * * * * * * * * * * * * * * * \n",
1636 " * * * ** * * ** * ** * * * * ** * * ** * ** * \n",
1637 " ** ** * * * ** ** * * * * ** * * * ** ** * * \n"
1638 ]
1639 }
1640 ],
1641 "source": [
1642 "print_grid(jantar_mantar(), suppress_dots=True)"
1643 ]
1644 },
1645 {
1646 "cell_type": "code",
1647 "execution_count": null,
1648 "metadata": {
1649 "collapsed": true
1650 },
1651 "outputs": [],
1652 "source": []
1653 }
1654 ],
1655 "metadata": {
1656 "kernelspec": {
1657 "display_name": "Python 3",
1658 "language": "python",
1659 "name": "python3"
1660 },
1661 "language_info": {
1662 "codemirror_mode": {
1663 "name": "ipython",
1664 "version": 3
1665 },
1666 "file_extension": ".py",
1667 "mimetype": "text/x-python",
1668 "name": "python",
1669 "nbconvert_exporter": "python",
1670 "pygments_lexer": "ipython3",
1671 "version": "3.5.2+"
1672 }
1673 },
1674 "nbformat": 4,
1675 "nbformat_minor": 2
1676 }