Skip to content

Commit 8be0545

Browse files
Add robot-simulator exercise (#75)
1 parent fcb15e5 commit 8be0545

File tree

11 files changed

+1287
-0
lines changed

11 files changed

+1287
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,14 @@
468468
"prerequisites": [],
469469
"difficulty": 4
470470
},
471+
{
472+
"slug": "robot-simulator",
473+
"name": "Robot Simulator",
474+
"uuid": "189e3b7f-6b74-4333-b0f9-b3b4a218f899",
475+
"practices": [],
476+
"prerequisites": [],
477+
"difficulty": 4
478+
},
471479
{
472480
"slug": "affine-cipher",
473481
"name": "Affine 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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"RobotSimulator.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
14+
]
15+
},
16+
"blurb": "Write a robot simulator.",
17+
"source": "Inspired by an interview question at a famous company."
18+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
unit RobotSimulator;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TPosition = record
9+
x: Int64;
10+
y: Int64;
11+
end;
12+
13+
TDirection = (north, east, south, west);
14+
15+
TRobot = class
16+
private
17+
fpos: TPosition;
18+
fdir: TDirection;
19+
public
20+
constructor Create(const pos: TPosition; const dir: TDirection);
21+
procedure Move(const instructions : String);
22+
function ToString: String; override;
23+
end;
24+
25+
implementation
26+
27+
uses SysUtils;
28+
29+
constructor TRobot.Create(const pos: TPosition; const dir: TDirection);
30+
begin
31+
fpos := pos;
32+
fdir := dir;
33+
end;
34+
35+
procedure TRobot.Move(const instructions : String);
36+
var
37+
c : Char;
38+
begin
39+
for c in instructions do
40+
case c of
41+
'R':
42+
case fdir of
43+
north: fdir := east;
44+
east: fdir := south;
45+
south: fdir := west;
46+
west: fdir := north;
47+
end;
48+
'L':
49+
case fdir of
50+
north: fdir := west;
51+
east: fdir := north;
52+
south: fdir := east;
53+
west: fdir := south;
54+
end;
55+
'A':
56+
case fdir of
57+
north: fpos.y := fpos.y + 1;
58+
east: fpos.x := fpos.x + 1;
59+
south: fpos.y := fpos.y - 1;
60+
west: fpos.x := fpos.x - 1;
61+
end;
62+
else
63+
raise Exception.Create('invalid instruction');
64+
end;
65+
end;
66+
67+
function TRobot.ToString: String;
68+
var
69+
direction : String;
70+
begin
71+
case fdir of
72+
north:
73+
direction := 'N';
74+
east:
75+
direction := 'E';
76+
south:
77+
direction := 'S';
78+
west:
79+
direction := 'W';
80+
end;
81+
result := format('%d,%d %s', [fpos.x, fpos.y, direction]);
82+
end;
83+
84+
end.
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+
SHELL = /bin/bash
2+
MAKEFLAGS += --no-print-directory
3+
DESTDIR = build
4+
EXECUTABLE = $(DESTDIR)/test
5+
COMMAND = fpc -l- -v0 -g -gl -Sa -Cr -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"
6+
7+
.ONESHELL:
8+
9+
test:
10+
@mkdir -p "./$(DESTDIR)"
11+
@cp -r ./lib "./$(DESTDIR)"
12+
@$(COMMAND) && ./$(EXECUTABLE) $(test)
13+
14+
clean:
15+
@rm -fr "./$(DESTDIR)"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
unit RobotSimulator;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TPosition = record
9+
x: Int64;
10+
y: Int64;
11+
end;
12+
13+
TDirection = (north, east, south, west);
14+
15+
TRobot = class
16+
private
17+
{ Please implement your solution. }
18+
public
19+
constructor Create(const pos: TPosition; const dir: TDirection);
20+
procedure Move(const instructions : String);
21+
function ToString: String; override;
22+
end;
23+
24+
implementation
25+
26+
uses SysUtils;
27+
28+
constructor TRobot.Create(const pos: TPosition; const dir: TDirection);
29+
begin
30+
if (pos.x = pos.x) and (pos.y = pos.y) and (dir = dir) then
31+
raise ENotImplemented.Create('Please implement your solution.');
32+
end;
33+
34+
procedure TRobot.Move(const instructions : String);
35+
begin
36+
if instructions = instructions then
37+
raise ENotImplemented.Create('Please implement your solution.');
38+
end;
39+
40+
function TRobot.ToString: String;
41+
begin
42+
raise ENotImplemented.Create('Please implement your solution.');
43+
result := '';
44+
end;
45+
46+
end.

0 commit comments

Comments
 (0)