Skip to content

Commit

Permalink
Add robot-simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Jun 6, 2024
1 parent b98c42b commit 0ea026c
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
"uuid": "beae3683-46fc-4d4d-bacf-e3bd453d9c17",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "run-length-encoding",
"name": "Run-Length Encoding",
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/robot-simulator/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 18 additions & 0 deletions exercises/practice/robot-simulator/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"src/robot-simulator.art"
],
"test": [
"tests/test-robot-simulator.art"
],
"example": [
".meta/src/example.art"
]
},
"blurb": "Write a robot simulator.",
"source": "Inspired by an interview question at a famous company."
}
36 changes: 36 additions & 0 deletions exercises/practice/robot-simulator/.meta/src/example.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
directions: ["north" "east" "south" "west"]

steps: #[
north: @[0 1]
east: @[1 0]
south: @[0 neg 1]
west: @[neg 1 0]
]


define :robot [
init: constructor [x :integer y :integer direction :string]

simulate: method [commands] [
loop commands 'command [
case [command]
when? [= 'L'] -> this\turn "left"
when? [= 'R'] -> this\turn "right"
when? [= 'A'] -> this\advance
]
]

turn: method [direction] [
case [direction]
when? [= "left"]
-> this\direction: directions\[(add 1 mod add 2 (index directions this\direction)) 4]
when? [= "right"]
-> this\direction: directions\[mod (add 1 index directions this\direction) 4]
]

advance: method [] [
this\x: add this\x first steps\[this\direction]
this\y: add this\y last steps\[this\direction]
]
]

66 changes: 66 additions & 0 deletions exercises/practice/robot-simulator/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions exercises/practice/robot-simulator/src/robot-simulator.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
define :robot [
init: constructor [x :integer y :integer direction :string]

simulate: method [commands] [
panic "Please implement the simulate method"
]
]

3 changes: 3 additions & 0 deletions exercises/practice/robot-simulator/tester.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {unitt}!

runTests.failFast findTests "tests"
174 changes: 174 additions & 0 deletions exercises/practice/robot-simulator/tests/test-robot-simulator.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import {unitt}!
import {src/robot-simulator}!


suite "Robot Simulator" [
suite "Robot Simulator - Rotating clockwise" [
test "rotating clockwise changes north to east" [
r: to :robot @[0 0 "north"]
r\simulate "R"
expected: to :robot @[0 0 "east"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "rotating clockwise changes east to south" [
r: to :robot @[0 0 "east"]
r\simulate "R"
expected: to :robot @[0 0 "south"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "rotating clockwise changes south to west" [
r: to :robot @[0 0 "south"]
r\simulate "R"
expected: to :robot @[0 0 "west"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "rotating clockwise changes west to north" [
r: to :robot @[0 0 "west"]
r\simulate "R"
expected: to :robot @[0 0 "north"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]
]

suite "Robot Simulator - Rotating counterclockwise" [
test.skip "rotating counterclockwise changes north to west" [
r: to :robot @[0 0 "north"]
r\simulate "L"
expected: to :robot @[0 0 "west"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "rotating counterclockwise changes west to south" [
r: to :robot @[0 0 "west"]
r\simulate "L"
expected: to :robot @[0 0 "south"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "rotating counterclockwise changes south to east" [
r: to :robot @[0 0 "south"]
r\simulate "L"
expected: to :robot @[0 0 "east"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "rotating counterclockwise changes east to north" [
r: to :robot @[0 0 "east"]
r\simulate "L"
expected: to :robot @[0 0 "north"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]
]

suite "Robot Simulator - Moving forward" [
test.skip "Moving forward facing north increments Y" [
r: to :robot @[0 0 "north"]
r\simulate "A"
expected: to :robot @[0 1 "north"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "Moving forward facing south decrements Y" [
r: to :robot @[0 0 "south"]
r\simulate "A"
expected: to :robot @[0 neg 1 "south"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "Moving forward facing east increments X" [
r: to :robot @[0 0 "east"]
r\simulate "A"
expected: to :robot @[1 0 "east"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "Moving forward facing west decrements X" [
r: to :robot @[0 0 "west"]
r\simulate "A"
expected: to :robot @[neg 1 0 "west"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]
]

suite "Robot Simulator - Follow series of instructions" [
test.skip "moving east and north from README" [
r: to :robot @[7 3 "north"]
r\simulate "RAALAL"
expected: to :robot @[9 4 "west"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "moving west and north" [
r: to :robot @[0 0 "north"]
r\simulate "LAAARALA"
expected: to :robot @[neg 4 1 "west"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "moving west and south" [
r: to :robot [2 neg 7 "east"]
r\simulate "RRAAAAALA"
expected: to :robot @[neg 3 neg 8 "south"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]

test.skip "moving east and north" [
r: to :robot [8 4 "south"]
r\simulate "LAAARRRALLLL"
expected: to :robot @[11 5 "north"]

assert -> expected\x = r\x
assert -> expected\y = r\y
assert -> expected\direction = r\direction
]
]
]

0 comments on commit 0ea026c

Please sign in to comment.