diff --git a/config.json b/config.json index 04357c2..5d8870f 100644 --- a/config.json +++ b/config.json @@ -528,6 +528,14 @@ "practices": [], "prerequisites": [], "difficulty": 2 + }, + { + "slug": "robot-simulator", + "name": "Robot Simulator", + "uuid": "b89d7805-b71f-45f9-929d-4f1ea8f695e9", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/robot-simulator/.docs/instructions.md b/exercises/practice/robot-simulator/.docs/instructions.md new file mode 100644 index 0000000..0ac96ce --- /dev/null +++ b/exercises/practice/robot-simulator/.docs/instructions.md @@ -0,0 +1,25 @@ +# Instructions + +Write a robot simulator. + +A robot factory's test facility needs a program to verify robot movements. + +The robots have three possible movements: + +- turn right +- turn left +- advance + +Robots are placed on a hypothetical infinite grid, facing a particular direction (north, east, south, or west) at a set of {x,y} coordinates, +e.g., {3,8}, with coordinates increasing to the north and east. + +The robot then receives a number of instructions, at which point the testing facility verifies the robot's new position, and in which direction it is pointing. + +- The letter-string "RAALAL" means: + - Turn right + - Advance twice + - Turn left + - Advance once + - Turn left yet again +- Say a robot starts at {7, 3} facing north. + Then running this stream of instructions should leave it at {9, 4} facing west. diff --git a/exercises/practice/robot-simulator/.meta/config.json b/exercises/practice/robot-simulator/.meta/config.json new file mode 100644 index 0000000..c527f61 --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "source/robot_simulator.d" + ], + "test": [ + "source/robot_simulator.d" + ], + "example": [ + "example/robot_simulator.d" + ] + }, + "blurb": "Write a robot simulator.", + "source": "Inspired by an interview question at a famous company." +} diff --git a/exercises/practice/robot-simulator/.meta/tests.toml b/exercises/practice/robot-simulator/.meta/tests.toml new file mode 100644 index 0000000..16da03d --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/tests.toml @@ -0,0 +1,64 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[c557c16d-26c1-4e06-827c-f6602cd0785c] +description = "Create robot -> at origin facing north" + +[bf0dffce-f11c-4cdb-8a5e-2c89d8a5a67d] +description = "Create robot -> at negative position facing south" + +[8cbd0086-6392-4680-b9b9-73cf491e67e5] +description = "Rotating clockwise -> changes north to east" + +[8abc87fc-eab2-4276-93b7-9c009e866ba1] +description = "Rotating clockwise -> changes east to south" + +[3cfe1b85-bbf2-4bae-b54d-d73e7e93617a] +description = "Rotating clockwise -> changes south to west" + +[5ea9fb99-3f2c-47bd-86f7-46b7d8c3c716] +description = "Rotating clockwise -> changes west to north" + +[fa0c40f5-6ba3-443d-a4b3-58cbd6cb8d63] +description = "Rotating counter-clockwise -> changes north to west" + +[da33d734-831f-445c-9907-d66d7d2a92e2] +description = "Rotating counter-clockwise -> changes west to south" + +[bd1ca4b9-4548-45f4-b32e-900fc7c19389] +description = "Rotating counter-clockwise -> changes south to east" + +[2de27b67-a25c-4b59-9883-bc03b1b55bba] +description = "Rotating counter-clockwise -> changes east to north" + +[f0dc2388-cddc-4f83-9bed-bcf46b8fc7b8] +description = "Moving forward one -> facing north increments Y" + +[2786cf80-5bbf-44b0-9503-a89a9c5789da] +description = "Moving forward one -> facing south decrements Y" + +[84bf3c8c-241f-434d-883d-69817dbd6a48] +description = "Moving forward one -> facing east increments X" + +[bb69c4a7-3bbf-4f64-b415-666fa72d7b04] +description = "Moving forward one -> facing west decrements X" + +[e34ac672-4ed4-4be3-a0b8-d9af259cbaa1] +description = "Follow series of instructions -> moving east and north from README" + +[f30e4955-4b47-4aa3-8b39-ae98cfbd515b] +description = "Follow series of instructions -> moving west and north" + +[3e466bf6-20ab-4d79-8b51-264165182fca] +description = "Follow series of instructions -> moving west and south" + +[41f0bb96-c617-4e6b-acff-a4b279d44514] +description = "Follow series of instructions -> moving east and north" diff --git a/exercises/practice/robot-simulator/dub.sdl b/exercises/practice/robot-simulator/dub.sdl new file mode 100644 index 0000000..f4ac7c8 --- /dev/null +++ b/exercises/practice/robot-simulator/dub.sdl @@ -0,0 +1,2 @@ +name "robot-simulator" +buildRequirements "disallowDeprecations" diff --git a/exercises/practice/robot-simulator/example/robot_simulator.d b/exercises/practice/robot-simulator/example/robot_simulator.d new file mode 100644 index 0000000..2d6df32 --- /dev/null +++ b/exercises/practice/robot-simulator/example/robot_simulator.d @@ -0,0 +1,64 @@ +module robot_simulator; + +string[string][string] directions; + +shared static this() +{ + directions["north"] = ["left": "west", "right": "east"]; + directions["east"] = ["left": "north", "right": "south"]; + directions["south"] = ["left": "east", "right": "west"]; + directions["west"] = ["left": "south", "right": "north"]; +} + +class RobotSimulator +{ +public: + this(int x, int y, string direction) + { + this.x = x; + this.y = y; + this.direction = direction; + } + + void move(string commands) + { + foreach (command; commands) + { + if (command == 'A') + { + advance(); + } else + { + rotate(command); + } + } + } + +private: + void rotate(char command) + { + string key = command == 'L' ? "left" : "right"; + this.direction = directions[this.direction][key]; + } + + void advance() + { + if (this.direction == "north") + { + this.y += 1; + } else if (this.direction == "east") + { + this.x += 1; + } else if (this.direction == "south") + { + this.y -= 1; + } else if (this.direction == "west") + { + this.x -= 1; + } + } + + int x; + int y; + string direction; +} diff --git a/exercises/practice/robot-simulator/source/robot_simulator.d b/exercises/practice/robot-simulator/source/robot_simulator.d new file mode 100644 index 0000000..3ce83a0 --- /dev/null +++ b/exercises/practice/robot-simulator/source/robot_simulator.d @@ -0,0 +1,183 @@ +module robot_simulator; + +class RobotSimulator +{ + this(int x, int y, string direction) + { + // implement this function + } + + void move(string commands) + { + // implement this function + } +} + +unittest +{ + immutable int allTestsEnabled = 0; + + // Create robot at origin facing north + { + RobotSimulator robot = new RobotSimulator(0, 0, "north"); + assert(robot.x == 0); + assert(robot.y == 0); + assert(robot.direction == "north"); + } + + static if (allTestsEnabled) + { + // Create robot at negative position facing south + { + RobotSimulator robot = new RobotSimulator(-1, -1, "south"); + assert(robot.x == -1); + assert(robot.y == -1); + assert(robot.direction == "south"); + } + + // Rotating clockwise changes north to east + { + RobotSimulator robot = new RobotSimulator(0, 0, "north"); + robot.move("R"); + assert(robot.x == 0); + assert(robot.y == 0); + assert(robot.direction == "east"); + } + + // Rotating clockwise changes east to south + { + RobotSimulator robot = new RobotSimulator(0, 0, "east"); + robot.move("R"); + assert(robot.x == 0); + assert(robot.y == 0); + assert(robot.direction == "south"); + } + + // Rotating clockwise changes south to west + { + RobotSimulator robot = new RobotSimulator(0, 0, "south"); + robot.move("R"); + assert(robot.x == 0); + assert(robot.y == 0); + assert(robot.direction == "west"); + } + + // Rotating clockwise changes west to north + { + RobotSimulator robot = new RobotSimulator(0, 0, "west"); + robot.move("R"); + assert(robot.x == 0); + assert(robot.y == 0); + assert(robot.direction == "north"); + } + + // Rotating counter-clockwise changes north to west + { + RobotSimulator robot = new RobotSimulator(0, 0, "north"); + robot.move("L"); + assert(robot.x == 0); + assert(robot.y == 0); + assert(robot.direction == "west"); + } + + // Rotating counter-clockwise changes west to south + { + RobotSimulator robot = new RobotSimulator(0, 0, "west"); + robot.move("L"); + assert(robot.x == 0); + assert(robot.y == 0); + assert(robot.direction == "south"); + } + + // Rotating counter-clockwise changes south to east + { + RobotSimulator robot = new RobotSimulator(0, 0, "south"); + robot.move("L"); + assert(robot.x == 0); + assert(robot.y == 0); + assert(robot.direction == "east"); + } + + // Rotating counter-clockwise changes east to north + { + RobotSimulator robot = new RobotSimulator(0, 0, "east"); + robot.move("L"); + assert(robot.x == 0); + assert(robot.y == 0); + assert(robot.direction == "north"); + } + + // Moving forward one facing north increments Y + { + RobotSimulator robot = new RobotSimulator(0, 0, "north"); + robot.move("A"); + assert(robot.x == 0); + assert(robot.y == 1); + assert(robot.direction == "north"); + } + + // Moving forward one facing south decrements Y + { + RobotSimulator robot = new RobotSimulator(0, 0, "south"); + robot.move("A"); + assert(robot.x == 0); + assert(robot.y == -1); + assert(robot.direction == "south"); + } + + // Moving forward one facing east increments X + { + RobotSimulator robot = new RobotSimulator(0, 0, "east"); + robot.move("A"); + assert(robot.x == 1); + assert(robot.y == 0); + assert(robot.direction == "east"); + } + + // Moving forward one facing west decrements X + { + RobotSimulator robot = new RobotSimulator(0, 0, "west"); + robot.move("A"); + assert(robot.x == -1); + assert(robot.y == 0); + assert(robot.direction == "west"); + } + + // Follow series of instructions moving east and north from README + { + RobotSimulator robot = new RobotSimulator(7, 3, "north"); + robot.move("RAALAL"); + assert(robot.x == 9); + assert(robot.y == 4); + assert(robot.direction == "west"); + } + + // Follow series of instructions moving west and north + { + RobotSimulator robot = new RobotSimulator(0, 0, "north"); + robot.move("LAAARALA"); + assert(robot.x == -4); + assert(robot.y == 1); + assert(robot.direction == "west"); + } + + // Follow series of instructions moving west and south + { + RobotSimulator robot = new RobotSimulator(2, -7, "east"); + robot.move("RRAAAAALA"); + assert(robot.x == -3); + assert(robot.y == -8); + assert(robot.direction == "south"); + } + + // Follow series of instructions moving east and north + { + RobotSimulator robot = new RobotSimulator(8, 4, "south"); + robot.move("LAAARRRALLLL"); + assert(robot.x == 11); + assert(robot.y == 5); + assert(robot.direction == "north"); + } + } + +}