Skip to content

Commit 132889d

Browse files
committed
exercises(robot-simulator): implement
1 parent 5dda99d commit 132889d

File tree

7 files changed

+274
-0
lines changed

7 files changed

+274
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,14 @@
979979
"events",
980980
"reactive_programming"
981981
]
982+
},
983+
{
984+
"slug": "robot-simulator",
985+
"name": "Robot Simulator",
986+
"uuid": "cfedbb93-eda4-49f9-92a1-aa1a6cc8453f",
987+
"practices": [],
988+
"prerequisites": [],
989+
"difficulty": 1
982990
}
983991
]
984992
},
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"authors": [],
3+
"files": {
4+
"solution": [
5+
"robot_simulator.nim"
6+
],
7+
"test": [
8+
"test_robot_simulator.nim"
9+
],
10+
"example": [
11+
".meta/example.nim"
12+
]
13+
},
14+
"blurb": "Write a robot simulator.",
15+
"source": "Inspired by an interview question at a famous company."
16+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
type
2+
Compass* = enum
3+
North,East,South,West
4+
Robot* = object
5+
direction*: Compass
6+
x_pos*: int
7+
y_pos*: int
8+
9+
proc initRobot*(facing: Compass=North, x: int=0, y: int=0) : Robot =
10+
Robot(direction: facing,x_pos: x,y_pos: y)
11+
12+
proc right(dir: Compass) : Compass =
13+
case dir:
14+
of North: East
15+
of East: South
16+
of South: West
17+
of West: North
18+
19+
proc left(dir: Compass) : Compass =
20+
case dir:
21+
of North: West
22+
of East: North
23+
of South: East
24+
of West: South
25+
26+
proc move*(robot: var Robot, instructions: string) =
27+
for c in instructions:
28+
if c=='L':
29+
robot.direction=robot.direction.left
30+
if c=='R':
31+
robot.direction=robot.direction.right
32+
if c!='A':
33+
continue
34+
case robot.direction:
35+
of North: inc(robot.y_pos)
36+
of South: dec(robot.y_pos)
37+
of East: inc(robot.x_pos)
38+
of West: dec(robot.x_pos)
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type
2+
Compass* = enum
3+
North,East,South,West
4+
Robot* = object
5+
direction*: Compass
6+
x_pos*: int
7+
y_pos*: int
8+
9+
# constructor proc to return a new robot with default attributes
10+
proc initRobot*(facing: Compass=North, x: int=0, y: int=0) : Robot =
11+
discard
12+
13+
# implement this proc to move the robot following the instructions
14+
proc move*(robot: var Robot, instructions: string) =
15+
discard
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import unittest
2+
import robot_simulator
3+
4+
suite "Robot_Simulator":
5+
test "Create robot -> at origin facing north":
6+
var robot = initRobot()
7+
check robot.direction == North
8+
9+
test "Create robot -> at negative position facing south":
10+
var robot = initRobot(South,-1,-1)
11+
check robot.direction == South
12+
check robot.x_pos == -1 and robot.y_pos == -1
13+
14+
test "Rotating clockwise -> changes north to east":
15+
var robot = initRobot()
16+
robot.move("R")
17+
check robot.direction == East
18+
check robot.x_pos == 0 and robot.y_pos == 0
19+
20+
test "Rotating clockwise -> changes east to south":
21+
var robot = initRobot(East)
22+
robot.move("R")
23+
check robot.direction == South
24+
check robot.x_pos == 0 and robot.y_pos == 0
25+
26+
test "Rotating clockwise -> changes south to west":
27+
var robot = initRobot(South)
28+
robot.move("R")
29+
check robot.direction == West
30+
check robot.x_pos == 0 and robot.y_pos == 0
31+
32+
test "Rotating clockwise -> changes west to north":
33+
var robot = initRobot(West)
34+
robot.move("R")
35+
check robot.direction == North
36+
check robot.x_pos == 0 and robot.y_pos == 0
37+
38+
test "Rotating counter-clockwise -> changes north to west":
39+
var robot = initRobot()
40+
robot.move("L")
41+
check robot.direction == West
42+
check robot.x_pos == 0 and robot.y_pos == 0
43+
44+
test "Rotating counter-clockwise -> changes west to south":
45+
var robot = initRobot(West)
46+
robot.move("L")
47+
check robot.direction == South
48+
check robot.x_pos == 0 and robot.y_pos == 0
49+
50+
test "Rotating counter-clockwise -> changes south to east":
51+
var robot = initRobot(South)
52+
robot.move("L")
53+
check robot.direction == East
54+
check robot.x_pos == 0 and robot.y_pos == 0
55+
56+
test "Rotating counter-clockwise -> changes east to north":
57+
var robot = initRobot(East)
58+
robot.move("L")
59+
check robot.direction == North
60+
check robot.x_pos == 0 and robot.y_pos == 0
61+
62+
test "Moving forward one -> facing north increments Y":
63+
var robot = initRobot()
64+
robot.move("A")
65+
check robot.direction == North
66+
check robot.x_pos == 0 and robot.y_pos == 1
67+
68+
test "Moving forward one -> facing south decrements Y":
69+
var robot = initRobot(South)
70+
robot.move("A")
71+
check robot.direction == South
72+
check robot.x_pos == 0 and robot.y_pos == -1
73+
74+
test "Moving forward one -> facing east increments X":
75+
var robot = initRobot(East)
76+
robot.move("A")
77+
check robot.direction == East
78+
check robot.x_pos == 1 and robot.y_pos == 0
79+
80+
test "Moving forward one -> facing west decrements X":
81+
var robot = initRobot(West)
82+
robot.move("A")
83+
check robot.direction == West
84+
check robot.x_pos == -1 and robot.y_pos == 0
85+
86+
test "Follow series of instructions -> moving east and north from README":
87+
var robot = initRobot(North,7,3)
88+
robot.move("RAALAL")
89+
check robot.direction == West
90+
check robot.x_pos == 9 and robot.y_pos == 4
91+
92+
test "Follow series of instructions -> moving west and north":
93+
var robot = initRobot()
94+
robot.move("LAAARALA")
95+
check robot.direction == West
96+
check robot.x_pos == -4 and robot.y_pos == 1
97+
98+
test "Follow series of instructions -> moving west and south":
99+
var robot = initRobot(East,2,-7)
100+
robot.move("RRAAAAALA")
101+
check robot.direction == South
102+
check robot.x_pos == -3 and robot.y_pos == -8
103+
104+
test "Follow series of instructions -> moving east and north":
105+
var robot = initRobot(South,8,4)
106+
robot.move("LAAARRRALLLL")
107+
check robot.direction == North
108+
check robot.x_pos == 11 and robot.y_pos == 5

0 commit comments

Comments
 (0)