Tasks 7 and 8
[summerofcode2018soln.git] / src / task7 / task7.ipynb
diff --git a/src/task7/task7.ipynb b/src/task7/task7.ipynb
new file mode 100644 (file)
index 0000000..b760261
--- /dev/null
@@ -0,0 +1,125 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def enflip(items, flips, burnt=False, debug=False):\n",
+    "    if debug: i0 = items\n",
+    "    for flip in flips:\n",
+    "        if burnt:\n",
+    "            items = [-i for i in reversed(items[:flip])] + items[flip:]\n",
+    "        else:\n",
+    "            items = [i for i in reversed(items[:flip])] + items[flip:]\n",
+    "        if debug: print('{} -{}-> {}'.format(i0, flip, items))\n",
+    "        if debug: i0 = items\n",
+    "    return items"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def burnt_sorted(pancakes):\n",
+    "    return pancakes == sorted(pancakes)\n",
+    "\n",
+    "def unburnt_sorted(pancakes):\n",
+    "    simple_pancakes = [abs(p) for p in pancakes]\n",
+    "    return simple_pancakes == sorted(simple_pancakes)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "flips_t = [l.strip().split(': ') for l in open('../../data/07-flips.txt')]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "stack = [[int(p) for p in l.split()] for c, l in flips_t if c == 'burgers'][0]\n",
+    "flips = {int(c): [int(p) for p in l.split()] for c, l in flips_t if c != 'burgers'}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 14,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "69"
+      ]
+     },
+     "execution_count": 14,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "sum(1 for f in flips.values()\n",
+    "   if unburnt_sorted(enflip(stack, f)))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 15,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "92"
+      ]
+     },
+     "execution_count": 15,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "[f for f in flips\n",
+    "   if burnt_sorted(enflip(stack, flips[f], burnt=True))][0]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "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.6.6"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}