Task 1
[summerofcode2018soln.git] / src / task1 / task1.ipynb
diff --git a/src/task1/task1.ipynb b/src/task1/task1.ipynb
new file mode 100644 (file)
index 0000000..61839d0
--- /dev/null
@@ -0,0 +1,223 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "instructions = [l.strip() for l in open('../../data/01-mowmaster.txt')]"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Part 1"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1136"
+      ]
+     },
+     "execution_count": 3,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "sum(1 for i in instructions if not i.startswith('#'))"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Or, as a one-liner:"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1136"
+      ]
+     },
+     "execution_count": 4,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "sum(1 for i in open('../../data/01-mowmaster.txt') if not i.startswith('#'))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "395"
+      ]
+     },
+     "execution_count": 5,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "sum(1 for i in instructions if i.startswith('#'))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1531"
+      ]
+     },
+     "execution_count": 6,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "len(instructions)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Part 2"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def move(mower, distance):\n",
+    "    if mower['d'] == 0:\n",
+    "        mower['y'] += distance\n",
+    "    elif mower['d'] == 90:\n",
+    "        mower['x'] += distance\n",
+    "    elif mower['d'] == 180:\n",
+    "        mower['y'] -= distance\n",
+    "    elif mower['d'] == 270:\n",
+    "        mower['x'] -= distance\n",
+    "    else:\n",
+    "        raise ValueError\n",
+    "    return mower"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def init_mowmaster():\n",
+    "    return {'x': 0, 'y': 0, 'd': 90}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 16,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def mowmaster_distance(mw):\n",
+    "    return abs(mw['x']) + abs(mw['y'])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def execute(mowmaster, instructions, debug=False):\n",
+    "    for instruction in instructions:\n",
+    "        if instruction == 'C':\n",
+    "            mowmaster['d'] = (mowmaster['d'] + 90) % 360\n",
+    "        elif instruction == 'A':\n",
+    "            mowmaster['d'] = (mowmaster['d'] - 90) % 360\n",
+    "        elif instruction.startswith('F'):\n",
+    "            mowmaster = move(mowmaster, int(instruction[1:]))\n",
+    "        if debug: \n",
+    "            print(instruction, mowmaster)\n",
+    "    return mowmaster"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 18,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "337"
+      ]
+     },
+     "execution_count": 18,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "mw = init_mowmaster()\n",
+    "execute(mw, instructions)\n",
+    "mowmaster_distance(mw)"
+   ]
+  },
+  {
+   "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.5"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}