Skip to content

Commit 2d0d876

Browse files
authored
robot-simulator (#49)
1 parent c39420c commit 2d0d876

File tree

9 files changed

+321
-0
lines changed

9 files changed

+321
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,14 @@
374374
"practices": [],
375375
"prerequisites": [],
376376
"difficulty": 4
377+
},
378+
{
379+
"slug": "robot-simulator",
380+
"name": "Robot Simulator",
381+
"uuid": "55452cf4-0037-4bf5-a518-c16fb5d1da83",
382+
"practices": [],
383+
"prerequisites": [],
384+
"difficulty": 4
377385
}
378386
]
379387
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"robot_simulator.moon"
8+
],
9+
"test": [
10+
"robot_simulator_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Write a robot simulator.",
17+
"source": "Inspired by an interview question at a famous company."
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Turns = {
2+
north: {left: 'west', right: 'east'},
3+
east: {left: 'north', right: 'south'},
4+
south: {left: 'east', right: 'west'},
5+
west: {left: 'south', right: 'north'},
6+
}
7+
8+
9+
class Robot
10+
new: (params) =>
11+
@_x = params.x
12+
@_y = params.y
13+
@_dir = params.direction
14+
15+
x: => @_x
16+
y: => @_y
17+
direction: => @_dir
18+
19+
move: (instructions) =>
20+
for instruction in instructions\gmatch('[RLA]')
21+
switch instruction
22+
when 'R' then @_dir = Turns[@_dir].right
23+
when 'L' then @_dir = Turns[@_dir].left
24+
when 'A'
25+
switch @_dir
26+
when 'north' then @_y += 1
27+
when 'east' then @_x += 1
28+
when 'south' then @_y -= 1
29+
when 'west' then @_x -= 1
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
module_name: 'Robot',
3+
4+
generate_test: (case, level) ->
5+
local lines
6+
switch case.property
7+
when 'create'
8+
lines = {
9+
"robot = Robot x: #{case.input.position.x}, y: #{case.input.position.y}, direction: #{quote case.input.direction}",
10+
"assert.are.equal #{case.expected.position.x}, robot\\x!",
11+
"assert.are.equal #{case.expected.position.y}, robot\\y!",
12+
"assert.are.equal #{quote case.expected.direction}, robot\\direction!"
13+
}
14+
15+
when 'move'
16+
lines = {
17+
"robot = Robot x: #{case.input.position.x}, y: #{case.input.position.y}, direction: #{quote case.input.direction}",
18+
"robot\\move #{quote case.input.instructions}",
19+
20+
"assert.are.equal #{case.expected.position.x}, robot\\x!",
21+
"assert.are.equal #{case.expected.position.y}, robot\\y!",
22+
"assert.are.equal #{quote case.expected.direction}, robot\\direction!"
23+
}
24+
25+
table.concat [indent line, level for line in *lines], '\n'
26+
}
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+
class Robot
2+
new: (params) =>
3+
error 'Implement the constructor'
4+
5+
x: =>
6+
error 'Implement the x method'
7+
8+
y: =>
9+
error 'Implement the y method'
10+
11+
direction: =>
12+
error 'Implement the direction method'
13+
14+
move: (instructions) =>
15+
error 'Implement the move method'
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
Robot = require 'robot_simulator'
2+
3+
describe 'robot-simulator', ->
4+
describe 'Create robot', ->
5+
it 'at origin facing north', ->
6+
robot = Robot x: 0, y: 0, direction: 'north'
7+
assert.are.equal 0, robot\x!
8+
assert.are.equal 0, robot\y!
9+
assert.are.equal 'north', robot\direction!
10+
11+
pending 'at negative position facing south', ->
12+
robot = Robot x: -1, y: -1, direction: 'south'
13+
assert.are.equal -1, robot\x!
14+
assert.are.equal -1, robot\y!
15+
assert.are.equal 'south', robot\direction!
16+
17+
describe 'Rotating clockwise', ->
18+
pending 'changes north to east', ->
19+
robot = Robot x: 0, y: 0, direction: 'north'
20+
robot\move 'R'
21+
assert.are.equal 0, robot\x!
22+
assert.are.equal 0, robot\y!
23+
assert.are.equal 'east', robot\direction!
24+
25+
pending 'changes east to south', ->
26+
robot = Robot x: 0, y: 0, direction: 'east'
27+
robot\move 'R'
28+
assert.are.equal 0, robot\x!
29+
assert.are.equal 0, robot\y!
30+
assert.are.equal 'south', robot\direction!
31+
32+
pending 'changes south to west', ->
33+
robot = Robot x: 0, y: 0, direction: 'south'
34+
robot\move 'R'
35+
assert.are.equal 0, robot\x!
36+
assert.are.equal 0, robot\y!
37+
assert.are.equal 'west', robot\direction!
38+
39+
pending 'changes west to north', ->
40+
robot = Robot x: 0, y: 0, direction: 'west'
41+
robot\move 'R'
42+
assert.are.equal 0, robot\x!
43+
assert.are.equal 0, robot\y!
44+
assert.are.equal 'north', robot\direction!
45+
46+
describe 'Rotating counter-clockwise', ->
47+
pending 'changes north to west', ->
48+
robot = Robot x: 0, y: 0, direction: 'north'
49+
robot\move 'L'
50+
assert.are.equal 0, robot\x!
51+
assert.are.equal 0, robot\y!
52+
assert.are.equal 'west', robot\direction!
53+
54+
pending 'changes west to south', ->
55+
robot = Robot x: 0, y: 0, direction: 'west'
56+
robot\move 'L'
57+
assert.are.equal 0, robot\x!
58+
assert.are.equal 0, robot\y!
59+
assert.are.equal 'south', robot\direction!
60+
61+
pending 'changes south to east', ->
62+
robot = Robot x: 0, y: 0, direction: 'south'
63+
robot\move 'L'
64+
assert.are.equal 0, robot\x!
65+
assert.are.equal 0, robot\y!
66+
assert.are.equal 'east', robot\direction!
67+
68+
pending 'changes east to north', ->
69+
robot = Robot x: 0, y: 0, direction: 'east'
70+
robot\move 'L'
71+
assert.are.equal 0, robot\x!
72+
assert.are.equal 0, robot\y!
73+
assert.are.equal 'north', robot\direction!
74+
75+
describe 'Moving forward one', ->
76+
pending 'facing north increments Y', ->
77+
robot = Robot x: 0, y: 0, direction: 'north'
78+
robot\move 'A'
79+
assert.are.equal 0, robot\x!
80+
assert.are.equal 1, robot\y!
81+
assert.are.equal 'north', robot\direction!
82+
83+
pending 'facing south decrements Y', ->
84+
robot = Robot x: 0, y: 0, direction: 'south'
85+
robot\move 'A'
86+
assert.are.equal 0, robot\x!
87+
assert.are.equal -1, robot\y!
88+
assert.are.equal 'south', robot\direction!
89+
90+
pending 'facing east increments X', ->
91+
robot = Robot x: 0, y: 0, direction: 'east'
92+
robot\move 'A'
93+
assert.are.equal 1, robot\x!
94+
assert.are.equal 0, robot\y!
95+
assert.are.equal 'east', robot\direction!
96+
97+
pending 'facing west decrements X', ->
98+
robot = Robot x: 0, y: 0, direction: 'west'
99+
robot\move 'A'
100+
assert.are.equal -1, robot\x!
101+
assert.are.equal 0, robot\y!
102+
assert.are.equal 'west', robot\direction!
103+
104+
describe 'Follow series of instructions', ->
105+
pending 'moving east and north from README', ->
106+
robot = Robot x: 7, y: 3, direction: 'north'
107+
robot\move 'RAALAL'
108+
assert.are.equal 9, robot\x!
109+
assert.are.equal 4, robot\y!
110+
assert.are.equal 'west', robot\direction!
111+
112+
pending 'moving west and north', ->
113+
robot = Robot x: 0, y: 0, direction: 'north'
114+
robot\move 'LAAARALA'
115+
assert.are.equal -4, robot\x!
116+
assert.are.equal 1, robot\y!
117+
assert.are.equal 'west', robot\direction!
118+
119+
pending 'moving west and south', ->
120+
robot = Robot x: 2, y: -7, direction: 'east'
121+
robot\move 'RRAAAAALA'
122+
assert.are.equal -3, robot\x!
123+
assert.are.equal -8, robot\y!
124+
assert.are.equal 'south', robot\direction!
125+
126+
pending 'moving east and north', ->
127+
robot = Robot x: 8, y: 4, direction: 'south'
128+
robot\move 'LAAARRRALLLL'
129+
assert.are.equal 11, robot\x!
130+
assert.are.equal 5, robot\y!
131+
assert.are.equal 'north', robot\direction!

0 commit comments

Comments
 (0)