diff --git a/config.json b/config.json index c7e9b7c..fcd35cf 100644 --- a/config.json +++ b/config.json @@ -374,6 +374,14 @@ "practices": [], "prerequisites": [], "difficulty": 4 + }, + { + "slug": "robot-simulator", + "name": "Robot Simulator", + "uuid": "55452cf4-0037-4bf5-a518-c16fb5d1da83", + "practices": [], + "prerequisites": [], + "difficulty": 4 } ] }, diff --git a/exercises/practice/robot-simulator/.busted b/exercises/practice/robot-simulator/.busted new file mode 100644 index 0000000..86b84e7 --- /dev/null +++ b/exercises/practice/robot-simulator/.busted @@ -0,0 +1,5 @@ +return { + default = { + ROOT = { '.' } + } +} 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..fb94098 --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "robot_simulator.moon" + ], + "test": [ + "robot_simulator_spec.moon" + ], + "example": [ + ".meta/example.moon" + ] + }, + "blurb": "Write a robot simulator.", + "source": "Inspired by an interview question at a famous company." +} diff --git a/exercises/practice/robot-simulator/.meta/example.moon b/exercises/practice/robot-simulator/.meta/example.moon new file mode 100644 index 0000000..b024a08 --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/example.moon @@ -0,0 +1,29 @@ +Turns = { + north: {left: 'west', right: 'east'}, + east: {left: 'north', right: 'south'}, + south: {left: 'east', right: 'west'}, + west: {left: 'south', right: 'north'}, +} + + +class Robot + new: (params) => + @_x = params.x + @_y = params.y + @_dir = params.direction + + x: => @_x + y: => @_y + direction: => @_dir + + move: (instructions) => + for instruction in instructions\gmatch('[RLA]') + switch instruction + when 'R' then @_dir = Turns[@_dir].right + when 'L' then @_dir = Turns[@_dir].left + when 'A' + switch @_dir + when 'north' then @_y += 1 + when 'east' then @_x += 1 + when 'south' then @_y -= 1 + when 'west' then @_x -= 1 diff --git a/exercises/practice/robot-simulator/.meta/spec_generator.moon b/exercises/practice/robot-simulator/.meta/spec_generator.moon new file mode 100644 index 0000000..138919a --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/spec_generator.moon @@ -0,0 +1,26 @@ +{ + module_name: 'Robot', + + generate_test: (case, level) -> + local lines + switch case.property + when 'create' + lines = { + "robot = Robot x: #{case.input.position.x}, y: #{case.input.position.y}, direction: #{quote case.input.direction}", + "assert.are.equal #{case.expected.position.x}, robot\\x!", + "assert.are.equal #{case.expected.position.y}, robot\\y!", + "assert.are.equal #{quote case.expected.direction}, robot\\direction!" + } + + when 'move' + lines = { + "robot = Robot x: #{case.input.position.x}, y: #{case.input.position.y}, direction: #{quote case.input.direction}", + "robot\\move #{quote case.input.instructions}", + + "assert.are.equal #{case.expected.position.x}, robot\\x!", + "assert.are.equal #{case.expected.position.y}, robot\\y!", + "assert.are.equal #{quote case.expected.direction}, robot\\direction!" + } + + table.concat [indent line, level for line in *lines], '\n' +} 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/robot_simulator.moon b/exercises/practice/robot-simulator/robot_simulator.moon new file mode 100644 index 0000000..bfb6c55 --- /dev/null +++ b/exercises/practice/robot-simulator/robot_simulator.moon @@ -0,0 +1,15 @@ +class Robot + new: (params) => + error 'Implement the constructor' + + x: => + error 'Implement the x method' + + y: => + error 'Implement the y method' + + direction: => + error 'Implement the direction method' + + move: (instructions) => + error 'Implement the move method' diff --git a/exercises/practice/robot-simulator/robot_simulator_spec.moon b/exercises/practice/robot-simulator/robot_simulator_spec.moon new file mode 100644 index 0000000..b748c8a --- /dev/null +++ b/exercises/practice/robot-simulator/robot_simulator_spec.moon @@ -0,0 +1,131 @@ +Robot = require 'robot_simulator' + +describe 'robot-simulator', -> + describe 'Create robot', -> + it 'at origin facing north', -> + robot = Robot x: 0, y: 0, direction: 'north' + assert.are.equal 0, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'north', robot\direction! + + pending 'at negative position facing south', -> + robot = Robot x: -1, y: -1, direction: 'south' + assert.are.equal -1, robot\x! + assert.are.equal -1, robot\y! + assert.are.equal 'south', robot\direction! + + describe 'Rotating clockwise', -> + pending 'changes north to east', -> + robot = Robot x: 0, y: 0, direction: 'north' + robot\move 'R' + assert.are.equal 0, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'east', robot\direction! + + pending 'changes east to south', -> + robot = Robot x: 0, y: 0, direction: 'east' + robot\move 'R' + assert.are.equal 0, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'south', robot\direction! + + pending 'changes south to west', -> + robot = Robot x: 0, y: 0, direction: 'south' + robot\move 'R' + assert.are.equal 0, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'west', robot\direction! + + pending 'changes west to north', -> + robot = Robot x: 0, y: 0, direction: 'west' + robot\move 'R' + assert.are.equal 0, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'north', robot\direction! + + describe 'Rotating counter-clockwise', -> + pending 'changes north to west', -> + robot = Robot x: 0, y: 0, direction: 'north' + robot\move 'L' + assert.are.equal 0, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'west', robot\direction! + + pending 'changes west to south', -> + robot = Robot x: 0, y: 0, direction: 'west' + robot\move 'L' + assert.are.equal 0, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'south', robot\direction! + + pending 'changes south to east', -> + robot = Robot x: 0, y: 0, direction: 'south' + robot\move 'L' + assert.are.equal 0, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'east', robot\direction! + + pending 'changes east to north', -> + robot = Robot x: 0, y: 0, direction: 'east' + robot\move 'L' + assert.are.equal 0, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'north', robot\direction! + + describe 'Moving forward one', -> + pending 'facing north increments Y', -> + robot = Robot x: 0, y: 0, direction: 'north' + robot\move 'A' + assert.are.equal 0, robot\x! + assert.are.equal 1, robot\y! + assert.are.equal 'north', robot\direction! + + pending 'facing south decrements Y', -> + robot = Robot x: 0, y: 0, direction: 'south' + robot\move 'A' + assert.are.equal 0, robot\x! + assert.are.equal -1, robot\y! + assert.are.equal 'south', robot\direction! + + pending 'facing east increments X', -> + robot = Robot x: 0, y: 0, direction: 'east' + robot\move 'A' + assert.are.equal 1, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'east', robot\direction! + + pending 'facing west decrements X', -> + robot = Robot x: 0, y: 0, direction: 'west' + robot\move 'A' + assert.are.equal -1, robot\x! + assert.are.equal 0, robot\y! + assert.are.equal 'west', robot\direction! + + describe 'Follow series of instructions', -> + pending 'moving east and north from README', -> + robot = Robot x: 7, y: 3, direction: 'north' + robot\move 'RAALAL' + assert.are.equal 9, robot\x! + assert.are.equal 4, robot\y! + assert.are.equal 'west', robot\direction! + + pending 'moving west and north', -> + robot = Robot x: 0, y: 0, direction: 'north' + robot\move 'LAAARALA' + assert.are.equal -4, robot\x! + assert.are.equal 1, robot\y! + assert.are.equal 'west', robot\direction! + + pending 'moving west and south', -> + robot = Robot x: 2, y: -7, direction: 'east' + robot\move 'RRAAAAALA' + assert.are.equal -3, robot\x! + assert.are.equal -8, robot\y! + assert.are.equal 'south', robot\direction! + + pending 'moving east and north', -> + robot = Robot x: 8, y: 4, direction: 'south' + robot\move 'LAAARRRALLLL' + assert.are.equal 11, robot\x! + assert.are.equal 5, robot\y! + assert.are.equal 'north', robot\direction!