Fixed typo
[advent-of-code-15.git] / advent18.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [
10 {
11 "data": {
12 "text/plain": [
13 "['.#.#.#', '...##.', '#....#', '..#...', '#.#..#', '####..']"
14 ]
15 },
16 "execution_count": 1,
17 "metadata": {},
18 "output_type": "execute_result"
19 }
20 ],
21 "source": [
22 "pi18 = \"\"\".#.#.#\n",
23 "...##.\n",
24 "#....#\n",
25 "..#...\n",
26 "#.#..#\n",
27 "####..\n",
28 "\"\"\".splitlines()\n",
29 "pi18"
30 ]
31 },
32 {
33 "cell_type": "code",
34 "execution_count": 2,
35 "metadata": {
36 "collapsed": false
37 },
38 "outputs": [
39 {
40 "data": {
41 "text/plain": [
42 "(6, 6)"
43 ]
44 },
45 "execution_count": 2,
46 "metadata": {},
47 "output_type": "execute_result"
48 }
49 ],
50 "source": [
51 "rows = len(pi18)\n",
52 "columns = len(pi18[0])\n",
53 "rows, columns"
54 ]
55 },
56 {
57 "cell_type": "code",
58 "execution_count": 3,
59 "metadata": {
60 "collapsed": true
61 },
62 "outputs": [],
63 "source": [
64 "def read_grid(lines):\n",
65 " grid = {}\n",
66 " for i, r in enumerate(lines):\n",
67 " for j, c in enumerate(r):\n",
68 " if c == '#':\n",
69 " grid[i, j] = True\n",
70 " return grid "
71 ]
72 },
73 {
74 "cell_type": "code",
75 "execution_count": 4,
76 "metadata": {
77 "collapsed": false
78 },
79 "outputs": [
80 {
81 "data": {
82 "text/plain": [
83 "{(0, 1): True,\n",
84 " (0, 3): True,\n",
85 " (0, 5): True,\n",
86 " (1, 3): True,\n",
87 " (1, 4): True,\n",
88 " (2, 0): True,\n",
89 " (2, 5): True,\n",
90 " (3, 2): True,\n",
91 " (4, 0): True,\n",
92 " (4, 2): True,\n",
93 " (4, 5): True,\n",
94 " (5, 0): True,\n",
95 " (5, 1): True,\n",
96 " (5, 2): True,\n",
97 " (5, 3): True}"
98 ]
99 },
100 "execution_count": 4,
101 "metadata": {},
102 "output_type": "execute_result"
103 }
104 ],
105 "source": [
106 "g = read_grid(pi18)\n",
107 "g"
108 ]
109 },
110 {
111 "cell_type": "code",
112 "execution_count": 5,
113 "metadata": {
114 "collapsed": true
115 },
116 "outputs": [],
117 "source": [
118 "def print_grid(grid, rows, columns):\n",
119 " for r in range(rows):\n",
120 " for c in range(columns):\n",
121 " if (r, c) in grid:\n",
122 " print('#', end='')\n",
123 " else:\n",
124 " print('.', end='')\n",
125 " print('')"
126 ]
127 },
128 {
129 "cell_type": "code",
130 "execution_count": 6,
131 "metadata": {
132 "collapsed": false
133 },
134 "outputs": [
135 {
136 "name": "stdout",
137 "output_type": "stream",
138 "text": [
139 ".#.#.#\n",
140 "...##.\n",
141 "#....#\n",
142 "..#...\n",
143 "#.#..#\n",
144 "####..\n"
145 ]
146 }
147 ],
148 "source": [
149 "print_grid(g, rows, columns)"
150 ]
151 },
152 {
153 "cell_type": "code",
154 "execution_count": 7,
155 "metadata": {
156 "collapsed": false
157 },
158 "outputs": [],
159 "source": [
160 "def do_generation(grid, rows, columns):\n",
161 " new_grid = {}\n",
162 " for r in range(rows):\n",
163 " for c in range(columns):\n",
164 " live_neighbours = sum(1\n",
165 " for dr in [-1, 0, 1]\n",
166 " for dc in [-1, 0, 1]\n",
167 " if not (dr == 0 and dc == 0)\n",
168 " if (r+dr, c+dc) in grid)\n",
169 " if (r, c) in grid:\n",
170 " if live_neighbours in [2, 3]:\n",
171 " new_grid[r, c] = True\n",
172 " else:\n",
173 " if live_neighbours == 3:\n",
174 " new_grid[r, c] = True\n",
175 " return new_grid"
176 ]
177 },
178 {
179 "cell_type": "code",
180 "execution_count": 8,
181 "metadata": {
182 "collapsed": false
183 },
184 "outputs": [
185 {
186 "name": "stdout",
187 "output_type": "stream",
188 "text": [
189 "..##..\n",
190 "..##.#\n",
191 "...##.\n",
192 "......\n",
193 "#.....\n",
194 "#.##..\n"
195 ]
196 }
197 ],
198 "source": [
199 "print_grid(do_generation(g, 6, 6), 6, 6)"
200 ]
201 },
202 {
203 "cell_type": "code",
204 "execution_count": 9,
205 "metadata": {
206 "collapsed": false
207 },
208 "outputs": [
209 {
210 "name": "stdout",
211 "output_type": "stream",
212 "text": [
213 ".#.#.#\n",
214 "...##.\n",
215 "#....#\n",
216 "..#...\n",
217 "#.#..#\n",
218 "####..\n"
219 ]
220 }
221 ],
222 "source": [
223 "print_grid(g, 6, 6)"
224 ]
225 },
226 {
227 "cell_type": "code",
228 "execution_count": 10,
229 "metadata": {
230 "collapsed": true
231 },
232 "outputs": [],
233 "source": [
234 "def many_generations(grid, rows, columns, generations):\n",
235 " for _ in range(generations):\n",
236 " grid = do_generation(grid, rows, columns)\n",
237 " return grid"
238 ]
239 },
240 {
241 "cell_type": "code",
242 "execution_count": 11,
243 "metadata": {
244 "collapsed": false
245 },
246 "outputs": [
247 {
248 "name": "stdout",
249 "output_type": "stream",
250 "text": [
251 "......\n",
252 "......\n",
253 "..##..\n",
254 "..##..\n",
255 "......\n",
256 "......\n"
257 ]
258 }
259 ],
260 "source": [
261 "print_grid(many_generations(g, 6, 6, 4), 6, 6)"
262 ]
263 },
264 {
265 "cell_type": "code",
266 "execution_count": 12,
267 "metadata": {
268 "collapsed": false
269 },
270 "outputs": [
271 {
272 "data": {
273 "text/plain": [
274 "(100, 100)"
275 ]
276 },
277 "execution_count": 12,
278 "metadata": {},
279 "output_type": "execute_result"
280 }
281 ],
282 "source": [
283 "pi18 = [l.strip() for l in open('advent18.txt').readlines()]\n",
284 "rows = len(pi18)\n",
285 "columns = len(pi18[0])\n",
286 "rows, columns"
287 ]
288 },
289 {
290 "cell_type": "code",
291 "execution_count": 13,
292 "metadata": {
293 "collapsed": false
294 },
295 "outputs": [
296 {
297 "data": {
298 "text/plain": [
299 "5076"
300 ]
301 },
302 "execution_count": 13,
303 "metadata": {},
304 "output_type": "execute_result"
305 }
306 ],
307 "source": [
308 "g = read_grid(pi18)\n",
309 "len(g)"
310 ]
311 },
312 {
313 "cell_type": "code",
314 "execution_count": 14,
315 "metadata": {
316 "collapsed": false
317 },
318 "outputs": [
319 {
320 "data": {
321 "text/plain": [
322 "1061"
323 ]
324 },
325 "execution_count": 14,
326 "metadata": {},
327 "output_type": "execute_result"
328 }
329 ],
330 "source": [
331 "g = many_generations(g, rows, columns, 100)\n",
332 "len(g)"
333 ]
334 },
335 {
336 "cell_type": "code",
337 "execution_count": 15,
338 "metadata": {
339 "collapsed": true
340 },
341 "outputs": [],
342 "source": [
343 "def set_corners_on(grid, rows, columns):\n",
344 " grid[0, 0] = True\n",
345 " grid[0, columns-1] = True\n",
346 " grid[rows-1, 0] = True\n",
347 " grid[rows-1, columns-1] = True\n",
348 " return grid"
349 ]
350 },
351 {
352 "cell_type": "code",
353 "execution_count": 16,
354 "metadata": {
355 "collapsed": true
356 },
357 "outputs": [],
358 "source": [
359 "def do_generation_2(grid, rows, columns):\n",
360 " grid = do_generation(grid, rows, columns)\n",
361 " grid = set_corners_on(grid, rows, columns)\n",
362 " return grid"
363 ]
364 },
365 {
366 "cell_type": "code",
367 "execution_count": 17,
368 "metadata": {
369 "collapsed": true
370 },
371 "outputs": [],
372 "source": [
373 "def many_generations_2(grid, rows, columns, generations):\n",
374 " for _ in range(generations):\n",
375 " grid = do_generation_2(grid, rows, columns)\n",
376 " return grid"
377 ]
378 },
379 {
380 "cell_type": "code",
381 "execution_count": 18,
382 "metadata": {
383 "collapsed": false
384 },
385 "outputs": [
386 {
387 "name": "stdout",
388 "output_type": "stream",
389 "text": [
390 "##.#.#\n",
391 "...##.\n",
392 "#....#\n",
393 "..#...\n",
394 "#.#..#\n",
395 "####.#\n"
396 ]
397 }
398 ],
399 "source": [
400 "pi18 = \"\"\".#.#.#\n",
401 "...##.\n",
402 "#....#\n",
403 "..#...\n",
404 "#.#..#\n",
405 "####..\n",
406 "\"\"\".splitlines()\n",
407 "rows = len(pi18)\n",
408 "columns = len(pi18[0])\n",
409 "g = read_grid(pi18)\n",
410 "g = set_corners_on(g, rows, columns)\n",
411 "print_grid(g, rows, columns)"
412 ]
413 },
414 {
415 "cell_type": "code",
416 "execution_count": 19,
417 "metadata": {
418 "collapsed": false
419 },
420 "outputs": [
421 {
422 "name": "stdout",
423 "output_type": "stream",
424 "text": [
425 "#.##.#\n",
426 "####.#\n",
427 "...##.\n",
428 "......\n",
429 "#...#.\n",
430 "#.####\n"
431 ]
432 }
433 ],
434 "source": [
435 "g = do_generation_2(g, 6, 6)\n",
436 "print_grid(g, 6, 6)"
437 ]
438 },
439 {
440 "cell_type": "code",
441 "execution_count": 20,
442 "metadata": {
443 "collapsed": false
444 },
445 "outputs": [
446 {
447 "name": "stdout",
448 "output_type": "stream",
449 "text": [
450 "#..#.#\n",
451 "#....#\n",
452 ".#.##.\n",
453 "...##.\n",
454 ".#..##\n",
455 "##.###\n"
456 ]
457 }
458 ],
459 "source": [
460 "g = do_generation_2(g, 6, 6)\n",
461 "print_grid(g, 6, 6)"
462 ]
463 },
464 {
465 "cell_type": "code",
466 "execution_count": 21,
467 "metadata": {
468 "collapsed": false
469 },
470 "outputs": [
471 {
472 "name": "stdout",
473 "output_type": "stream",
474 "text": [
475 "##.#.#\n",
476 "...##.\n",
477 "#....#\n",
478 "..#...\n",
479 "#.#..#\n",
480 "####.#\n"
481 ]
482 }
483 ],
484 "source": [
485 "pi18 = \"\"\".#.#.#\n",
486 "...##.\n",
487 "#....#\n",
488 "..#...\n",
489 "#.#..#\n",
490 "####..\n",
491 "\"\"\".splitlines()\n",
492 "rows = len(pi18)\n",
493 "columns = len(pi18[0])\n",
494 "g = read_grid(pi18)\n",
495 "g = set_corners_on(g, rows, columns)\n",
496 "print_grid(g, rows, columns)"
497 ]
498 },
499 {
500 "cell_type": "code",
501 "execution_count": 22,
502 "metadata": {
503 "collapsed": false
504 },
505 "outputs": [
506 {
507 "name": "stdout",
508 "output_type": "stream",
509 "text": [
510 "##.###\n",
511 ".##..#\n",
512 ".##...\n",
513 ".##...\n",
514 "#.#...\n",
515 "##...#\n"
516 ]
517 }
518 ],
519 "source": [
520 "print_grid(many_generations_2(g, 6, 6, 5), 6, 6)"
521 ]
522 },
523 {
524 "cell_type": "code",
525 "execution_count": 23,
526 "metadata": {
527 "collapsed": false
528 },
529 "outputs": [
530 {
531 "data": {
532 "text/plain": [
533 "(100, 100)"
534 ]
535 },
536 "execution_count": 23,
537 "metadata": {},
538 "output_type": "execute_result"
539 }
540 ],
541 "source": [
542 "pi18 = [l.strip() for l in open('advent18.txt').readlines()]\n",
543 "rows = len(pi18)\n",
544 "columns = len(pi18[0])\n",
545 "rows, columns"
546 ]
547 },
548 {
549 "cell_type": "code",
550 "execution_count": 24,
551 "metadata": {
552 "collapsed": false
553 },
554 "outputs": [
555 {
556 "data": {
557 "text/plain": [
558 "5078"
559 ]
560 },
561 "execution_count": 24,
562 "metadata": {},
563 "output_type": "execute_result"
564 }
565 ],
566 "source": [
567 "g = read_grid(pi18)\n",
568 "g = set_corners_on(g, rows, columns)\n",
569 "len(g)"
570 ]
571 },
572 {
573 "cell_type": "code",
574 "execution_count": 25,
575 "metadata": {
576 "collapsed": false
577 },
578 "outputs": [
579 {
580 "data": {
581 "text/plain": [
582 "1006"
583 ]
584 },
585 "execution_count": 25,
586 "metadata": {},
587 "output_type": "execute_result"
588 }
589 ],
590 "source": [
591 "g = many_generations_2(g, rows, columns, 100)\n",
592 "len(g)"
593 ]
594 },
595 {
596 "cell_type": "code",
597 "execution_count": null,
598 "metadata": {
599 "collapsed": true
600 },
601 "outputs": [],
602 "source": []
603 }
604 ],
605 "metadata": {
606 "kernelspec": {
607 "display_name": "Python 3",
608 "language": "python",
609 "name": "python3"
610 },
611 "language_info": {
612 "codemirror_mode": {
613 "name": "ipython",
614 "version": 3
615 },
616 "file_extension": ".py",
617 "mimetype": "text/x-python",
618 "name": "python",
619 "nbconvert_exporter": "python",
620 "pygments_lexer": "ipython3",
621 "version": "3.4.3"
622 }
623 },
624 "nbformat": 4,
625 "nbformat_minor": 0
626 }