From 651c63490e673b710291d7a3dc6675bb2c58569d Mon Sep 17 00:00:00 2001 From: Andrea Manzini Date: Sat, 27 Jan 2024 16:28:11 +0100 Subject: [PATCH 01/17] exercises(robot-simulator): implement --- config.json | 8 ++ .../robot-simulator/.docs/instructions.md | 25 ++++ .../robot-simulator/.meta/config.json | 16 +++ .../robot-simulator/.meta/example.nim | 38 ++++++ .../practice/robot-simulator/.meta/tests.toml | 64 +++++++++++ .../robot-simulator/robot_simulator.nim | 15 +++ .../robot-simulator/test_robot_simulator.nim | 108 ++++++++++++++++++ 7 files changed, 274 insertions(+) create mode 100644 exercises/practice/robot-simulator/.docs/instructions.md create mode 100644 exercises/practice/robot-simulator/.meta/config.json create mode 100644 exercises/practice/robot-simulator/.meta/example.nim create mode 100644 exercises/practice/robot-simulator/.meta/tests.toml create mode 100644 exercises/practice/robot-simulator/robot_simulator.nim create mode 100644 exercises/practice/robot-simulator/test_robot_simulator.nim diff --git a/config.json b/config.json index 32448b50..ed7073a7 100644 --- a/config.json +++ b/config.json @@ -979,6 +979,14 @@ "events", "reactive_programming" ] + }, + { + "slug": "robot-simulator", + "name": "Robot Simulator", + "uuid": "cfedbb93-eda4-49f9-92a1-aa1a6cc8453f", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ] }, diff --git a/exercises/practice/robot-simulator/.docs/instructions.md b/exercises/practice/robot-simulator/.docs/instructions.md new file mode 100644 index 00000000..0ac96ce0 --- /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 00000000..ce4d43e7 --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/config.json @@ -0,0 +1,16 @@ +{ + "authors": [], + "files": { + "solution": [ + "robot_simulator.nim" + ], + "test": [ + "test_robot_simulator.nim" + ], + "example": [ + ".meta/example.nim" + ] + }, + "blurb": "Write a robot simulator.", + "source": "Inspired by an interview question at a famous company." +} diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim new file mode 100644 index 00000000..d537eb6d --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -0,0 +1,38 @@ +type + Compass* = enum + North,East,South,West + Robot* = object + direction*: Compass + xPos*: int + yPos*: int + +proc initRobot*(facing: Compass=North, x: int=0, y: int=0) : Robot = + Robot(direction: facing,xPos: x,yPos: y) + +proc right(dir: Compass) : Compass = + case dir: + of North: East + of East: South + of South: West + of West: North + +proc left(dir: Compass) : Compass = + case dir: + of North: West + of East: North + of South: East + of West: South + +proc move*(robot: var Robot, instructions: string) = + for c in instructions: + if c=='L': + robot.direction=robot.direction.left + if c=='R': + robot.direction=robot.direction.right + if c!='A': + continue + case robot.direction: + of North: inc(robot.yPos) + of South: dec(robot.yPos) + of East: inc(robot.xPos) + of West: dec(robot.xPos) diff --git a/exercises/practice/robot-simulator/.meta/tests.toml b/exercises/practice/robot-simulator/.meta/tests.toml new file mode 100644 index 00000000..16da03d4 --- /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.nim b/exercises/practice/robot-simulator/robot_simulator.nim new file mode 100644 index 00000000..0aad0809 --- /dev/null +++ b/exercises/practice/robot-simulator/robot_simulator.nim @@ -0,0 +1,15 @@ +type + Compass* = enum + North,East,South,West + Robot* = object + direction*: Compass + xPos*: int + yPos*: int + +# constructor proc to return a new robot with default attributes +proc initRobot*(facing: Compass=North, x: int=0, y: int=0) : Robot = + discard + +# implement this proc to move the robot following the instructions +proc move*(robot: var Robot, instructions: string) = + discard diff --git a/exercises/practice/robot-simulator/test_robot_simulator.nim b/exercises/practice/robot-simulator/test_robot_simulator.nim new file mode 100644 index 00000000..eaf6c79b --- /dev/null +++ b/exercises/practice/robot-simulator/test_robot_simulator.nim @@ -0,0 +1,108 @@ +import unittest +import robot_simulator + +suite "Robot_Simulator": + test "Create robot -> at origin facing north": + var robot = initRobot() + check robot.direction == North + + test "Create robot -> at negative position facing south": + var robot = initRobot(South,-1,-1) + check robot.direction == South + check robot.xPos == -1 and robot.yPos == -1 + + test "Rotating clockwise -> changes north to east": + var robot = initRobot() + robot.move("R") + check robot.direction == East + check robot.xPos == 0 and robot.yPos == 0 + + test "Rotating clockwise -> changes east to south": + var robot = initRobot(East) + robot.move("R") + check robot.direction == South + check robot.xPos == 0 and robot.yPos == 0 + + test "Rotating clockwise -> changes south to west": + var robot = initRobot(South) + robot.move("R") + check robot.direction == West + check robot.xPos == 0 and robot.yPos == 0 + + test "Rotating clockwise -> changes west to north": + var robot = initRobot(West) + robot.move("R") + check robot.direction == North + check robot.xPos == 0 and robot.yPos == 0 + + test "Rotating counter-clockwise -> changes north to west": + var robot = initRobot() + robot.move("L") + check robot.direction == West + check robot.xPos == 0 and robot.yPos == 0 + + test "Rotating counter-clockwise -> changes west to south": + var robot = initRobot(West) + robot.move("L") + check robot.direction == South + check robot.xPos == 0 and robot.yPos == 0 + + test "Rotating counter-clockwise -> changes south to east": + var robot = initRobot(South) + robot.move("L") + check robot.direction == East + check robot.xPos == 0 and robot.yPos == 0 + + test "Rotating counter-clockwise -> changes east to north": + var robot = initRobot(East) + robot.move("L") + check robot.direction == North + check robot.xPos == 0 and robot.yPos == 0 + + test "Moving forward one -> facing north increments Y": + var robot = initRobot() + robot.move("A") + check robot.direction == North + check robot.xPos == 0 and robot.yPos == 1 + + test "Moving forward one -> facing south decrements Y": + var robot = initRobot(South) + robot.move("A") + check robot.direction == South + check robot.xPos == 0 and robot.yPos == -1 + + test "Moving forward one -> facing east increments X": + var robot = initRobot(East) + robot.move("A") + check robot.direction == East + check robot.xPos == 1 and robot.yPos == 0 + + test "Moving forward one -> facing west decrements X": + var robot = initRobot(West) + robot.move("A") + check robot.direction == West + check robot.xPos == -1 and robot.yPos == 0 + + test "Follow series of instructions -> moving east and north from README": + var robot = initRobot(North,7,3) + robot.move("RAALAL") + check robot.direction == West + check robot.xPos == 9 and robot.yPos == 4 + + test "Follow series of instructions -> moving west and north": + var robot = initRobot() + robot.move("LAAARALA") + check robot.direction == West + check robot.xPos == -4 and robot.yPos == 1 + + test "Follow series of instructions -> moving west and south": + var robot = initRobot(East,2,-7) + robot.move("RRAAAAALA") + check robot.direction == South + check robot.xPos == -3 and robot.yPos == -8 + + test "Follow series of instructions -> moving east and north": + var robot = initRobot(South,8,4) + robot.move("LAAARRRALLLL") + check robot.direction == North + check robot.xPos == 11 and robot.yPos == 5 From c97ebb66d8f78c843caba3fe2d1d13fd972bb5e1 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:01 +0100 Subject: [PATCH 02/17] exercises(robot-simulator): example, stub, tests: format --- .../robot-simulator/.meta/example.nim | 21 ++++++++++--------- .../robot-simulator/robot_simulator.nim | 5 +++-- .../robot-simulator/test_robot_simulator.nim | 8 +++---- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index d537eb6d..a880aadb 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -1,22 +1,23 @@ type Compass* = enum - North,East,South,West + North, East, South, West + Robot* = object direction*: Compass xPos*: int yPos*: int -proc initRobot*(facing: Compass=North, x: int=0, y: int=0) : Robot = - Robot(direction: facing,xPos: x,yPos: y) +proc initRobot*(facing: Compass = North, x: int = 0, y: int = 0): Robot = + Robot(direction: facing, xPos: x, yPos: y) -proc right(dir: Compass) : Compass = +proc right(dir: Compass): Compass = case dir: of North: East of East: South of South: West of West: North -proc left(dir: Compass) : Compass = +proc left(dir: Compass): Compass = case dir: of North: West of East: North @@ -25,11 +26,11 @@ proc left(dir: Compass) : Compass = proc move*(robot: var Robot, instructions: string) = for c in instructions: - if c=='L': - robot.direction=robot.direction.left - if c=='R': - robot.direction=robot.direction.right - if c!='A': + if c == 'L': + robot.direction = robot.direction.left + if c == 'R': + robot.direction = robot.direction.right + if c != 'A': continue case robot.direction: of North: inc(robot.yPos) diff --git a/exercises/practice/robot-simulator/robot_simulator.nim b/exercises/practice/robot-simulator/robot_simulator.nim index 0aad0809..d7bc3c8a 100644 --- a/exercises/practice/robot-simulator/robot_simulator.nim +++ b/exercises/practice/robot-simulator/robot_simulator.nim @@ -1,13 +1,14 @@ type Compass* = enum - North,East,South,West + North, East, South, West + Robot* = object direction*: Compass xPos*: int yPos*: int # constructor proc to return a new robot with default attributes -proc initRobot*(facing: Compass=North, x: int=0, y: int=0) : Robot = +proc initRobot*(facing: Compass = North, x: int = 0, y: int = 0): Robot = discard # implement this proc to move the robot following the instructions diff --git a/exercises/practice/robot-simulator/test_robot_simulator.nim b/exercises/practice/robot-simulator/test_robot_simulator.nim index eaf6c79b..0c55153b 100644 --- a/exercises/practice/robot-simulator/test_robot_simulator.nim +++ b/exercises/practice/robot-simulator/test_robot_simulator.nim @@ -7,7 +7,7 @@ suite "Robot_Simulator": check robot.direction == North test "Create robot -> at negative position facing south": - var robot = initRobot(South,-1,-1) + var robot = initRobot(South, -1, -1) check robot.direction == South check robot.xPos == -1 and robot.yPos == -1 @@ -84,7 +84,7 @@ suite "Robot_Simulator": check robot.xPos == -1 and robot.yPos == 0 test "Follow series of instructions -> moving east and north from README": - var robot = initRobot(North,7,3) + var robot = initRobot(North, 7, 3) robot.move("RAALAL") check robot.direction == West check robot.xPos == 9 and robot.yPos == 4 @@ -96,13 +96,13 @@ suite "Robot_Simulator": check robot.xPos == -4 and robot.yPos == 1 test "Follow series of instructions -> moving west and south": - var robot = initRobot(East,2,-7) + var robot = initRobot(East, 2, -7) robot.move("RRAAAAALA") check robot.direction == South check robot.xPos == -3 and robot.yPos == -8 test "Follow series of instructions -> moving east and north": - var robot = initRobot(South,8,4) + var robot = initRobot(South, 8, 4) robot.move("LAAARRRALLLL") check robot.direction == North check robot.xPos == 11 and robot.yPos == 5 From 538b433fe04cab4e26c93ee46baddc3811234aed Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:02 +0100 Subject: [PATCH 03/17] exercises(robot-simulator): example, stub: infer types --- exercises/practice/robot-simulator/.meta/example.nim | 2 +- exercises/practice/robot-simulator/robot_simulator.nim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index a880aadb..70dfae13 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -7,7 +7,7 @@ type xPos*: int yPos*: int -proc initRobot*(facing: Compass = North, x: int = 0, y: int = 0): Robot = +proc initRobot*(facing = North, x = 0, y = 0): Robot = Robot(direction: facing, xPos: x, yPos: y) proc right(dir: Compass): Compass = diff --git a/exercises/practice/robot-simulator/robot_simulator.nim b/exercises/practice/robot-simulator/robot_simulator.nim index d7bc3c8a..0c9a08bb 100644 --- a/exercises/practice/robot-simulator/robot_simulator.nim +++ b/exercises/practice/robot-simulator/robot_simulator.nim @@ -8,7 +8,7 @@ type yPos*: int # constructor proc to return a new robot with default attributes -proc initRobot*(facing: Compass = North, x: int = 0, y: int = 0): Robot = +proc initRobot*(facing = North, x = 0, y = 0): Robot = discard # implement this proc to move the robot following the instructions From 7e70ddaf50b67b7b14faec8b04c6830b60607a40 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:03 +0100 Subject: [PATCH 04/17] exercises(robot-simulator): stub: remove comments --- exercises/practice/robot-simulator/robot_simulator.nim | 2 -- 1 file changed, 2 deletions(-) diff --git a/exercises/practice/robot-simulator/robot_simulator.nim b/exercises/practice/robot-simulator/robot_simulator.nim index 0c9a08bb..46451c16 100644 --- a/exercises/practice/robot-simulator/robot_simulator.nim +++ b/exercises/practice/robot-simulator/robot_simulator.nim @@ -7,10 +7,8 @@ type xPos*: int yPos*: int -# constructor proc to return a new robot with default attributes proc initRobot*(facing = North, x = 0, y = 0): Robot = discard -# implement this proc to move the robot following the instructions proc move*(robot: var Robot, instructions: string) = discard From 233fcff555cb68d709684268e9a4f64b209e36c9 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:04 +0100 Subject: [PATCH 05/17] exercises(robot-simulator): example: use `func` --- exercises/practice/robot-simulator/.meta/example.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index 70dfae13..7755723c 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -7,24 +7,24 @@ type xPos*: int yPos*: int -proc initRobot*(facing = North, x = 0, y = 0): Robot = +func initRobot*(facing = North, x = 0, y = 0): Robot = Robot(direction: facing, xPos: x, yPos: y) -proc right(dir: Compass): Compass = +func right(dir: Compass): Compass = case dir: of North: East of East: South of South: West of West: North -proc left(dir: Compass): Compass = +func left(dir: Compass): Compass = case dir: of North: West of East: North of South: East of West: South -proc move*(robot: var Robot, instructions: string) = +func move*(robot: var Robot, instructions: string) = for c in instructions: if c == 'L': robot.direction = robot.direction.left From ba21c9fcc572f464c9586e2f49a4062e3984b2cd Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:05 +0100 Subject: [PATCH 06/17] exercises(robot-simulator): rename `xPos` to `x`, and `yPos` to `y` --- .../robot-simulator/.meta/example.nim | 14 ++++---- .../robot-simulator/robot_simulator.nim | 4 +-- .../robot-simulator/test_robot_simulator.nim | 34 +++++++++---------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index 7755723c..ce3a2e89 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -4,11 +4,11 @@ type Robot* = object direction*: Compass - xPos*: int - yPos*: int + x*: int + y*: int func initRobot*(facing = North, x = 0, y = 0): Robot = - Robot(direction: facing, xPos: x, yPos: y) + Robot(direction: facing, x: x, y: y) func right(dir: Compass): Compass = case dir: @@ -33,7 +33,7 @@ func move*(robot: var Robot, instructions: string) = if c != 'A': continue case robot.direction: - of North: inc(robot.yPos) - of South: dec(robot.yPos) - of East: inc(robot.xPos) - of West: dec(robot.xPos) + of North: inc(robot.y) + of South: dec(robot.y) + of East: inc(robot.x) + of West: dec(robot.x) diff --git a/exercises/practice/robot-simulator/robot_simulator.nim b/exercises/practice/robot-simulator/robot_simulator.nim index 46451c16..9e4f829c 100644 --- a/exercises/practice/robot-simulator/robot_simulator.nim +++ b/exercises/practice/robot-simulator/robot_simulator.nim @@ -4,8 +4,8 @@ type Robot* = object direction*: Compass - xPos*: int - yPos*: int + x*: int + y*: int proc initRobot*(facing = North, x = 0, y = 0): Robot = discard diff --git a/exercises/practice/robot-simulator/test_robot_simulator.nim b/exercises/practice/robot-simulator/test_robot_simulator.nim index 0c55153b..7f94d468 100644 --- a/exercises/practice/robot-simulator/test_robot_simulator.nim +++ b/exercises/practice/robot-simulator/test_robot_simulator.nim @@ -9,100 +9,100 @@ suite "Robot_Simulator": test "Create robot -> at negative position facing south": var robot = initRobot(South, -1, -1) check robot.direction == South - check robot.xPos == -1 and robot.yPos == -1 + check robot.x == -1 and robot.y == -1 test "Rotating clockwise -> changes north to east": var robot = initRobot() robot.move("R") check robot.direction == East - check robot.xPos == 0 and robot.yPos == 0 + check robot.x == 0 and robot.y == 0 test "Rotating clockwise -> changes east to south": var robot = initRobot(East) robot.move("R") check robot.direction == South - check robot.xPos == 0 and robot.yPos == 0 + check robot.x == 0 and robot.y == 0 test "Rotating clockwise -> changes south to west": var robot = initRobot(South) robot.move("R") check robot.direction == West - check robot.xPos == 0 and robot.yPos == 0 + check robot.x == 0 and robot.y == 0 test "Rotating clockwise -> changes west to north": var robot = initRobot(West) robot.move("R") check robot.direction == North - check robot.xPos == 0 and robot.yPos == 0 + check robot.x == 0 and robot.y == 0 test "Rotating counter-clockwise -> changes north to west": var robot = initRobot() robot.move("L") check robot.direction == West - check robot.xPos == 0 and robot.yPos == 0 + check robot.x == 0 and robot.y == 0 test "Rotating counter-clockwise -> changes west to south": var robot = initRobot(West) robot.move("L") check robot.direction == South - check robot.xPos == 0 and robot.yPos == 0 + check robot.x == 0 and robot.y == 0 test "Rotating counter-clockwise -> changes south to east": var robot = initRobot(South) robot.move("L") check robot.direction == East - check robot.xPos == 0 and robot.yPos == 0 + check robot.x == 0 and robot.y == 0 test "Rotating counter-clockwise -> changes east to north": var robot = initRobot(East) robot.move("L") check robot.direction == North - check robot.xPos == 0 and robot.yPos == 0 + check robot.x == 0 and robot.y == 0 test "Moving forward one -> facing north increments Y": var robot = initRobot() robot.move("A") check robot.direction == North - check robot.xPos == 0 and robot.yPos == 1 + check robot.x == 0 and robot.y == 1 test "Moving forward one -> facing south decrements Y": var robot = initRobot(South) robot.move("A") check robot.direction == South - check robot.xPos == 0 and robot.yPos == -1 + check robot.x == 0 and robot.y == -1 test "Moving forward one -> facing east increments X": var robot = initRobot(East) robot.move("A") check robot.direction == East - check robot.xPos == 1 and robot.yPos == 0 + check robot.x == 1 and robot.y == 0 test "Moving forward one -> facing west decrements X": var robot = initRobot(West) robot.move("A") check robot.direction == West - check robot.xPos == -1 and robot.yPos == 0 + check robot.x == -1 and robot.y == 0 test "Follow series of instructions -> moving east and north from README": var robot = initRobot(North, 7, 3) robot.move("RAALAL") check robot.direction == West - check robot.xPos == 9 and robot.yPos == 4 + check robot.x == 9 and robot.y == 4 test "Follow series of instructions -> moving west and north": var robot = initRobot() robot.move("LAAARALA") check robot.direction == West - check robot.xPos == -4 and robot.yPos == 1 + check robot.x == -4 and robot.y == 1 test "Follow series of instructions -> moving west and south": var robot = initRobot(East, 2, -7) robot.move("RRAAAAALA") check robot.direction == South - check robot.xPos == -3 and robot.yPos == -8 + check robot.x == -3 and robot.y == -8 test "Follow series of instructions -> moving east and north": var robot = initRobot(South, 8, 4) robot.move("LAAARRRALLLL") check robot.direction == North - check robot.xPos == 11 and robot.yPos == 5 + check robot.x == 11 and robot.y == 5 From cb640f21c89273bd43f449cfbf0c95381fecb502 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:06 +0100 Subject: [PATCH 07/17] exercises(robot-simulator): tests: split position checks Improve error messages on test failure. --- .../robot-simulator/test_robot_simulator.nim | 51 ++++++++++++------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/exercises/practice/robot-simulator/test_robot_simulator.nim b/exercises/practice/robot-simulator/test_robot_simulator.nim index 7f94d468..3d6197f2 100644 --- a/exercises/practice/robot-simulator/test_robot_simulator.nim +++ b/exercises/practice/robot-simulator/test_robot_simulator.nim @@ -9,100 +9,117 @@ suite "Robot_Simulator": test "Create robot -> at negative position facing south": var robot = initRobot(South, -1, -1) check robot.direction == South - check robot.x == -1 and robot.y == -1 + check robot.x == -1 + check robot.y == -1 test "Rotating clockwise -> changes north to east": var robot = initRobot() robot.move("R") check robot.direction == East - check robot.x == 0 and robot.y == 0 + check robot.x == 0 + check robot.y == 0 test "Rotating clockwise -> changes east to south": var robot = initRobot(East) robot.move("R") check robot.direction == South - check robot.x == 0 and robot.y == 0 + check robot.x == 0 + check robot.y == 0 test "Rotating clockwise -> changes south to west": var robot = initRobot(South) robot.move("R") check robot.direction == West - check robot.x == 0 and robot.y == 0 + check robot.x == 0 + check robot.y == 0 test "Rotating clockwise -> changes west to north": var robot = initRobot(West) robot.move("R") check robot.direction == North - check robot.x == 0 and robot.y == 0 + check robot.x == 0 + check robot.y == 0 test "Rotating counter-clockwise -> changes north to west": var robot = initRobot() robot.move("L") check robot.direction == West - check robot.x == 0 and robot.y == 0 + check robot.x == 0 + check robot.y == 0 test "Rotating counter-clockwise -> changes west to south": var robot = initRobot(West) robot.move("L") check robot.direction == South - check robot.x == 0 and robot.y == 0 + check robot.x == 0 + check robot.y == 0 test "Rotating counter-clockwise -> changes south to east": var robot = initRobot(South) robot.move("L") check robot.direction == East - check robot.x == 0 and robot.y == 0 + check robot.x == 0 + check robot.y == 0 test "Rotating counter-clockwise -> changes east to north": var robot = initRobot(East) robot.move("L") check robot.direction == North - check robot.x == 0 and robot.y == 0 + check robot.x == 0 + check robot.y == 0 test "Moving forward one -> facing north increments Y": var robot = initRobot() robot.move("A") check robot.direction == North - check robot.x == 0 and robot.y == 1 + check robot.x == 0 + check robot.y == 1 test "Moving forward one -> facing south decrements Y": var robot = initRobot(South) robot.move("A") check robot.direction == South - check robot.x == 0 and robot.y == -1 + check robot.x == 0 + check robot.y == -1 test "Moving forward one -> facing east increments X": var robot = initRobot(East) robot.move("A") check robot.direction == East - check robot.x == 1 and robot.y == 0 + check robot.x == 1 + check robot.y == 0 test "Moving forward one -> facing west decrements X": var robot = initRobot(West) robot.move("A") check robot.direction == West - check robot.x == -1 and robot.y == 0 + check robot.x == -1 + check robot.y == 0 test "Follow series of instructions -> moving east and north from README": var robot = initRobot(North, 7, 3) robot.move("RAALAL") check robot.direction == West - check robot.x == 9 and robot.y == 4 + check robot.x == 9 + check robot.y == 4 test "Follow series of instructions -> moving west and north": var robot = initRobot() robot.move("LAAARALA") check robot.direction == West - check robot.x == -4 and robot.y == 1 + check robot.x == -4 + check robot.y == 1 test "Follow series of instructions -> moving west and south": var robot = initRobot(East, 2, -7) robot.move("RRAAAAALA") check robot.direction == South - check robot.x == -3 and robot.y == -8 + check robot.x == -3 + check robot.y == -8 test "Follow series of instructions -> moving east and north": var robot = initRobot(South, 8, 4) robot.move("LAAARRRALLLL") check robot.direction == North - check robot.x == 11 and robot.y == 5 + check robot.x == 11 + check robot.y == 5 From 005482dfee618bc99221960f416d96cccd98f9ef Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:07 +0100 Subject: [PATCH 08/17] exercises(robot-simulator): example: avoid `continue` --- .../robot-simulator/.meta/example.nim | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index ce3a2e89..071852f2 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -26,14 +26,16 @@ func left(dir: Compass): Compass = func move*(robot: var Robot, instructions: string) = for c in instructions: - if c == 'L': + case c + of 'L': robot.direction = robot.direction.left - if c == 'R': + of 'R': robot.direction = robot.direction.right - if c != 'A': - continue - case robot.direction: - of North: inc(robot.y) - of South: dec(robot.y) - of East: inc(robot.x) - of West: dec(robot.x) + of 'A': + case robot.direction: + of North: inc(robot.y) + of South: dec(robot.y) + of East: inc(robot.x) + of West: dec(robot.x) + else: + discard From 270555927f0278c52a7d21aaf8ecbea320ca3e1d Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:08 +0100 Subject: [PATCH 09/17] exercises(robot-simulator): example: remove parens for `inc` and `dec` --- exercises/practice/robot-simulator/.meta/example.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index 071852f2..8e57052c 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -33,9 +33,9 @@ func move*(robot: var Robot, instructions: string) = robot.direction = robot.direction.right of 'A': case robot.direction: - of North: inc(robot.y) - of South: dec(robot.y) - of East: inc(robot.x) - of West: dec(robot.x) + of North: inc robot.y + of South: dec robot.y + of East: inc robot.x + of West: dec robot.x else: discard From 757f83e3a776f63d6c64b061dbe6d2121e4a9359 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:09 +0100 Subject: [PATCH 10/17] exercises(robot-simulator): example: add parens --- exercises/practice/robot-simulator/.meta/example.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index 8e57052c..c1d1440e 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -28,9 +28,9 @@ func move*(robot: var Robot, instructions: string) = for c in instructions: case c of 'L': - robot.direction = robot.direction.left + robot.direction = robot.direction.left() of 'R': - robot.direction = robot.direction.right + robot.direction = robot.direction.right() of 'A': case robot.direction: of North: inc robot.y From e2ade8d73c2933e4815f3b52f7c32e93c13e169e Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:10 +0100 Subject: [PATCH 11/17] exercises(robot-simulator): example: switch right and left procs Make the order match their usage in `move`. --- .../practice/robot-simulator/.meta/example.nim | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index c1d1440e..28d9fe16 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -10,13 +10,6 @@ type func initRobot*(facing = North, x = 0, y = 0): Robot = Robot(direction: facing, x: x, y: y) -func right(dir: Compass): Compass = - case dir: - of North: East - of East: South - of South: West - of West: North - func left(dir: Compass): Compass = case dir: of North: West @@ -24,6 +17,13 @@ func left(dir: Compass): Compass = of South: East of West: South +func right(dir: Compass): Compass = + case dir: + of North: East + of East: South + of South: West + of West: North + func move*(robot: var Robot, instructions: string) = for c in instructions: case c From 2d60a5513ec519002e4dfd4231cd9d32ea4ba399 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:11 +0100 Subject: [PATCH 12/17] exercises(robot-simulator): example: bikeshed left and right procs --- exercises/practice/robot-simulator/.meta/example.nim | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index 28d9fe16..ab7e9e5e 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -11,18 +11,10 @@ func initRobot*(facing = North, x = 0, y = 0): Robot = Robot(direction: facing, x: x, y: y) func left(dir: Compass): Compass = - case dir: - of North: West - of East: North - of South: East - of West: South + if dir == Compass.low: Compass.high else: pred(dir) func right(dir: Compass): Compass = - case dir: - of North: East - of East: South - of South: West - of West: North + if dir == Compass.high: Compass.low else: succ(dir) func move*(robot: var Robot, instructions: string) = for c in instructions: From 2ca10abac0b82e374ed9cb811756ac9cd371dc86 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:12 +0100 Subject: [PATCH 13/17] exercises(robot-simulator): example: inline left and right --- exercises/practice/robot-simulator/.meta/example.nim | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/exercises/practice/robot-simulator/.meta/example.nim b/exercises/practice/robot-simulator/.meta/example.nim index ab7e9e5e..d230dab3 100644 --- a/exercises/practice/robot-simulator/.meta/example.nim +++ b/exercises/practice/robot-simulator/.meta/example.nim @@ -10,19 +10,15 @@ type func initRobot*(facing = North, x = 0, y = 0): Robot = Robot(direction: facing, x: x, y: y) -func left(dir: Compass): Compass = - if dir == Compass.low: Compass.high else: pred(dir) - -func right(dir: Compass): Compass = - if dir == Compass.high: Compass.low else: succ(dir) - func move*(robot: var Robot, instructions: string) = for c in instructions: case c of 'L': - robot.direction = robot.direction.left() + robot.direction = + if robot.direction == Compass.low: Compass.high else: pred(robot.direction) of 'R': - robot.direction = robot.direction.right() + robot.direction = + if robot.direction == Compass.high: Compass.low else: succ(robot.direction) of 'A': case robot.direction: of North: inc robot.y From 0a8631afae7d5b212a9c53bc7e75676cfde8842f Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:13 +0100 Subject: [PATCH 14/17] exercises(robot-simulator): tests: replace arrow with colon --- .../robot-simulator/test_robot_simulator.nim | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/exercises/practice/robot-simulator/test_robot_simulator.nim b/exercises/practice/robot-simulator/test_robot_simulator.nim index 3d6197f2..280c1f9b 100644 --- a/exercises/practice/robot-simulator/test_robot_simulator.nim +++ b/exercises/practice/robot-simulator/test_robot_simulator.nim @@ -2,122 +2,122 @@ import unittest import robot_simulator suite "Robot_Simulator": - test "Create robot -> at origin facing north": + test "Create robot: at origin facing north": var robot = initRobot() check robot.direction == North - test "Create robot -> at negative position facing south": + test "Create robot: at negative position facing south": var robot = initRobot(South, -1, -1) check robot.direction == South check robot.x == -1 check robot.y == -1 - test "Rotating clockwise -> changes north to east": + test "Rotating clockwise: changes north to east": var robot = initRobot() robot.move("R") check robot.direction == East check robot.x == 0 check robot.y == 0 - test "Rotating clockwise -> changes east to south": + test "Rotating clockwise: changes east to south": var robot = initRobot(East) robot.move("R") check robot.direction == South check robot.x == 0 check robot.y == 0 - test "Rotating clockwise -> changes south to west": + test "Rotating clockwise: changes south to west": var robot = initRobot(South) robot.move("R") check robot.direction == West check robot.x == 0 check robot.y == 0 - test "Rotating clockwise -> changes west to north": + test "Rotating clockwise: changes west to north": var robot = initRobot(West) robot.move("R") check robot.direction == North check robot.x == 0 check robot.y == 0 - test "Rotating counter-clockwise -> changes north to west": + test "Rotating counter-clockwise: changes north to west": var robot = initRobot() robot.move("L") check robot.direction == West check robot.x == 0 check robot.y == 0 - test "Rotating counter-clockwise -> changes west to south": + test "Rotating counter-clockwise: changes west to south": var robot = initRobot(West) robot.move("L") check robot.direction == South check robot.x == 0 check robot.y == 0 - test "Rotating counter-clockwise -> changes south to east": + test "Rotating counter-clockwise: changes south to east": var robot = initRobot(South) robot.move("L") check robot.direction == East check robot.x == 0 check robot.y == 0 - test "Rotating counter-clockwise -> changes east to north": + test "Rotating counter-clockwise: changes east to north": var robot = initRobot(East) robot.move("L") check robot.direction == North check robot.x == 0 check robot.y == 0 - test "Moving forward one -> facing north increments Y": + test "Moving forward one: facing north increments Y": var robot = initRobot() robot.move("A") check robot.direction == North check robot.x == 0 check robot.y == 1 - test "Moving forward one -> facing south decrements Y": + test "Moving forward one: facing south decrements Y": var robot = initRobot(South) robot.move("A") check robot.direction == South check robot.x == 0 check robot.y == -1 - test "Moving forward one -> facing east increments X": + test "Moving forward one: facing east increments X": var robot = initRobot(East) robot.move("A") check robot.direction == East check robot.x == 1 check robot.y == 0 - test "Moving forward one -> facing west decrements X": + test "Moving forward one: facing west decrements X": var robot = initRobot(West) robot.move("A") check robot.direction == West check robot.x == -1 check robot.y == 0 - test "Follow series of instructions -> moving east and north from README": + test "Follow series of instructions: moving east and north from README": var robot = initRobot(North, 7, 3) robot.move("RAALAL") check robot.direction == West check robot.x == 9 check robot.y == 4 - test "Follow series of instructions -> moving west and north": + test "Follow series of instructions: moving west and north": var robot = initRobot() robot.move("LAAARALA") check robot.direction == West check robot.x == -4 check robot.y == 1 - test "Follow series of instructions -> moving west and south": + test "Follow series of instructions: moving west and south": var robot = initRobot(East, 2, -7) robot.move("RRAAAAALA") check robot.direction == South check robot.x == -3 check robot.y == -8 - test "Follow series of instructions -> moving east and north": + test "Follow series of instructions: moving east and north": var robot = initRobot(South, 8, 4) robot.move("LAAARRRALLLL") check robot.direction == North From 6cea53132b7aa775bce04cdde7ed21dc09c93726 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:14 +0100 Subject: [PATCH 15/17] exercises(robot-simulator): tests: lowercase test names Be consistent with the other test files on the track. --- .../robot-simulator/test_robot_simulator.nim | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/exercises/practice/robot-simulator/test_robot_simulator.nim b/exercises/practice/robot-simulator/test_robot_simulator.nim index 280c1f9b..d29809cd 100644 --- a/exercises/practice/robot-simulator/test_robot_simulator.nim +++ b/exercises/practice/robot-simulator/test_robot_simulator.nim @@ -2,94 +2,94 @@ import unittest import robot_simulator suite "Robot_Simulator": - test "Create robot: at origin facing north": + test "create robot: at origin facing north": var robot = initRobot() check robot.direction == North - test "Create robot: at negative position facing south": + test "create robot: at negative position facing south": var robot = initRobot(South, -1, -1) check robot.direction == South check robot.x == -1 check robot.y == -1 - test "Rotating clockwise: changes north to east": + test "rotating clockwise: changes north to east": var robot = initRobot() robot.move("R") check robot.direction == East check robot.x == 0 check robot.y == 0 - test "Rotating clockwise: changes east to south": + test "rotating clockwise: changes east to south": var robot = initRobot(East) robot.move("R") check robot.direction == South check robot.x == 0 check robot.y == 0 - test "Rotating clockwise: changes south to west": + test "rotating clockwise: changes south to west": var robot = initRobot(South) robot.move("R") check robot.direction == West check robot.x == 0 check robot.y == 0 - test "Rotating clockwise: changes west to north": + test "rotating clockwise: changes west to north": var robot = initRobot(West) robot.move("R") check robot.direction == North check robot.x == 0 check robot.y == 0 - test "Rotating counter-clockwise: changes north to west": + test "rotating counter-clockwise: changes north to west": var robot = initRobot() robot.move("L") check robot.direction == West check robot.x == 0 check robot.y == 0 - test "Rotating counter-clockwise: changes west to south": + test "rotating counter-clockwise: changes west to south": var robot = initRobot(West) robot.move("L") check robot.direction == South check robot.x == 0 check robot.y == 0 - test "Rotating counter-clockwise: changes south to east": + test "rotating counter-clockwise: changes south to east": var robot = initRobot(South) robot.move("L") check robot.direction == East check robot.x == 0 check robot.y == 0 - test "Rotating counter-clockwise: changes east to north": + test "rotating counter-clockwise: changes east to north": var robot = initRobot(East) robot.move("L") check robot.direction == North check robot.x == 0 check robot.y == 0 - test "Moving forward one: facing north increments Y": + test "moving forward one: facing north increments Y": var robot = initRobot() robot.move("A") check robot.direction == North check robot.x == 0 check robot.y == 1 - test "Moving forward one: facing south decrements Y": + test "moving forward one: facing south decrements Y": var robot = initRobot(South) robot.move("A") check robot.direction == South check robot.x == 0 check robot.y == -1 - test "Moving forward one: facing east increments X": + test "moving forward one: facing east increments X": var robot = initRobot(East) robot.move("A") check robot.direction == East check robot.x == 1 check robot.y == 0 - test "Moving forward one: facing west decrements X": + test "moving forward one: facing west decrements X": var robot = initRobot(West) robot.move("A") check robot.direction == West From 21c99622d6253615b6e840c4b3659c79a0eb902e Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:15 +0100 Subject: [PATCH 16/17] exercises(robot-simulator): tests: remove _ from suite name The other suite names are like this. --- exercises/practice/robot-simulator/test_robot_simulator.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/robot-simulator/test_robot_simulator.nim b/exercises/practice/robot-simulator/test_robot_simulator.nim index d29809cd..84b3e45b 100644 --- a/exercises/practice/robot-simulator/test_robot_simulator.nim +++ b/exercises/practice/robot-simulator/test_robot_simulator.nim @@ -1,7 +1,7 @@ import unittest import robot_simulator -suite "Robot_Simulator": +suite "Robot Simulator": test "create robot: at origin facing north": var robot = initRobot() check robot.direction == North From 19119bcbf66066ce0626a1189c38272e2255ca83 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:15:16 +0100 Subject: [PATCH 17/17] exercises(robot-simulator): config: add authors/contributors --- exercises/practice/robot-simulator/.meta/config.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/exercises/practice/robot-simulator/.meta/config.json b/exercises/practice/robot-simulator/.meta/config.json index ce4d43e7..9dde3511 100644 --- a/exercises/practice/robot-simulator/.meta/config.json +++ b/exercises/practice/robot-simulator/.meta/config.json @@ -1,5 +1,10 @@ { - "authors": [], + "authors": [ + "ilmanzo" + ], + "contributors": [ + "ee7" + ], "files": { "solution": [ "robot_simulator.nim"