Skip to content

Commit 9c6eb1d

Browse files
Add robot-simulator exercise (#26)
1 parent 4199154 commit 9c6eb1d

9 files changed

Lines changed: 582 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@
201201
"prerequisites": [],
202202
"difficulty": 4
203203
},
204+
{
205+
"slug": "robot-simulator",
206+
"name": "Robot Simulator",
207+
"uuid": "60c7cac3-3593-4625-8939-7c33695bc50c",
208+
"practices": [],
209+
"prerequisites": [],
210+
"difficulty": 4
211+
},
204212
{
205213
"slug": "rotational-cipher",
206214
"name": "Rotational Cipher",
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+
"erikschierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"solution.jai"
8+
],
9+
"test": [
10+
"tests.jai"
11+
],
12+
"example": [
13+
".meta/example.jai"
14+
]
15+
},
16+
"blurb": "Write a robot simulator.",
17+
"source": "Inspired by an interview question at a famous company."
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Direction :: enum u8 {
2+
NORTH;
3+
EAST;
4+
SOUTH;
5+
WEST;
6+
};
7+
8+
Robot :: struct {
9+
x: int;
10+
y: int;
11+
direction: Direction;
12+
};
13+
14+
create :: (x: int, y: int, direction: Direction) -> Robot {
15+
return .{x, y, direction};
16+
}
17+
18+
x :: (using robot: Robot) -> int {
19+
return x;
20+
}
21+
22+
y :: (using robot: Robot) -> int {
23+
return y;
24+
}
25+
26+
direction :: (using robot: Robot) -> Direction {
27+
return direction;
28+
}
29+
30+
move :: (using robot: *Robot, instructions: string) {
31+
for instructions {
32+
if it == {
33+
case #char "R"; direction = (direction + 1) % 4;
34+
case #char "L"; direction = (direction + 3) % 4;
35+
case #char "A";
36+
if direction == {
37+
case Direction.NORTH; y += 1;
38+
case Direction.EAST; x += 1;
39+
case Direction.SOUTH; y -= 1;
40+
case Direction.WEST; x -= 1;
41+
}
42+
}
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
to_test_case :: (canonical_case: CanonicalCase) -> Test {
2+
_, x := get_property(canonical_case.input, "position", "x");
3+
_, y := get_property(canonical_case.input, "position", "y");
4+
_, direction := get_property(canonical_case.input, "direction");
5+
6+
_, expected_x := get_property(canonical_case.expected, "position", "x");
7+
_, expected_y := get_property(canonical_case.expected, "position", "y");
8+
_, expected_direction := get_property(canonical_case.expected, "direction");
9+
10+
lines: [..] string;
11+
array_add(*lines, tprint("robot := create(%, %, %);", to_code(x), to_code(y), to_enum(direction)));
12+
13+
if canonical_case.property == "move" {
14+
_, instructions := get_property(canonical_case.input, "instructions");
15+
array_add(*lines, tprint("move(*robot, %);", to_code(instructions)));
16+
}
17+
18+
array_add(*lines, tprint("assert_equal(%, robot.x);", to_code(expected_x)));
19+
array_add(*lines, tprint("assert_equal(%, robot.y);", to_code(expected_y)));
20+
array_add(*lines, tprint("assert_equal(%, robot.direction);", to_enum(expected_direction, "Direction")));
21+
22+
return Test.{
23+
name = to_test_name(canonical_case.description_path),
24+
multiline_body = lines,
25+
skip = canonical_case.index > 0
26+
};
27+
}
28+
29+
#run run_generator("robot-simulator", to_test_case);
30+
31+
#import "Basic";
32+
#import,dir "../../../shared/generator";
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"

0 commit comments

Comments
 (0)