"cell_type": "markdown",
"metadata": {},
"source": [
- "# Part 2"
+ "# Part 2\n",
+ "\n",
+ "I have the `mower` as an \"object\" storing its state. (In this case, the mower is a `dict` and the state is just its location and direction.) As each instruction is executed, the mower is updated."
]
},
{
- "cell_type": "code",
- "execution_count": 7,
+ "cell_type": "markdown",
"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"
+ "An initial mower. The initial location and direction don't matter in this case, so I choose a location that's easy and an arbitrary location."
]
},
{
]
},
{
- "cell_type": "code",
- "execution_count": 16,
+ "cell_type": "markdown",
"metadata": {},
- "outputs": [],
"source": [
- "def mowmaster_distance(mw):\n",
- " return abs(mw['x']) + abs(mw['y'])"
+ "Execute the instructions. If it starts `C` or `A`, turn; if it starts `F`, move forward. Ignore all other instructions."
]
},
{
"def execute(mowmaster, instructions, debug=False):\n",
" for instruction in instructions:\n",
" if instruction == 'C':\n",
- " mowmaster['d'] = (mowmaster['d'] + 90) % 360\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",
" return mowmaster"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The "
+ ]
+ },
+ {
+ "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": 16,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def mowmaster_distance(mw):\n",
+ " return abs(mw['x']) + abs(mw['y'])"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 18,