Skip to content

Commit 55094d1

Browse files
authored
new exercise: robot simulator (#145)
1 parent ed917a5 commit 55094d1

14 files changed

+8969
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@
335335
"prerequisites": [],
336336
"difficulty": 6
337337
},
338+
{
339+
"slug": "robot-simulator",
340+
"name": "Robot Simulator",
341+
"uuid": "af31c3d6-95e2-4817-aaea-4bac4e71e647",
342+
"practices": [],
343+
"prerequisites": [],
344+
"difficulty": 5
345+
},
338346
{
339347
"slug": "circular-buffer",
340348
"name": "Circular Buffer",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Instruction append
2+
3+
The direction the robot is facing will be expressed as one of the following numbers:
4+
5+
- North = 0
6+
- East = 1
7+
- South = 2
8+
- West = 3
9+
10+
When the input is invalid, return a direction of -1 (the other values will be ignored).
11+
12+
## Reserved Memory
13+
14+
The buffer for the input string uses bytes 64-319 of linear memory.
15+
16+
The input string can be modified if desired.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Instructions
2+
3+
Write a robot simulator.
4+
5+
A robot factory's test facility needs a program to verify robot movements.
6+
7+
The robots have three possible movements:
8+
9+
- turn right
10+
- turn left
11+
- advance
12+
13+
Robots are placed on a hypothetical infinite grid, facing a particular direction (north, east, south, or west) at a set of {x,y} coordinates,
14+
e.g., {3,8}, with coordinates increasing to the north and east.
15+
16+
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.
17+
18+
- The letter-string "RAALAL" means:
19+
- Turn right
20+
- Advance twice
21+
- Turn left
22+
- Advance once
23+
- Turn left yet again
24+
- Say a robot starts at {7, 3} facing north.
25+
Then running this stream of instructions should leave it at {9, 4} facing west.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"root": true,
3+
"extends": "@exercism/eslint-config-javascript",
4+
"env": {
5+
"jest": true
6+
},
7+
"overrides": [
8+
{
9+
"files": [
10+
"*.spec.js"
11+
],
12+
"excludedFiles": [
13+
"custom.spec.js"
14+
],
15+
"extends": "@exercism/eslint-config-javascript/maintainers"
16+
}
17+
]
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"authors": [
3+
"atk"
4+
],
5+
"files": {
6+
"solution": [
7+
"robot-simulator.wat"
8+
],
9+
"test": [
10+
"robot-simulator.spec.js"
11+
],
12+
"example": [
13+
".meta/proof.ci.wat"
14+
],
15+
"invalidator": [
16+
"package.json"
17+
]
18+
},
19+
"blurb": "Write a robot simulator.",
20+
"source": "Inspired by an interview question at a famous company.",
21+
"custom": {
22+
"version.tests.compatibility": "jest-27",
23+
"flag.tests.task-per-describe": false,
24+
"flag.tests.may-run-long": false,
25+
"flag.tests.includes-optional": false
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(module
2+
(memory (export "mem") 1)
3+
4+
(global $Advance i32 (i32.const 65))
5+
(global $Left i32 (i32.const 76))
6+
(global $Right i32 (i32.const 82))
7+
8+
;;
9+
;; evaluate robot placement after running instructions
10+
;;
11+
;; @param {i32} $direction - 0 = north, 1 = east, 2 = south, 3 = west
12+
;; @param {i32} $x - horizontal position
13+
;; @param {i32} $y - vertical position
14+
;; @param {i32} $offset - the offset of the instructions in linear memory
15+
;; @param {i32} $length - the length of the instructions in linear memory
16+
;;
17+
;; @returns {(i32,i32,i32)} direction, x and y after the instructions
18+
;; are executed; direction is -1 on error
19+
;;
20+
(func (export "evaluate") (param $direction i32) (param $x i32) (param $y i32)
21+
(param $offset i32) (param $length i32) (result i32 i32 i32)
22+
(local $pos i32)
23+
(local $instruction i32)
24+
(loop $instructions
25+
(local.set $instruction (i32.load8_u
26+
(i32.add (local.get $offset) (local.get $pos))))
27+
(if (i32.eq (local.get $instruction) (global.get $Advance)) (then
28+
(local.set $x (i32.add (local.get $x) (i32.rem_s
29+
(i32.sub (i32.const 2) (local.get $direction)) (i32.const 2))))
30+
(local.set $y (i32.add (local.get $y) (i32.rem_s
31+
(i32.sub (i32.const 1) (local.get $direction)) (i32.const 2)))))
32+
(else (if (i32.eq (local.get $instruction) (global.get $Left)) (then
33+
(local.set $direction (i32.rem_u (i32.add (local.get $direction)
34+
(i32.const 3)) (i32.const 4))))
35+
(else (if (i32.eq (local.get $instruction) (global.get $Right)) (then ;; R
36+
(local.set $direction (i32.rem_u (i32.add (local.get $direction)
37+
(i32.const 1)) (i32.const 4))))
38+
(else (return (i32.const -1) (local.get $x) (local.get $y))))))))
39+
(local.set $pos (i32.add (local.get $pos) (i32.const 1)))
40+
(br_if $instructions (i32.lt_u (local.get $pos) (local.get $length))))
41+
(local.get $direction) (local.get $x) (local.get $y)
42+
)
43+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[c557c16d-26c1-4e06-827c-f6602cd0785c]
13+
description = "Create robot -> at origin facing north"
14+
15+
[bf0dffce-f11c-4cdb-8a5e-2c89d8a5a67d]
16+
description = "Create robot -> at negative position facing south"
17+
18+
[8cbd0086-6392-4680-b9b9-73cf491e67e5]
19+
description = "Rotating clockwise -> changes north to east"
20+
21+
[8abc87fc-eab2-4276-93b7-9c009e866ba1]
22+
description = "Rotating clockwise -> changes east to south"
23+
24+
[3cfe1b85-bbf2-4bae-b54d-d73e7e93617a]
25+
description = "Rotating clockwise -> changes south to west"
26+
27+
[5ea9fb99-3f2c-47bd-86f7-46b7d8c3c716]
28+
description = "Rotating clockwise -> changes west to north"
29+
30+
[fa0c40f5-6ba3-443d-a4b3-58cbd6cb8d63]
31+
description = "Rotating counter-clockwise -> changes north to west"
32+
33+
[da33d734-831f-445c-9907-d66d7d2a92e2]
34+
description = "Rotating counter-clockwise -> changes west to south"
35+
36+
[bd1ca4b9-4548-45f4-b32e-900fc7c19389]
37+
description = "Rotating counter-clockwise -> changes south to east"
38+
39+
[2de27b67-a25c-4b59-9883-bc03b1b55bba]
40+
description = "Rotating counter-clockwise -> changes east to north"
41+
42+
[f0dc2388-cddc-4f83-9bed-bcf46b8fc7b8]
43+
description = "Moving forward one -> facing north increments Y"
44+
45+
[2786cf80-5bbf-44b0-9503-a89a9c5789da]
46+
description = "Moving forward one -> facing south decrements Y"
47+
48+
[84bf3c8c-241f-434d-883d-69817dbd6a48]
49+
description = "Moving forward one -> facing east increments X"
50+
51+
[bb69c4a7-3bbf-4f64-b415-666fa72d7b04]
52+
description = "Moving forward one -> facing west decrements X"
53+
54+
[e34ac672-4ed4-4be3-a0b8-d9af259cbaa1]
55+
description = "Follow series of instructions -> moving east and north from README"
56+
57+
[f30e4955-4b47-4aa3-8b39-ae98cfbd515b]
58+
description = "Follow series of instructions -> moving west and north"
59+
60+
[3e466bf6-20ab-4d79-8b51-264165182fca]
61+
description = "Follow series of instructions -> moving west and south"
62+
63+
[41f0bb96-c617-4e6b-acff-a4b279d44514]
64+
description = "Follow series of instructions -> moving east and north"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
presets: ["@exercism/babel-preset-javascript"],
3+
plugins: [],
4+
};

0 commit comments

Comments
 (0)