Day 18
[advent-of-code-15.git] / advent18.ipynb
diff --git a/advent18.ipynb b/advent18.ipynb
new file mode 100644 (file)
index 0000000..730bedc
--- /dev/null
@@ -0,0 +1,626 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "['.#.#.#', '...##.', '#....#', '..#...', '#.#..#', '####..']"
+      ]
+     },
+     "execution_count": 1,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "pi18 = \"\"\".#.#.#\n",
+    "...##.\n",
+    "#....#\n",
+    "..#...\n",
+    "#.#..#\n",
+    "####..\n",
+    "\"\"\".splitlines()\n",
+    "pi18"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(6, 6)"
+      ]
+     },
+     "execution_count": 2,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "rows = len(pi18)\n",
+    "columns = len(pi18[0])\n",
+    "rows, columns"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def read_grid(lines):\n",
+    "    grid = {}\n",
+    "    for i, r in enumerate(lines):\n",
+    "        for j, c in enumerate(r):\n",
+    "            if c == '#':\n",
+    "                grid[i, j] = True\n",
+    "    return grid    "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{(0, 1): True,\n",
+       " (0, 3): True,\n",
+       " (0, 5): True,\n",
+       " (1, 3): True,\n",
+       " (1, 4): True,\n",
+       " (2, 0): True,\n",
+       " (2, 5): True,\n",
+       " (3, 2): True,\n",
+       " (4, 0): True,\n",
+       " (4, 2): True,\n",
+       " (4, 5): True,\n",
+       " (5, 0): True,\n",
+       " (5, 1): True,\n",
+       " (5, 2): True,\n",
+       " (5, 3): True}"
+      ]
+     },
+     "execution_count": 4,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "g = read_grid(pi18)\n",
+    "g"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def print_grid(grid, rows, columns):\n",
+    "    for r in range(rows):\n",
+    "        for c in range(columns):\n",
+    "            if (r, c) in grid:\n",
+    "                print('#', end='')\n",
+    "            else:\n",
+    "                print('.', end='')\n",
+    "        print('')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      ".#.#.#\n",
+      "...##.\n",
+      "#....#\n",
+      "..#...\n",
+      "#.#..#\n",
+      "####..\n"
+     ]
+    }
+   ],
+   "source": [
+    "print_grid(g, rows, columns)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "def do_generation(grid, rows, columns):\n",
+    "    new_grid = {}\n",
+    "    for r in range(rows):\n",
+    "        for c in range(columns):\n",
+    "            live_neighbours = sum(1\n",
+    "                   for dr in [-1, 0, 1]\n",
+    "                   for dc in [-1, 0, 1]\n",
+    "                   if not (dr == 0 and dc == 0)\n",
+    "                   if (r+dr, c+dc) in grid)\n",
+    "            if (r, c) in grid:\n",
+    "                if live_neighbours in [2, 3]:\n",
+    "                    new_grid[r, c] = True\n",
+    "            else:\n",
+    "                if live_neighbours == 3:\n",
+    "                    new_grid[r, c] = True\n",
+    "    return new_grid"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "..##..\n",
+      "..##.#\n",
+      "...##.\n",
+      "......\n",
+      "#.....\n",
+      "#.##..\n"
+     ]
+    }
+   ],
+   "source": [
+    "print_grid(do_generation(g, 6, 6), 6, 6)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      ".#.#.#\n",
+      "...##.\n",
+      "#....#\n",
+      "..#...\n",
+      "#.#..#\n",
+      "####..\n"
+     ]
+    }
+   ],
+   "source": [
+    "print_grid(g, 6, 6)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def many_generations(grid, rows, columns, generations):\n",
+    "    for _ in range(generations):\n",
+    "        grid = do_generation(grid, rows, columns)\n",
+    "    return grid"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "......\n",
+      "......\n",
+      "..##..\n",
+      "..##..\n",
+      "......\n",
+      "......\n"
+     ]
+    }
+   ],
+   "source": [
+    "print_grid(many_generations(g, 6, 6, 4), 6, 6)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(100, 100)"
+      ]
+     },
+     "execution_count": 12,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "pi18 = [l.strip() for l in open('advent18.txt').readlines()]\n",
+    "rows = len(pi18)\n",
+    "columns = len(pi18[0])\n",
+    "rows, columns"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "5076"
+      ]
+     },
+     "execution_count": 13,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "g = read_grid(pi18)\n",
+    "len(g)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 14,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1061"
+      ]
+     },
+     "execution_count": 14,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "g = many_generations(g, rows, columns, 100)\n",
+    "len(g)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 15,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def set_corners_on(grid, rows, columns):\n",
+    "    grid[0, 0] = True\n",
+    "    grid[0, columns-1] = True\n",
+    "    grid[rows-1, 0] = True\n",
+    "    grid[rows-1, columns-1] = True\n",
+    "    return grid"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 16,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def do_generation_2(grid, rows, columns):\n",
+    "    grid = do_generation(grid, rows, columns)\n",
+    "    grid = set_corners_on(grid, rows, columns)\n",
+    "    return grid"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 17,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def many_generations_2(grid, rows, columns, generations):\n",
+    "    for _ in range(generations):\n",
+    "        grid = do_generation_2(grid, rows, columns)\n",
+    "    return grid"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 18,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "##.#.#\n",
+      "...##.\n",
+      "#....#\n",
+      "..#...\n",
+      "#.#..#\n",
+      "####.#\n"
+     ]
+    }
+   ],
+   "source": [
+    "pi18 = \"\"\".#.#.#\n",
+    "...##.\n",
+    "#....#\n",
+    "..#...\n",
+    "#.#..#\n",
+    "####..\n",
+    "\"\"\".splitlines()\n",
+    "rows = len(pi18)\n",
+    "columns = len(pi18[0])\n",
+    "g = read_grid(pi18)\n",
+    "g = set_corners_on(g, rows, columns)\n",
+    "print_grid(g, rows, columns)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 19,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "#.##.#\n",
+      "####.#\n",
+      "...##.\n",
+      "......\n",
+      "#...#.\n",
+      "#.####\n"
+     ]
+    }
+   ],
+   "source": [
+    "g = do_generation_2(g, 6, 6)\n",
+    "print_grid(g, 6, 6)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 20,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "#..#.#\n",
+      "#....#\n",
+      ".#.##.\n",
+      "...##.\n",
+      ".#..##\n",
+      "##.###\n"
+     ]
+    }
+   ],
+   "source": [
+    "g = do_generation_2(g, 6, 6)\n",
+    "print_grid(g, 6, 6)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 21,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "##.#.#\n",
+      "...##.\n",
+      "#....#\n",
+      "..#...\n",
+      "#.#..#\n",
+      "####.#\n"
+     ]
+    }
+   ],
+   "source": [
+    "pi18 = \"\"\".#.#.#\n",
+    "...##.\n",
+    "#....#\n",
+    "..#...\n",
+    "#.#..#\n",
+    "####..\n",
+    "\"\"\".splitlines()\n",
+    "rows = len(pi18)\n",
+    "columns = len(pi18[0])\n",
+    "g = read_grid(pi18)\n",
+    "g = set_corners_on(g, rows, columns)\n",
+    "print_grid(g, rows, columns)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 22,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "##.###\n",
+      ".##..#\n",
+      ".##...\n",
+      ".##...\n",
+      "#.#...\n",
+      "##...#\n"
+     ]
+    }
+   ],
+   "source": [
+    "print_grid(many_generations_2(g, 6, 6, 5), 6, 6)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 23,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(100, 100)"
+      ]
+     },
+     "execution_count": 23,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "pi18 = [l.strip() for l in open('advent18.txt').readlines()]\n",
+    "rows = len(pi18)\n",
+    "columns = len(pi18[0])\n",
+    "rows, columns"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 24,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "5078"
+      ]
+     },
+     "execution_count": 24,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "g = read_grid(pi18)\n",
+    "g = set_corners_on(g, rows, columns)\n",
+    "len(g)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 25,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1006"
+      ]
+     },
+     "execution_count": 25,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "g = many_generations_2(g, rows, columns, 100)\n",
+    "len(g)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.4.3"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}