Skip to content

Commit 4280977

Browse files
authored
Add robot-simulator (#32)
1 parent dcc3f97 commit 4280977

File tree

7 files changed

+307
-0
lines changed

7 files changed

+307
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@
162162
"practices": [],
163163
"prerequisites": [],
164164
"difficulty": 3
165+
},
166+
{
167+
"slug": "robot-simulator",
168+
"name": "Robot Simulator",
169+
"uuid": "31a8cb4a-c211-4daa-a14b-7347fed015a6",
170+
"practices": [],
171+
"prerequisites": [],
172+
"difficulty": 3
165173
}
166174
]
167175
},
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"contributors": [
6+
"pfertyk"
7+
],
8+
"files": {
9+
"solution": [
10+
"robot_simulator.gd"
11+
],
12+
"test": [
13+
"robot_simulator_test.gd"
14+
],
15+
"example": [
16+
".meta/example.gd"
17+
]
18+
},
19+
"blurb": "Write a robot simulator.",
20+
"source": "Inspired by an interview question at a famous company."
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@export var position : Vector2i
2+
@export var direction : String
3+
4+
5+
var allowed_directions = ["north", "east", "south", "west"]
6+
7+
8+
func move(instructions: String):
9+
for instruction in instructions:
10+
match [instruction, direction]:
11+
["L", _]:
12+
var index = allowed_directions.find(direction) - 1
13+
direction = allowed_directions[index]
14+
["R", _]:
15+
var index = allowed_directions.find(direction)
16+
index = (index + 1) % 4
17+
direction = allowed_directions[index]
18+
["A", "north"]:
19+
position += Vector2i(0, 1)
20+
["A", "south"]:
21+
position += Vector2i(0, -1)
22+
["A", "east"]:
23+
position += Vector2i(1, 0)
24+
["A", "west"]:
25+
position += Vector2i(-1, 0)
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@export var position : Vector2i
2+
@export var direction : String
3+
4+
5+
func move(instructions: String):
6+
pass
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
func test_create_robot_at_origin_facing_north(robot):
2+
robot.position = Vector2i(0, 0)
3+
robot.direction = "north"
4+
var result = [robot.position, robot.direction]
5+
var expected = [Vector2i(0, 0), "north"]
6+
return [expected, result]
7+
8+
9+
func test_create_robot_at_negative_position_facing_south(robot):
10+
robot.position = Vector2i(-1, -1)
11+
robot.direction = "south"
12+
var result = [robot.position, robot.direction]
13+
var expected = [Vector2i(-1, -1), "south"]
14+
return [expected, result]
15+
16+
17+
func test_rotating_clockwise_changes_north_to_east(robot):
18+
robot.position = Vector2i(0, 0)
19+
robot.direction = "north"
20+
robot.move("R")
21+
var result = [robot.position, robot.direction]
22+
var expected = [Vector2i(0, 0), "east"]
23+
return [expected, result]
24+
25+
26+
func test_rotating_clockwise_changes_east_to_south(robot):
27+
robot.position = Vector2i(0, 0)
28+
robot.direction = "east"
29+
robot.move("R")
30+
var result = [robot.position, robot.direction]
31+
var expected = [Vector2i(0, 0), "south"]
32+
return [expected, result]
33+
34+
35+
func test_rotating_clockwise_changes_south_to_west(robot):
36+
robot.position = Vector2i(0, 0)
37+
robot.direction = "south"
38+
robot.move("R")
39+
var result = [robot.position, robot.direction]
40+
var expected = [Vector2i(0, 0), "west"]
41+
return [expected, result]
42+
43+
44+
func test_rotating_clockwise_changes_west_to_north(robot):
45+
robot.position = Vector2i(0, 0)
46+
robot.direction = "west"
47+
robot.move("R")
48+
var result = [robot.position, robot.direction]
49+
var expected = [Vector2i(0, 0), "north"]
50+
return [expected, result]
51+
52+
53+
func test_rotating_counterclockwise_changes_north_to_west(robot):
54+
robot.position = Vector2i(0, 0)
55+
robot.direction = "north"
56+
robot.move("L")
57+
var result = [robot.position, robot.direction]
58+
var expected = [Vector2i(0, 0), "west"]
59+
return [expected, result]
60+
61+
62+
func test_rotating_counterclockwise_changes_west_to_south(robot):
63+
robot.position = Vector2i(0, 0)
64+
robot.direction = "west"
65+
robot.move("L")
66+
var result = [robot.position, robot.direction]
67+
var expected = [Vector2i(0, 0), "south"]
68+
return [expected, result]
69+
70+
71+
func test_rotating_counterclockwise_changes_south_to_east(robot):
72+
robot.position = Vector2i(0, 0)
73+
robot.direction = "south"
74+
robot.move("L")
75+
var result = [robot.position, robot.direction]
76+
var expected = [Vector2i(0, 0), "east"]
77+
return [expected, result]
78+
79+
80+
func test_rotating_counterclockwise_changes_east_to_north(robot):
81+
robot.position = Vector2i(0, 0)
82+
robot.direction = "east"
83+
robot.move("L")
84+
var result = [robot.position, robot.direction]
85+
var expected = [Vector2i(0, 0), "north"]
86+
return [expected, result]
87+
88+
89+
func test_moving_forward_one_facing_north_increments_y(robot):
90+
robot.position = Vector2i(0, 0)
91+
robot.direction = "north"
92+
robot.move("A")
93+
var result = [robot.position, robot.direction]
94+
var expected = [Vector2i(0, 1), "north"]
95+
return [expected, result]
96+
97+
98+
func test_moving_forward_one_facing_south_decrements_y(robot):
99+
robot.position = Vector2i(0, 0)
100+
robot.direction = "south"
101+
robot.move("A")
102+
var result = [robot.position, robot.direction]
103+
var expected = [Vector2i(0, -1), "south"]
104+
return [expected, result]
105+
106+
107+
func test_moving_forward_one_facing_east_increments_x(robot):
108+
robot.position = Vector2i(0, 0)
109+
robot.direction = "east"
110+
robot.move("A")
111+
var result = [robot.position, robot.direction]
112+
var expected = [Vector2i(1, 0), "east"]
113+
return [expected, result]
114+
115+
116+
func test_moving_forward_one_facing_west_decrements_x(robot):
117+
robot.position = Vector2i(0, 0)
118+
robot.direction = "west"
119+
robot.move("A")
120+
var result = [robot.position, robot.direction]
121+
var expected = [Vector2i(-1, 0), "west"]
122+
return [expected, result]
123+
124+
125+
func test_moving_east_and_north_from_readme(robot):
126+
robot.position = Vector2i(7, 3)
127+
robot.direction = "north"
128+
robot.move("RAALAL")
129+
var result = [robot.position, robot.direction]
130+
var expected = [Vector2i(9, 4), "west"]
131+
return [expected, result]
132+
133+
134+
func test_moving_west_and_north(robot):
135+
robot.position = Vector2i(0, 0)
136+
robot.direction = "north"
137+
robot.move("LAAARALA")
138+
var result = [robot.position, robot.direction]
139+
var expected = [Vector2i(-4, 1), "west"]
140+
return [expected, result]
141+
142+
143+
func test_moving_west_and_south(robot):
144+
robot.position = Vector2i(2, -7)
145+
robot.direction = "east"
146+
robot.move("RRAAAAALA")
147+
var result = [robot.position, robot.direction]
148+
var expected = [Vector2i(-3, -8), "south"]
149+
return [expected, result]
150+
151+
152+
func test_moving_east_and_north(robot):
153+
robot.position = Vector2i(8, 4)
154+
robot.direction = "south"
155+
robot.move("LAAARRRALLLL")
156+
var result = [robot.position, robot.direction]
157+
var expected = [Vector2i(11, 5), "north"]
158+
return [expected, result]

0 commit comments

Comments
 (0)