Day 25
[advent-of-code-15.git] / advent25.ipynb
diff --git a/advent25.ipynb b/advent25.ipynb
new file mode 100644 (file)
index 0000000..c6c1136
--- /dev/null
@@ -0,0 +1,212 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "import itertools"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def coordinates():\n",
+    "    row = 1\n",
+    "    col = 1\n",
+    "    while True:\n",
+    "        yield row, col\n",
+    "        if row == 1:\n",
+    "            row = col + 1\n",
+    "            col = 1\n",
+    "        else:\n",
+    "            row -= 1\n",
+    "            col += 1"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[(1, 1),\n",
+       " (2, 1),\n",
+       " (1, 2),\n",
+       " (3, 1),\n",
+       " (2, 2),\n",
+       " (1, 3),\n",
+       " (4, 1),\n",
+       " (3, 2),\n",
+       " (2, 3),\n",
+       " (1, 4),\n",
+       " (5, 1),\n",
+       " (4, 2),\n",
+       " (3, 3),\n",
+       " (2, 4),\n",
+       " (1, 5),\n",
+       " (6, 1),\n",
+       " (5, 2),\n",
+       " (4, 3),\n",
+       " (3, 4),\n",
+       " (2, 5)]"
+      ]
+     },
+     "execution_count": 3,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "list(itertools.islice(coordinates(), 20))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def code():\n",
+    "    c = 20151125\n",
+    "    while True:\n",
+    "        yield c\n",
+    "        c = (c * 252533) % 33554393"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{(1, 1): 20151125,\n",
+       " (1, 2): 18749137,\n",
+       " (1, 3): 17289845,\n",
+       " (1, 4): 30943339,\n",
+       " (1, 5): 10071777,\n",
+       " (2, 1): 31916031,\n",
+       " (2, 2): 21629792,\n",
+       " (2, 3): 16929656,\n",
+       " (2, 4): 7726640,\n",
+       " (2, 5): 15514188,\n",
+       " (3, 1): 16080970,\n",
+       " (3, 2): 8057251,\n",
+       " (3, 3): 1601130,\n",
+       " (3, 4): 7981243,\n",
+       " (4, 1): 24592653,\n",
+       " (4, 2): 32451966,\n",
+       " (4, 3): 21345942,\n",
+       " (5, 1): 77061,\n",
+       " (5, 2): 17552253,\n",
+       " (6, 1): 33071741}"
+      ]
+     },
+     "execution_count": 7,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "{k: v for k, v in zip(itertools.islice(coordinates(), 20), code())}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[(4, 4)]"
+      ]
+     },
+     "execution_count": 11,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "list(itertools.islice(itertools.dropwhile(lambda c: not (c[0] == 4 and c[1] == 4), coordinates()), 1))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 17,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{(2981, 3075): 9132360}"
+      ]
+     },
+     "execution_count": 17,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# To continue, please consult the code grid in the manual.  Enter the code at row 2981, column 3075.\n",
+    "target_row = 2981\n",
+    "target_col = 3075\n",
+    "{k: v for k, v in itertools.islice(itertools.dropwhile(lambda c: not (c[0][0] == target_row and c[0][1] == target_col), \n",
+    "                                                       zip(coordinates(), code())),\n",
+    "                                   1)}"
+   ]
+  },
+  {
+   "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
+}