Skip to content

Commit 6f58059

Browse files
authored
Add practice exercise: robot-simulator (#202)
1 parent 775e36b commit 6f58059

File tree

11 files changed

+344
-0
lines changed

11 files changed

+344
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@
482482
"prerequisites": [],
483483
"difficulty": 8
484484
},
485+
{
486+
"slug": "robot-simulator",
487+
"name": "Robot Simulator",
488+
"uuid": "49dc9e30-e985-485b-b63f-6c2fd6bbfe23",
489+
"practices": [],
490+
"prerequisites": [],
491+
"difficulty": 8
492+
},
485493
{
486494
"slug": "roman-numerals",
487495
"name": "Roman Numerals",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SQLite-specific instructions
2+
3+
## JSON documentation
4+
5+
See [JSON Functions And Operators][json-docs] for SQLite JSON functions.
6+
7+
[json-docs]: https://www.sqlite.org/json1.html
Lines changed: 25 additions & 0 deletions
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.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"robot-simulator.sql"
8+
],
9+
"test": [
10+
"robot-simulator_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
]
15+
},
16+
"blurb": "Write a robot simulator.",
17+
"source": "Inspired by an interview question at a famous company."
18+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
UPDATE "robot-simulator"
2+
SET result = input
3+
WHERE property = 'create'
4+
;
5+
6+
DROP TABLE IF EXISTS directions;
7+
CREATE TABLE directions (
8+
direction TEXT NOT NULL,
9+
angle INTEGER NOT NULL,
10+
axis TEXT NOT NULL,
11+
incr INTEGER NOT NULL
12+
);
13+
INSERT INTO directions (direction, angle, axis, incr)
14+
VALUES ('north', 0, 'y', 1),
15+
('east' , 90, 'x', 1),
16+
('south', 180, 'y', -1),
17+
('west', 270, 'x', -1)
18+
;
19+
20+
DROP TABLE IF EXISTS tmp;
21+
CREATE TEMPORARY TABLE tmp (
22+
input TEXT PRIMARY KEY,
23+
output TEXT NOT NULL
24+
);
25+
INSERT INTO tmp
26+
SELECT input, JSON_SET(input, '$.instructions',
27+
(WITH RECURSIVE rcte (string, chr) AS (
28+
VALUES (JSON_EXTRACT(input, '$.instructions'), NULL)
29+
UNION ALL
30+
SELECT SUBSTR(string, 2), SUBSTR(string, 1, 1)
31+
FROM rcte
32+
WHERE string <> ''
33+
)
34+
SELECT JSON_GROUP_ARRAY(chr)
35+
FROM rcte
36+
WHERE chr NOTNULL
37+
)
38+
)
39+
FROM "robot-simulator"
40+
WHERE property = 'move'
41+
;
42+
43+
UPDATE "robot-simulator"
44+
SET result = (
45+
WITH RECURSIVE rcte (jobject, instructions, lr, dirmap) AS (
46+
VALUES (
47+
JSON_REMOVE(tmp.output, '$.instructions'),
48+
JSON_EXTRACT(tmp.output, '$.instructions'),
49+
'{"L": -90, "R": 90}',
50+
(SELECT JSON_GROUP_OBJECT(
51+
direction,
52+
JSON_OBJECT('axis', axis, 'incr', incr))
53+
FROM directions)
54+
)
55+
UNION ALL
56+
SELECT
57+
CASE
58+
WHEN JSON_EXTRACT(instructions, '$[0]') IN ('R', 'L')
59+
THEN JSON_SET(
60+
jobject,
61+
'$.direction',
62+
(SELECT direction
63+
FROM directions
64+
WHERE angle = (
65+
SELECT (360 + angle +
66+
JSON_EXTRACT(lr,
67+
PRINTF('$.%s',
68+
JSON_EXTRACT(instructions, '$[0]')))
69+
) % 360
70+
FROM directions
71+
WHERE direction = JSON_EXTRACT(jobject, '$.direction')))
72+
)
73+
WHEN JSON_EXTRACT(instructions, '$[0]') = 'A'
74+
THEN
75+
JSON_SET(
76+
jobject,
77+
PRINTF(
78+
'$.position.%s',
79+
JSON_EXTRACT(
80+
dirmap,
81+
PRINTF('$.%s.axis', JSON_EXTRACT(jobject, '$.direction'))
82+
)),
83+
JSON_EXTRACT(
84+
jobject,
85+
PRINTF(
86+
'$.position.%s',
87+
JSON_EXTRACT(
88+
dirmap,
89+
PRINTF('$.%s.axis', JSON_EXTRACT(jobject, '$.direction'))
90+
)
91+
)
92+
) +
93+
JSON_EXTRACT(
94+
dirmap, PRINTF('$.%s.incr', JSON_EXTRACT(jobject, '$.direction')))
95+
)
96+
ELSE jobject
97+
END,
98+
JSON_REMOVE(instructions, '$[0]'),
99+
lr,
100+
dirmap
101+
FROM rcte
102+
WHERE JSON_ARRAY_LENGTH(instructions) > 0
103+
)
104+
SELECT jobject
105+
FROM rcte
106+
WHERE JSON_ARRAY_LENGTH(instructions) = 0
107+
)
108+
FROM tmp
109+
WHERE "robot-simulator".input = tmp.input
110+
;
Lines changed: 64 additions & 0 deletions
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"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DROP TABLE IF EXISTS "robot-simulator";
2+
CREATE TABLE "robot-simulator" (
3+
property TEXT NOT NULL,
4+
input TEXT NOT NULL, -- json object
5+
result TEXT -- json object
6+
);
7+
8+
.mode csv
9+
.import ./data.csv "robot-simulator"
10+
11+
UPDATE "robot-simulator" SET result = NULL;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
DROP TABLE IF EXISTS tests;
2+
CREATE TABLE IF NOT EXISTS tests (
3+
-- uuid and description are taken from the test.toml file
4+
uuid TEXT PRIMARY KEY,
5+
description TEXT NOT NULL,
6+
-- The following section is needed by the online test-runner
7+
status TEXT DEFAULT 'fail',
8+
message TEXT,
9+
output TEXT,
10+
test_code TEXT,
11+
task_id INTEGER DEFAULT NULL,
12+
-- Here are columns for the actual tests
13+
property TEXT NOT NULL,
14+
input INTEGER NOT NULL, -- json object
15+
expected TEXT NOT NULL -- json object
16+
);
17+
18+
INSERT INTO tests (uuid, description, property, input, expected)
19+
VALUES
20+
('c557c16d-26c1-4e06-827c-f6602cd0785c', 'at origin facing north', 'create', '{"position":{"x":0,"y":0},"direction":"north"}', '{"position":{"x":0,"y":0},"direction":"north"}'),
21+
('bf0dffce-f11c-4cdb-8a5e-2c89d8a5a67d', 'at negative position facing south', 'create', '{"position":{"x":-1,"y":-1},"direction":"south"}', '{"position":{"x":-1,"y":-1},"direction":"south"}'),
22+
('8cbd0086-6392-4680-b9b9-73cf491e67e5', 'changes north to east', 'move', '{"position":{"x":0,"y":0},"direction":"north","instructions":"R"}', '{"position":{"x":0,"y":0},"direction":"east"}'),
23+
('8abc87fc-eab2-4276-93b7-9c009e866ba1', 'changes east to south', 'move', '{"position":{"x":0,"y":0},"direction":"east","instructions":"R"}', '{"position":{"x":0,"y":0},"direction":"south"}'),
24+
('3cfe1b85-bbf2-4bae-b54d-d73e7e93617a', 'changes south to west', 'move', '{"position":{"x":0,"y":0},"direction":"south","instructions":"R"}', '{"position":{"x":0,"y":0},"direction":"west"}'),
25+
('5ea9fb99-3f2c-47bd-86f7-46b7d8c3c716', 'changes west to north', 'move', '{"position":{"x":0,"y":0},"direction":"west","instructions":"R"}', '{"position":{"x":0,"y":0},"direction":"north"}'),
26+
('fa0c40f5-6ba3-443d-a4b3-58cbd6cb8d63', 'changes north to west', 'move', '{"position":{"x":0,"y":0},"direction":"north","instructions":"L"}', '{"position":{"x":0,"y":0},"direction":"west"}'),
27+
('da33d734-831f-445c-9907-d66d7d2a92e2', 'changes west to south', 'move', '{"position":{"x":0,"y":0},"direction":"west","instructions":"L"}', '{"position":{"x":0,"y":0},"direction":"south"}'),
28+
('bd1ca4b9-4548-45f4-b32e-900fc7c19389', 'changes south to east', 'move', '{"position":{"x":0,"y":0},"direction":"south","instructions":"L"}', '{"position":{"x":0,"y":0},"direction":"east"}'),
29+
('2de27b67-a25c-4b59-9883-bc03b1b55bba', 'changes east to north', 'move', '{"position":{"x":0,"y":0},"direction":"east","instructions":"L"}', '{"position":{"x":0,"y":0},"direction":"north"}'),
30+
('f0dc2388-cddc-4f83-9bed-bcf46b8fc7b8', 'facing north increments Y', 'move', '{"position":{"x":0,"y":0},"direction":"north","instructions":"A"}', '{"position":{"x":0,"y":1},"direction":"north"}'),
31+
('2786cf80-5bbf-44b0-9503-a89a9c5789da', 'facing south decrements Y', 'move', '{"position":{"x":0,"y":0},"direction":"south","instructions":"A"}', '{"position":{"x":0,"y":-1},"direction":"south"}'),
32+
('84bf3c8c-241f-434d-883d-69817dbd6a48', 'facing east increments X', 'move', '{"position":{"x":0,"y":0},"direction":"east","instructions":"A"}', '{"position":{"x":1,"y":0},"direction":"east"}'),
33+
('bb69c4a7-3bbf-4f64-b415-666fa72d7b04', 'facing west decrements X', 'move', '{"position":{"x":0,"y":0},"direction":"west","instructions":"A"}', '{"position":{"x":-1,"y":0},"direction":"west"}'),
34+
('e34ac672-4ed4-4be3-a0b8-d9af259cbaa1', 'moving east and north from README', 'move', '{"position":{"x":7,"y":3},"direction":"north","instructions":"RAALAL"}', '{"position":{"x":9,"y":4},"direction":"west"}'),
35+
('f30e4955-4b47-4aa3-8b39-ae98cfbd515b', 'moving west and north', 'move', '{"position":{"x":0,"y":0},"direction":"north","instructions":"LAAARALA"}', '{"position":{"x":-4,"y":1},"direction":"west"}'),
36+
('3e466bf6-20ab-4d79-8b51-264165182fca', 'moving west and south', 'move', '{"position":{"x":2,"y":-7},"direction":"east","instructions":"RRAAAAALA"}', '{"position":{"x":-3,"y":-8},"direction":"south"}'),
37+
('41f0bb96-c617-4e6b-acff-a4b279d44514', 'moving east and north', 'move', '{"position":{"x":8,"y":4},"direction":"south","instructions":"LAAARRRALLLL"}', '{"position":{"x":11,"y":5},"direction":"north"}');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"create","{""position"":{""x"":0,""y"":0},""direction"":""north""}",
2+
"create","{""position"":{""x"":-1,""y"":-1},""direction"":""south""}",
3+
"move","{""position"":{""x"":0,""y"":0},""direction"":""north"",""instructions"":""R""}",
4+
"move","{""position"":{""x"":0,""y"":0},""direction"":""east"",""instructions"":""R""}",
5+
"move","{""position"":{""x"":0,""y"":0},""direction"":""south"",""instructions"":""R""}",
6+
"move","{""position"":{""x"":0,""y"":0},""direction"":""west"",""instructions"":""R""}",
7+
"move","{""position"":{""x"":0,""y"":0},""direction"":""north"",""instructions"":""L""}",
8+
"move","{""position"":{""x"":0,""y"":0},""direction"":""west"",""instructions"":""L""}",
9+
"move","{""position"":{""x"":0,""y"":0},""direction"":""south"",""instructions"":""L""}",
10+
"move","{""position"":{""x"":0,""y"":0},""direction"":""east"",""instructions"":""L""}",
11+
"move","{""position"":{""x"":0,""y"":0},""direction"":""north"",""instructions"":""A""}",
12+
"move","{""position"":{""x"":0,""y"":0},""direction"":""south"",""instructions"":""A""}",
13+
"move","{""position"":{""x"":0,""y"":0},""direction"":""east"",""instructions"":""A""}",
14+
"move","{""position"":{""x"":0,""y"":0},""direction"":""west"",""instructions"":""A""}",
15+
"move","{""position"":{""x"":7,""y"":3},""direction"":""north"",""instructions"":""RAALAL""}",
16+
"move","{""position"":{""x"":0,""y"":0},""direction"":""north"",""instructions"":""LAAARALA""}",
17+
"move","{""position"":{""x"":2,""y"":-7},""direction"":""east"",""instructions"":""RRAAAAALA""}",
18+
"move","{""position"":{""x"":8,""y"":4},""direction"":""south"",""instructions"":""LAAARRRALLLL""}",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Schema:
2+
-- CREATE TABLE "robot-simulator" (
3+
-- property TEXT NOT NULL,
4+
-- input TEXT NOT NULL, -- json object
5+
-- result TEXT -- json object
6+
-- );
7+
--
8+
-- Task: update the robot-simulator table and set the result column based on property and input.

0 commit comments

Comments
 (0)