diff --git a/config.json b/config.json index 3166d2a..6c35fea 100644 --- a/config.json +++ b/config.json @@ -141,6 +141,14 @@ "practices": [], "prerequisites": [], "difficulty": 3 + }, + { + "slug": "robot-simulator", + "name": "Robot Simulator", + "uuid": "26025a1c-21b8-428b-80a1-6068fbdf7834", + "practices": [], + "prerequisites": [], + "difficulty": 3 } ] }, 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..418cca4 --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": [ + "quintuple-mallard" + ], + "files": { + "solution": [ + "robot-simulator.nu" + ], + "test": [ + "tests.nu" + ], + "example": [ + ".meta/example.nu" + ] + }, + "blurb": "Write a robot simulator.", + "source": "Inspired by an interview question at a famous company." +} diff --git a/exercises/practice/robot-simulator/.meta/example.nu b/exercises/practice/robot-simulator/.meta/example.nu new file mode 100644 index 0000000..00eda89 --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/example.nu @@ -0,0 +1,27 @@ +const directions: list = [north east south west] +const offsets: list> = [[0 1] [1 0] [0 -1] [-1 0]] +def indexof [item: any]: list -> number { + $in | enumerate | where $it.item == $item | get index.0 +} +def advance [robot: record, direction: string>] { + let change = $offsets | get ($directions | indexof $robot.direction | into cell-path) + $robot | update position ({x: ($robot.position.x + $change.0), y: ($robot.position.y + $change.1)}) +} +def turnr [robot: record, direction: string>] { + let idx = $directions | indexof $robot.direction + $robot | update direction ($directions | get (($idx + 1) mod 4 | into cell-path)) +} +def turnl [robot: record, direction: string>] { + let idx = $directions | indexof $robot.direction + $robot | update direction ($directions | get (($idx - 1) mod 4 | into cell-path)) +} +export def run [instructions: string]: record, direction: string> -> record, direction: string> { + let robot = $in + $instructions | split chars | reduce --fold $robot {|c, robot| + match $c { + "A" => (advance $robot), + "R" => (turnr $robot), + "L" => (turnl $robot), + } + } +} diff --git a/exercises/practice/robot-simulator/.meta/tests.toml b/exercises/practice/robot-simulator/.meta/tests.toml new file mode 100644 index 0000000..f2e52b1 --- /dev/null +++ b/exercises/practice/robot-simulator/.meta/tests.toml @@ -0,0 +1,66 @@ +# 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" +include = false + +[bf0dffce-f11c-4cdb-8a5e-2c89d8a5a67d] +description = "Create robot -> at negative position facing south" +include = false + +[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.nu b/exercises/practice/robot-simulator/robot-simulator.nu new file mode 100644 index 0000000..a45db0d --- /dev/null +++ b/exercises/practice/robot-simulator/robot-simulator.nu @@ -0,0 +1,4 @@ +export def run [instructions: string]: record, direction: string> -> record, direction: string> { + error make {msg: "Remove this line and implement grep"} + {position: {x: 0, y: 0}, direction: north} +} diff --git a/exercises/practice/robot-simulator/tests.nu b/exercises/practice/robot-simulator/tests.nu new file mode 100644 index 0000000..441dc2b --- /dev/null +++ b/exercises/practice/robot-simulator/tests.nu @@ -0,0 +1,49 @@ +use robot-simulator.nu run +use std/assert +# changes north to east +assert equal ({position: {x: 0, y: 0}, direction: north} | run R) {position: {x: 0, y: 0}, direction: east} + +# changes east to south +assert equal ({position: {x: 0, y: 0}, direction: east} | run R) {position: {x: 0, y: 0}, direction: south} + +# changes south to west +assert equal ({position: {x: 0, y: 0}, direction: south} | run R) {position: {x: 0, y: 0}, direction: west} + +# changes west to north +assert equal ({position: {x: 0, y: 0}, direction: west} | run R) {position: {x: 0, y: 0}, direction: north} + +# changes north to west +assert equal ({position: {x: 0, y: 0}, direction: north} | run L) {position: {x: 0, y: 0}, direction: west} + +# changes west to south +assert equal ({position: {x: 0, y: 0}, direction: west} | run L) {position: {x: 0, y: 0}, direction: south} + +# changes south to east +assert equal ({position: {x: 0, y: 0}, direction: south} | run L) {position: {x: 0, y: 0}, direction: east} + +# changes east to north +assert equal ({position: {x: 0, y: 0}, direction: east} | run L) {position: {x: 0, y: 0}, direction: north} + +# facing north increments Y +assert equal ({position: {x: 0, y: 0}, direction: north} | run A) {position: {x: 0, y: 1}, direction: north} + +# facing south decrements Y +assert equal ({position: {x: 0, y: 0}, direction: south} | run A) {position: {x: 0, y: -1}, direction: south} + +# facing east increments X +assert equal ({position: {x: 0, y: 0}, direction: east} | run A) {position: {x: 1, y: 0}, direction: east} + +# facing west decrements X +assert equal ({position: {x: 0, y: 0}, direction: west} | run A) {position: {x: -1, y: 0}, direction: west} + +# moving east and north from README +assert equal ({position: {x: 7, y: 3}, direction: north} | run RAALAL) {position: {x: 9, y: 4}, direction: west} + +# moving west and north +assert equal ({position: {x: 0, y: 0}, direction: north} | run LAAARALA) {position: {x: -4, y: 1}, direction: west} + +# moving west and south +assert equal ({position: {x: 2, y: -7}, direction: east} | run RRAAAAALA) {position: {x: -3, y: -8}, direction: south} + +# moving east and north +assert equal ({position: {x: 8, y: 4}, direction: south} | run LAAARRRALLLL) {position: {x: 11, y: 5}, direction: north}