- " if instruction == 'C':\n",
- " mowmaster['d'] = (mowmaster['d'] + 90) % 360 # Use the modul\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",
+ " mowmaster = execute_one(mowmaster, instruction, debug=debug)\n",
+ " return mowmaster"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Execute one instruction. If it starts `C` or `A`, turn; if it starts `F`, move forward. Ignore all other instructions."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def execute_one(mowmaster, instruction, debug=False):\n",
+ " if instruction == 'C':\n",
+ " mowmaster['d'] = (mowmaster['d'] + 90) % 360 # Use the modulus operator to keep the angle in the range 0--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",