9 "instructions = [l.strip() for l in open('../../data/01-mowmaster.txt')]"
13 "cell_type": "markdown",
32 "output_type": "execute_result"
36 "sum(1 for i in instructions if not i.startswith('#'))"
40 "cell_type": "markdown",
59 "output_type": "execute_result"
63 "sum(1 for i in open('../../data/01-mowmaster.txt') if not i.startswith('#'))"
79 "output_type": "execute_result"
83 "sum(1 for i in instructions if i.startswith('#'))"
99 "output_type": "execute_result"
107 "cell_type": "markdown",
115 "execution_count": 7,
119 "def move(mower, distance):\n",
120 " if mower['d'] == 0:\n",
121 " mower['y'] += distance\n",
122 " elif mower['d'] == 90:\n",
123 " mower['x'] += distance\n",
124 " elif mower['d'] == 180:\n",
125 " mower['y'] -= distance\n",
126 " elif mower['d'] == 270:\n",
127 " mower['x'] -= distance\n",
129 " raise ValueError\n",
135 "execution_count": 12,
139 "def init_mowmaster():\n",
140 " return {'x': 0, 'y': 0, 'd': 90}"
145 "execution_count": 16,
149 "def mowmaster_distance(mw):\n",
150 " return abs(mw['x']) + abs(mw['y'])"
155 "execution_count": 13,
159 "def execute(mowmaster, instructions, debug=False):\n",
160 " for instruction in instructions:\n",
161 " if instruction == 'C':\n",
162 " mowmaster['d'] = (mowmaster['d'] + 90) % 360\n",
163 " elif instruction == 'A':\n",
164 " mowmaster['d'] = (mowmaster['d'] - 90) % 360\n",
165 " elif instruction.startswith('F'):\n",
166 " mowmaster = move(mowmaster, int(instruction[1:]))\n",
168 " print(instruction, mowmaster)\n",
174 "execution_count": 18,
183 "execution_count": 18,
185 "output_type": "execute_result"
189 "mw = init_mowmaster()\n",
190 "execute(mw, instructions)\n",
191 "mowmaster_distance(mw)"
196 "execution_count": null,
204 "display_name": "Python 3",
205 "language": "python",
213 "file_extension": ".py",
214 "mimetype": "text/x-python",
216 "nbconvert_exporter": "python",
217 "pygments_lexer": "ipython3",