Skip to content

Commit ee77dde

Browse files
Add robot-simulator exercise (exercism#270)
1 parent 5780447 commit ee77dde

File tree

8 files changed

+366
-0
lines changed

8 files changed

+366
-0
lines changed

bin/reorder-exercises

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
required_tool() {
4+
command -v "${1}" >/dev/null 2>&1 ||
5+
die "${1} is required but not installed. Please install it and make sure it's in your PATH."
6+
}
7+
8+
required_tool jq
9+
10+
jq '.exercises.practice |= sort_by(.difficulty, (.name|ascii_upcase))' config.json > tmp && mv tmp config.json
11+
bin/fetch-configlet && bin/configlet fmt --update --yes

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,14 @@
600600
"prerequisites": [],
601601
"difficulty": 4
602602
},
603+
{
604+
"slug": "robot-simulator",
605+
"name": "Robot Simulator",
606+
"uuid": "513f8b55-a335-474c-b9bf-4aca8b20c720",
607+
"practices": [],
608+
"prerequisites": [],
609+
"difficulty": 4
610+
},
603611
{
604612
"slug": "rotational-cipher",
605613
"name": "Rotational 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+
"robot-simulator.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
14+
]
15+
},
16+
"blurb": "Write a robot simulator.",
17+
"source": "Inspired by an interview question at a famous company."
18+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module main
2+
3+
struct Position {
4+
x int
5+
y int
6+
}
7+
8+
enum Direction {
9+
north
10+
east
11+
south
12+
west
13+
}
14+
15+
struct Robot {
16+
mut:
17+
position Position
18+
direction Direction
19+
}
20+
21+
fn Robot.new(x int, y int, direction Direction) Robot {
22+
return Robot{Position{x, y}, direction}
23+
}
24+
25+
fn (mut robot Robot) move(instructions string) {
26+
for instruction in instructions {
27+
match instruction {
28+
`R` {
29+
match robot.direction {
30+
.north {
31+
robot.direction = .east
32+
}
33+
.east {
34+
robot.direction = .south
35+
}
36+
.south {
37+
robot.direction = .west
38+
}
39+
.west {
40+
robot.direction = .north
41+
}
42+
}
43+
}
44+
`L` {
45+
match robot.direction {
46+
.north {
47+
robot.direction = .west
48+
}
49+
.east {
50+
robot.direction = .north
51+
}
52+
.south {
53+
robot.direction = .east
54+
}
55+
.west {
56+
robot.direction = .south
57+
}
58+
}
59+
}
60+
`A` {
61+
match robot.direction {
62+
.north {
63+
robot.position = Position{robot.position.x, robot.position.y + 1}
64+
}
65+
.east {
66+
robot.position = Position{robot.position.x + 1, robot.position.y}
67+
}
68+
.south {
69+
robot.position = Position{robot.position.x, robot.position.y - 1}
70+
}
71+
.west {
72+
robot.position = Position{robot.position.x - 1, robot.position.y}
73+
}
74+
}
75+
}
76+
else {}
77+
}
78+
}
79+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[c557c16d-26c1-4e06-827c-f6602cd0785c]
6+
description = "Create robot -> at origin facing north"
7+
8+
[bf0dffce-f11c-4cdb-8a5e-2c89d8a5a67d]
9+
description = "Create robot -> at negative position facing south"
10+
11+
[8cbd0086-6392-4680-b9b9-73cf491e67e5]
12+
description = "Rotating clockwise -> changes north to east"
13+
14+
[8abc87fc-eab2-4276-93b7-9c009e866ba1]
15+
description = "Rotating clockwise -> changes east to south"
16+
17+
[3cfe1b85-bbf2-4bae-b54d-d73e7e93617a]
18+
description = "Rotating clockwise -> changes south to west"
19+
20+
[5ea9fb99-3f2c-47bd-86f7-46b7d8c3c716]
21+
description = "Rotating clockwise -> changes west to north"
22+
23+
[fa0c40f5-6ba3-443d-a4b3-58cbd6cb8d63]
24+
description = "Rotating counter-clockwise -> changes north to west"
25+
26+
[da33d734-831f-445c-9907-d66d7d2a92e2]
27+
description = "Rotating counter-clockwise -> changes west to south"
28+
29+
[bd1ca4b9-4548-45f4-b32e-900fc7c19389]
30+
description = "Rotating counter-clockwise -> changes south to east"
31+
32+
[2de27b67-a25c-4b59-9883-bc03b1b55bba]
33+
description = "Rotating counter-clockwise -> changes east to north"
34+
35+
[f0dc2388-cddc-4f83-9bed-bcf46b8fc7b8]
36+
description = "Moving forward one -> facing north increments Y"
37+
38+
[2786cf80-5bbf-44b0-9503-a89a9c5789da]
39+
description = "Moving forward one -> facing south decrements Y"
40+
41+
[84bf3c8c-241f-434d-883d-69817dbd6a48]
42+
description = "Moving forward one -> facing east increments X"
43+
44+
[bb69c4a7-3bbf-4f64-b415-666fa72d7b04]
45+
description = "Moving forward one -> facing west decrements X"
46+
47+
[e34ac672-4ed4-4be3-a0b8-d9af259cbaa1]
48+
description = "Follow series of instructions -> moving east and north from README"
49+
50+
[f30e4955-4b47-4aa3-8b39-ae98cfbd515b]
51+
description = "Follow series of instructions -> moving west and north"
52+
53+
[3e466bf6-20ab-4d79-8b51-264165182fca]
54+
description = "Follow series of instructions -> moving west and south"
55+
56+
[41f0bb96-c617-4e6b-acff-a4b279d44514]
57+
description = "Follow series of instructions -> moving east and north"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module main
2+
3+
struct Position {
4+
x int
5+
y int
6+
}
7+
8+
enum Direction {
9+
north
10+
east
11+
south
12+
west
13+
}
14+
15+
struct Robot {
16+
// Please define the `Robot` struct
17+
}
18+
19+
fn Robot.new(x int, y int, direction Direction) Robot {
20+
// Please implement the `Robot.new` function
21+
}
22+
23+
fn (mut robot Robot) move(instructions string) {
24+
// Please implement the `move` function
25+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
module main
2+
3+
fn test_create_robot__at_origin_facing_north() {
4+
robot := Robot.new(0, 0, .north)
5+
assert robot.position.x == 0
6+
assert robot.position.y == 0
7+
assert robot.direction == .north
8+
}
9+
10+
fn test_create_robot__at_negative_position_facing_south() {
11+
robot := Robot.new(-1, -1, .south)
12+
assert robot.position.x == -1
13+
assert robot.position.y == -1
14+
assert robot.direction == .south
15+
}
16+
17+
fn test_rotating_clockwise__changes_north_to_east() {
18+
mut robot := Robot.new(0, 0, .north)
19+
robot.move('R')
20+
assert robot.position.x == 0
21+
assert robot.position.y == 0
22+
assert robot.direction == .east
23+
}
24+
25+
fn test_rotating_clockwise__changes_east_to_south() {
26+
mut robot := Robot.new(0, 0, .east)
27+
robot.move('R')
28+
assert robot.position.x == 0
29+
assert robot.position.y == 0
30+
assert robot.direction == .south
31+
}
32+
33+
fn test_rotating_clockwise__changes_south_to_west() {
34+
mut robot := Robot.new(0, 0, .south)
35+
robot.move('R')
36+
assert robot.position.x == 0
37+
assert robot.position.y == 0
38+
assert robot.direction == .west
39+
}
40+
41+
fn test_rotating_clockwise__changes_west_to_north() {
42+
mut robot := Robot.new(0, 0, .west)
43+
robot.move('R')
44+
assert robot.position.x == 0
45+
assert robot.position.y == 0
46+
assert robot.direction == .north
47+
}
48+
49+
fn test_rotating_counter_clockwise__changes_north_to_west() {
50+
mut robot := Robot.new(0, 0, .north)
51+
robot.move('L')
52+
assert robot.position.x == 0
53+
assert robot.position.y == 0
54+
assert robot.direction == .west
55+
}
56+
57+
fn test_rotating_counter_clockwise__changes_west_to_south() {
58+
mut robot := Robot.new(0, 0, .west)
59+
robot.move('L')
60+
assert robot.position.x == 0
61+
assert robot.position.y == 0
62+
assert robot.direction == .south
63+
}
64+
65+
fn test_rotating_counter_clockwise__changes_south_to_east() {
66+
mut robot := Robot.new(0, 0, .south)
67+
robot.move('L')
68+
assert robot.position.x == 0
69+
assert robot.position.y == 0
70+
assert robot.direction == .east
71+
}
72+
73+
fn test_rotating_counter_clockwise__changes_east_to_north() {
74+
mut robot := Robot.new(0, 0, .east)
75+
robot.move('L')
76+
assert robot.position.x == 0
77+
assert robot.position.y == 0
78+
assert robot.direction == .north
79+
}
80+
81+
fn test_moving_forward_one__facing_north_increments_y() {
82+
mut robot := Robot.new(0, 0, .north)
83+
robot.move('A')
84+
assert robot.position.x == 0
85+
assert robot.position.y == 1
86+
assert robot.direction == .north
87+
}
88+
89+
fn test_moving_forward_one__facing_south_decrements_y() {
90+
mut robot := Robot.new(0, 0, .south)
91+
robot.move('A')
92+
assert robot.position.x == 0
93+
assert robot.position.y == -1
94+
assert robot.direction == .south
95+
}
96+
97+
fn test_moving_forward_one__facing_east_increments_x() {
98+
mut robot := Robot.new(0, 0, .east)
99+
robot.move('A')
100+
assert robot.position.x == 1
101+
assert robot.position.y == 0
102+
assert robot.direction == .east
103+
}
104+
105+
fn test_moving_forward_one__facing_west_decrements_x() {
106+
mut robot := Robot.new(0, 0, .west)
107+
robot.move('A')
108+
assert robot.position.x == -1
109+
assert robot.position.y == 0
110+
assert robot.direction == .west
111+
}
112+
113+
fn test_follow_series_of_instructions__moving_east_and_north_from_readme() {
114+
mut robot := Robot.new(7, 3, .north)
115+
robot.move('RAALAL')
116+
assert robot.position.x == 9
117+
assert robot.position.y == 4
118+
assert robot.direction == .west
119+
}
120+
121+
fn test_follow_series_of_instructions__moving_west_and_north() {
122+
mut robot := Robot.new(0, 0, .north)
123+
robot.move('LAAARALA')
124+
assert robot.position.x == -4
125+
assert robot.position.y == 1
126+
assert robot.direction == .west
127+
}
128+
129+
fn test_follow_series_of_instructions__moving_west_and_south() {
130+
mut robot := Robot.new(2, -7, .east)
131+
robot.move('RRAAAAALA')
132+
assert robot.position.x == -3
133+
assert robot.position.y == -8
134+
assert robot.direction == .south
135+
}
136+
137+
fn test_follow_series_of_instructions__moving_east_and_north() {
138+
mut robot := Robot.new(8, 4, .south)
139+
robot.move('LAAARRRALLLL')
140+
assert robot.position.x == 11
141+
assert robot.position.y == 5
142+
assert robot.direction == .north
143+
}

0 commit comments

Comments
 (0)