9 "instructions = [l.strip() for l in open('../../data/03-graffiti.txt') if not l.startswith('#')]"
13 "cell_type": "markdown",
17 "Similar to task 1, but rather than just tracking the state of the mower, now we track the state of the grass as well.\n",
19 "The grass is a `dict` of mown patches. The key in the `grass` is a pair (2-tuple) of `x` and `y` position. The value is `True` if the patch is mown. Unmown patches aren't recorded in the `grass`.\n",
21 "`pen` records whether the Mowmaster is mowing or not."
26 "execution_count": 18,
30 "def initial_world():\n",
31 " return {'x': 0, 'y': 0, 'd': 0, 'pen': False, 'grass': {}}"
35 "cell_type": "markdown",
38 "One function for each command. The function is passed the world, and it updates the world."
43 "execution_count": 20,
47 "def f(world, distance):\n",
48 " for d in range(distance):\n",
49 " if world['d'] == 0:\n",
51 " elif world['d'] == 90:\n",
53 " elif world['d'] == 180:\n",
55 " elif world['d'] == 270:\n",
58 " raise ValueError\n",
59 " if world['pen']:\n",
60 " world['grass'][world['x'], world['y']] = True\n",
71 " world['d'] = (world['d'] + 90) % 360\n",
82 " world['d'] = (world['d'] - 90) % 360\n",
93 " world['pen'] = False\n",
104 " world['pen'] = True\n",
105 " world['grass'][world['x'], world['y']] = True\n",
110 "cell_type": "markdown",
113 "A dispatch table of commands. The keys are the command names, the values is the function to call and whether that function takes an argument or not."
118 "execution_count": 21,
123 " 'F': {'func': f, 'arg': True},\n",
124 " 'C': {'func': c, 'arg': False},\n",
125 " 'A': {'func': a, 'arg': False},\n",
126 " 'U': {'func': u, 'arg': False},\n",
127 " 'D': {'func': d, 'arg': False},\n",
133 "execution_count": 12,
137 "def execute(world, instructions, debug=False):\n",
138 " for instruction in instructions:\n",
139 " world = execute_one(world, instruction, debug=debug)\n",
144 "cell_type": "markdown",
147 "To execute a command, look it up in the dispatch table. If it's there, call the function."
152 "execution_count": 14,
156 "def execute_one(world, instruction, debug=False):\n",
157 " instruction_name = instruction[0]\n",
158 " if instruction_name in table:\n",
159 " if table[instruction_name]['arg']:\n",
160 " arg = int(instruction[1:])\n",
161 " world = table[instruction_name]['func'](world, arg)\n",
163 " world = table[instruction_name]['func'](world)\n",
169 "execution_count": 36,
178 "execution_count": 36,
180 "output_type": "execute_result"
184 "w = initial_world()\n",
185 "execute(w, instructions)\n",
190 "cell_type": "markdown",
195 "The `show_world` returns a string with a square for each mown patch. The string contains the embedded newlines, so I call `print` on the result."
200 "execution_count": 31,
204 "def show_world(world):\n",
205 " width_max = max(p[0] for p in world['grass'])\n",
206 " width_min = min(p[0] for p in world['grass'])\n",
207 " height_max = max(p[1] for p in world['grass'])\n",
208 " height_min = min(p[1] for p in world['grass'])\n",
210 " for r in range(height_max, height_min-1, -1):\n",
211 " display[r] = ''\n",
212 " for c in range(width_min, width_max+1):\n",
213 " if (c, r) in world['grass']:\n",
214 " display[r] += '⌷'\n",
216 " display[r] += ' '\n",
217 " return '\\n'.join(display[r] for r in reversed(sorted(display)))"
222 "execution_count": 37,
227 "output_type": "stream",
229 "⌷⌷⌷⌷⌷⌷ ⌷ ⌷ ⌷⌷⌷⌷⌷⌷⌷⌷ ⌷⌷⌷⌷⌷⌷⌷⌷ ⌷⌷⌷⌷⌷⌷ ⌷⌷ ⌷⌷ ⌷ ⌷ ⌷ ⌷⌷ ⌷⌷⌷⌷⌷⌷⌷⌷ ⌷⌷⌷⌷⌷⌷⌷\n",
230 "⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷⌷ ⌷ ⌷ ⌷ ⌷ ⌷ \n",
231 "⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ \n",
232 "⌷⌷⌷⌷⌷⌷ ⌷ ⌷ ⌷⌷⌷⌷⌷⌷⌷⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷⌷⌷⌷⌷⌷⌷\n",
233 "⌷ ⌷ ⌷ ⌷ ⌷⌷⌷⌷⌷⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ \n",
234 "⌷ ⌷⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ \n",
235 "⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷⌷⌷⌷⌷⌷⌷ ⌷ ⌷ \n",
236 "⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷⌷ ⌷ ⌷ ⌷ ⌷ \n",
237 "⌷⌷⌷⌷⌷⌷ ⌷ ⌷ ⌷ ⌷⌷⌷⌷⌷⌷⌷⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷⌷⌷⌷⌷⌷⌷\n"
242 "print(show_world(w))"
247 "execution_count": null,
255 "display_name": "Python 3",
256 "language": "python",
264 "file_extension": ".py",
265 "mimetype": "text/x-python",
267 "nbconvert_exporter": "python",
268 "pygments_lexer": "ipython3",