Skip to content

Commit 4eb070b

Browse files
Add robot-simulator exercise (#265)
* Add robot-simulator exercise * Apply suggestions from code review Co-authored-by: Bob Hoeppner <[email protected]> * UPdate name * Simplify * Fix --------- Co-authored-by: Bob Hoeppner <[email protected]>
1 parent 049019a commit 4eb070b

File tree

9 files changed

+430
-1
lines changed

9 files changed

+430
-1
lines changed

config.json

+19-1
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,25 @@
12251225
"difficulty": 7
12261226
},
12271227
{
1228-
1228+
"slug": "robot-simulator",
1229+
"name": "Robot Simulator",
1230+
"uuid": "99edb730-c79a-48b6-9bba-efc4b4882f44",
1231+
"practices": [
1232+
"switch-statements",
1233+
"switch-expressions",
1234+
"constructors"
1235+
],
1236+
"prerequisites": [
1237+
"enums",
1238+
"classes",
1239+
"constructors",
1240+
"properties",
1241+
"chars",
1242+
"switch-statements"
1243+
],
1244+
"difficulty": 3
1245+
},
1246+
{
12291247
"slug": "triangle",
12301248
"name": "Triangle",
12311249
"uuid": "78113af6-b1a4-40ab-86c6-3b26afdec75f",

exercises/Exercises.sln

+7
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "DndCharacter", "practice\dn
161161
EndProject
162162
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Poker", "practice\poker\Poker.vbproj", "{F62F286B-2E13-4914-B168-EDB399470B1C}"
163163
EndProject
164+
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "RobotSimulator", "practice\robot-simulator\RobotSimulator.vbproj", "{86A4A18F-6FC1-4953-9AAE-EEEE36FE8E2E}"
165+
EndProject
164166
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Triangle", "practice\triangle\Triangle.vbproj", "{1CE933B5-94BD-48AD-81B9-E352D75020CB}"
165167
EndProject
166168
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "RobotName", "practice\robot-name\RobotName.vbproj", "{B6F54248-A909-42A1-9A75-679160C63B28}"
@@ -492,6 +494,10 @@ Global
492494
{F62F286B-2E13-4914-B168-EDB399470B1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
493495
{F62F286B-2E13-4914-B168-EDB399470B1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
494496
{F62F286B-2E13-4914-B168-EDB399470B1C}.Release|Any CPU.Build.0 = Release|Any CPU
497+
{86A4A18F-6FC1-4953-9AAE-EEEE36FE8E2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
498+
{86A4A18F-6FC1-4953-9AAE-EEEE36FE8E2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
499+
{86A4A18F-6FC1-4953-9AAE-EEEE36FE8E2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
500+
{86A4A18F-6FC1-4953-9AAE-EEEE36FE8E2E}.Release|Any CPU.Build.0 = Release|Any CPU
495501
{1CE933B5-94BD-48AD-81B9-E352D75020CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
496502
{1CE933B5-94BD-48AD-81B9-E352D75020CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
497503
{1CE933B5-94BD-48AD-81B9-E352D75020CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -592,6 +598,7 @@ Global
592598
{057DDB74-6435-4B9E-988A-A21895AECE92} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
593599
{1C5BEE1E-B2B8-4D08-BC42-D83121DFFFB9} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
594600
{F62F286B-2E13-4914-B168-EDB399470B1C} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
601+
{86A4A18F-6FC1-4953-9AAE-EEEE36FE8E2E} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
595602
{1CE933B5-94BD-48AD-81B9-E352D75020CB} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
596603
{B6F54248-A909-42A1-9A75-679160C63B28} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
597604
{8CD79257-D9AF-4406-B93D-9C1AF256A6F4} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
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,84 @@
1+
Imports System
2+
3+
Public Enum DirectionType
4+
North
5+
East
6+
South
7+
West
8+
End Enum
9+
10+
Public Class RobotSimulator
11+
Public Sub New(ByVal startDirection As DirectionType, ByVal startX As Integer, ByVal startY As Integer)
12+
Direction = startDirection
13+
X = startX
14+
Y = startY
15+
End Sub
16+
17+
Public Property Direction As DirectionType
18+
Public Property X As Integer
19+
Public Property Y As Integer
20+
21+
Public Sub Move(ByVal instructions As String)
22+
For Each instruction In instructions
23+
Move(instruction)
24+
Next
25+
End Sub
26+
27+
Private Sub Move(ByVal code As Char)
28+
Select Case code
29+
Case "L"c
30+
TurnLeft()
31+
Case "R"c
32+
TurnRight()
33+
Case "A"c
34+
Advance()
35+
Case Else
36+
Throw New ArgumentOutOfRangeException(code)
37+
End Select
38+
End Sub
39+
40+
Private Sub TurnLeft()
41+
Select Case Direction
42+
Case DirectionType.North
43+
Direction = DirectionType.West
44+
Case DirectionType.East
45+
Direction = DirectionType.North
46+
Case DirectionType.South
47+
Direction = DirectionType.East
48+
Case DirectionType.West
49+
Direction = DirectionType.South
50+
Case Else
51+
Throw New ArgumentOutOfRangeException()
52+
End Select
53+
End Sub
54+
55+
Private Sub TurnRight()
56+
Select Case Direction
57+
Case DirectionType.North
58+
Direction = DirectionType.East
59+
Case DirectionType.East
60+
Direction = DirectionType.South
61+
Case DirectionType.South
62+
Direction = DirectionType.West
63+
Case DirectionType.West
64+
Direction = DirectionType.North
65+
Case Else
66+
Throw New ArgumentOutOfRangeException()
67+
End Select
68+
End Sub
69+
70+
Private Sub Advance()
71+
Select Case Direction
72+
Case DirectionType.North
73+
Y += 1
74+
Case DirectionType.East
75+
X += 1
76+
Case DirectionType.South
77+
Y -= 1
78+
Case DirectionType.West
79+
X -= 1
80+
Case Else
81+
Throw New ArgumentOutOfRangeException()
82+
End Select
83+
End Sub
84+
End Class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ErikSchierboom",
4+
"bobahop"
5+
],
6+
"files": {
7+
"solution": [
8+
"RobotSimulator.vb"
9+
],
10+
"test": [
11+
"RobotSimulatorTests.vb"
12+
],
13+
"example": [
14+
".meta/Example.vb"
15+
]
16+
},
17+
"blurb": "Write a robot simulator.",
18+
"source": "Inspired by an interview question at a famous company."
19+
}
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 = "at origin facing north"
14+
15+
[bf0dffce-f11c-4cdb-8a5e-2c89d8a5a67d]
16+
description = "at negative position facing south"
17+
18+
[8cbd0086-6392-4680-b9b9-73cf491e67e5]
19+
description = "changes north to east"
20+
21+
[8abc87fc-eab2-4276-93b7-9c009e866ba1]
22+
description = "changes east to south"
23+
24+
[3cfe1b85-bbf2-4bae-b54d-d73e7e93617a]
25+
description = "changes south to west"
26+
27+
[5ea9fb99-3f2c-47bd-86f7-46b7d8c3c716]
28+
description = "changes west to north"
29+
30+
[fa0c40f5-6ba3-443d-a4b3-58cbd6cb8d63]
31+
description = "changes north to west"
32+
33+
[da33d734-831f-445c-9907-d66d7d2a92e2]
34+
description = "changes west to south"
35+
36+
[bd1ca4b9-4548-45f4-b32e-900fc7c19389]
37+
description = "changes south to east"
38+
39+
[2de27b67-a25c-4b59-9883-bc03b1b55bba]
40+
description = "changes east to north"
41+
42+
[f0dc2388-cddc-4f83-9bed-bcf46b8fc7b8]
43+
description = "facing north increments Y"
44+
45+
[2786cf80-5bbf-44b0-9503-a89a9c5789da]
46+
description = "facing south decrements Y"
47+
48+
[84bf3c8c-241f-434d-883d-69817dbd6a48]
49+
description = "facing east increments X"
50+
51+
[bb69c4a7-3bbf-4f64-b415-666fa72d7b04]
52+
description = "facing west decrements X"
53+
54+
[e34ac672-4ed4-4be3-a0b8-d9af259cbaa1]
55+
description = "moving east and north from README"
56+
57+
[f30e4955-4b47-4aa3-8b39-ae98cfbd515b]
58+
description = "moving west and north"
59+
60+
[3e466bf6-20ab-4d79-8b51-264165182fca]
61+
description = "moving west and south"
62+
63+
[41f0bb96-c617-4e6b-acff-a4b279d44514]
64+
description = "moving east and north"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Imports System
2+
3+
Public Enum DirectionType
4+
North
5+
East
6+
South
7+
West
8+
End Enum
9+
10+
Public Class RobotSimulator
11+
Public Sub New(ByVal direction As DirectionType, ByVal x As Integer, ByVal y As Integer)
12+
End Sub
13+
14+
Public ReadOnly Property Direction As DirectionType
15+
Get
16+
Throw New NotImplementedException("You need to implement this function.")
17+
End Get
18+
End Property
19+
20+
Public ReadOnly Property X As Integer
21+
Get
22+
Throw New NotImplementedException("You need to implement this function.")
23+
End Get
24+
End Property
25+
26+
Public ReadOnly Property Y As Integer
27+
Get
28+
Throw New NotImplementedException("You need to implement this function.")
29+
End Get
30+
End Property
31+
32+
Public Sub Move(ByVal instructions As String)
33+
Throw New NotImplementedException("You need to implement this function.")
34+
End Sub
35+
End Class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net7.0</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
8+
<PackageReference Include="xunit" Version="2.4.2" />
9+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
10+
<PrivateAssets>all</PrivateAssets>
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
12+
</PackageReference>
13+
</ItemGroup>
14+
</Project>

0 commit comments

Comments
 (0)