Skip to content

Instantly share code, notes, and snippets.

@jonataa
Last active May 23, 2023 16:58
Show Gist options
  • Save jonataa/b909e9a5ff4b598920ffbdc3261eadb1 to your computer and use it in GitHub Desktop.
Save jonataa/b909e9a5ff4b598920ffbdc3261eadb1 to your computer and use it in GitHub Desktop.

The Toy Robot Challenge

The application is a simulation of a toy robot moving on a square tabletop, of dimensions 5 units x 5 units. There are no other obstructions on the table surface. The robot is free to roam around the surface of the table. Any movement that would result in the robot falling from the table is prevented, however further valid movement commands are still allowed. The application reads a file using a name passed in the command line, the Ifollowing commands are valid:

PLACE X,Y,F
MOVE
LEFT
RIGHT
REPORT
  • PLACE will put the toy robot on the table in position X,Y and facing NORTH SOUTH, EAST or WEST.
  • The origin(0, 0) is the SOUTH WEST most corner.
  • All commands are ignored until a valid PLACE is made.
  • MOVE will move the toy robot one unit forward in the direction it is Icurrently facing.
    • LEFT and RIGHT rotates the robot 90 degrees in the specified direction Iwithout changing the position of the robot.
    • REPORT announces the X, Y and F of the robot.

The file is assumed to have ASCII encoding. It is assumed that the PLACE command has only one space, that is PLACE 1, 2, NORTH is an invalid command. All commands must be in uppercase, all lower and mixed case commands will be ignored.

Ultimately, this description wants us to build an application that will take a list of commands such as:

commands.txt

PLACE 0,0,NORTH
MOVE
RIGHT
MOVE
LEFT
MOVE
REPORT

Once the application receives these commands, it should run a simulation for a toy robot which obeys these commands and then if the list of commands includes a REPORT command, the application will then tell us where the robot is:

$ toyrobot commands.txt

Robot is currently at (1, 2) and it's facing NORTH

In this example, the two coordinates refer to the robot’s EAST and NORTH coordinates respectively. The robot started at (0, 0) and was facing NORTH. The robot then moves (to (EAST=0, NORTH=1) or (0, 1) for short), turns right, moves again (to (1, 1)), turns left, and moves a final time (to (1, 2)) before reporting its location. The robot tells us that it has the coordinates of (EAST=1, NORTH=2), as it moved EAST once and NORTH twice given the commands that we provided it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment