The Robot Vacuum's Run

Tuples and SetsMedium+20 XP+20

The Robot Vacuum's Run

A robot vacuum cleans a floor made of square tiles. Every tile has a position (x, y): x grows towards the east and y grows towards the north. The floor carries on as far as the robot likes in every direction — there are no walls.

The robot starts on tile (0, 0), facing north. Its facing is kept as a step tuple (dx, dy): the change one move forward makes to its position.

facingstep tuple
north(0, 1)
east(1, 0)
south(0, -1)
west(-1, 0)

It follows a string of one-letter commands:

commandwhat the robot does
Fmoves forward one tile — unless that tile has furniture on it. Then it bumps, stays exactly where it is, and goes on to the next command.
Lturns 90° to its left, without moving
Rturns 90° to its right, without moving

furniture is a list of (x, y) tiles the robot can never enter (a tile can be listed more than once). The start tile is always free.

Task: write vacuum_run(commands, furniture), returning a tuple (position, facing, cleaned):

  • position — the tile it finishes on,
  • facing — its final step tuple,
  • cleaned — how many different tiles it has stood on, including the start tile.

Example

It moves north to (0, 1), turns to face east, moves to (1, 1), then bumps into the furniture at (2, 1) and stays put.

Examples

commands = "FRFF"
furniture = [(2, 1)]
returns ((1, 1), (1, 0), 3)
commands = "FFLLFF"
furniture = []
returns ((0, 0), (0, -1), 3)

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run