Skip to content

Commit 17b0420

Browse files
authored
Add robot simulator (#393)
1 parent e89a341 commit 17b0420

File tree

7 files changed

+284
-0
lines changed

7 files changed

+284
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@
308308
"prerequisites": [],
309309
"difficulty": 2
310310
},
311+
{
312+
"slug": "robot-simulator",
313+
"name": "Robot Simulator",
314+
"uuid": "575a40bd-2ff9-4623-ae98-c8d9b9f279a8",
315+
"practices": [],
316+
"prerequisites": [],
317+
"difficulty": 2
318+
},
311319
{
312320
"slug": "rna-transcription",
313321
"name": "RNA Transcription",
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,18 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"robot-simulator.el"
8+
],
9+
"test": [
10+
"robot-simulator-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
14+
]
15+
},
16+
"blurb": "Write a robot simulator.",
17+
"source": "Inspired by an interview question at a famous company."
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
;;; robot-simulator.el --- robot-simulator Exercise (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defconst directions [north east south west])
9+
10+
11+
(defconst steps '((north . (0 1))
12+
(east . (1 0))
13+
(south . (0 -1))
14+
(west . (-1 0))))
15+
16+
17+
(defun create-robot (x y direction)
18+
(record 'robot x y direction))
19+
20+
21+
(defun move (robot instructions)
22+
(let ((state robot))
23+
(seq-doseq (instruction instructions)
24+
(cond ((equal ?L instruction)
25+
(rotate state -1))
26+
((equal ?R instruction)
27+
(rotate state 1))
28+
((equal ?A instruction)
29+
(advance state))))
30+
state))
31+
32+
33+
(defun rotate (robot offset)
34+
(let* ((old-index (cl-position (aref robot 3) directions))
35+
(new-index (mod (+ old-index offset) 4)))
36+
(aset robot 3 (aref directions new-index))))
37+
38+
39+
(defun advance (robot)
40+
(let* ((delta (assq (aref robot 3) steps))
41+
(delta-x (cadr delta))
42+
(delta-y (caddr delta)))
43+
(aset robot 1 (+ delta-x (aref robot 1)))
44+
(aset robot 2 (+ delta-y (aref robot 2)))))
45+
46+
47+
(provide 'robot-simulator)
48+
;;; robot-simulator.el ends here
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
;;; robot-simulator-test.el --- Tests for robot-simulator (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "robot-simulator.el")
9+
(declare-function create-robot "robot-simulator.el" (x y direction))
10+
(declare-function move "robot-simulator.el" (robot instructions))
11+
12+
13+
(ert-deftest create-robot-at-origin-facing-north ()
14+
(should (equal #s(robot 0 0 north)
15+
(create-robot 0 0 'north))))
16+
17+
18+
(ert-deftest create-robot-at-negative-position-facing-south ()
19+
(should (equal #s(robot -1 -1 south)
20+
(create-robot -1 -1 'south))))
21+
22+
23+
(ert-deftest rotating-clockwise-changes-north-to-east ()
24+
(should (equal #s(robot 0 0 east)
25+
(move (create-robot 0 0 'north) "R"))))
26+
27+
28+
(ert-deftest rotating-clockwise-changes-east-to-south ()
29+
(should (equal #s(robot 0 0 south)
30+
(move (create-robot 0 0 'east) "R"))))
31+
32+
33+
(ert-deftest rotating-clockwise-changes-south-to-west ()
34+
(should (equal #s(robot 0 0 west)
35+
(move (create-robot 0 0 'south) "R"))))
36+
37+
38+
(ert-deftest rotating-clockwise-changes-west-to-north ()
39+
(should (equal #s(robot 0 0 north)
40+
(move (create-robot 0 0 'west) "R"))))
41+
42+
43+
(ert-deftest rotating-counter-clockwise-changes-north-to-west ()
44+
(should (equal #s(robot 0 0 west)
45+
(move (create-robot 0 0 'north) "L"))))
46+
47+
48+
(ert-deftest rotating-counter-clockwise-changes-west-to-south ()
49+
(should (equal #s(robot 0 0 south)
50+
(move (create-robot 0 0 'west) "L"))))
51+
52+
53+
(ert-deftest rotating-counter-clockwise-changes-south-to-east ()
54+
(should (equal #s(robot 0 0 east)
55+
(move (create-robot 0 0 'south) "L"))))
56+
57+
58+
(ert-deftest rotating-counter-clockwise-changes-east-to-north ()
59+
(should (equal #s(robot 0 0 north)
60+
(move (create-robot 0 0 'east) "L"))))
61+
62+
63+
(ert-deftest moving-forward-one-facing-north-increments-y ()
64+
(should (equal #s(robot 0 1 north)
65+
(move (create-robot 0 0 'north) "A"))))
66+
67+
68+
(ert-deftest moving-forward-one-facing-south-decrements-y ()
69+
(should (equal #s(robot 0 -1 south)
70+
(move (create-robot 0 0 'south) "A"))))
71+
72+
73+
(ert-deftest moving-forward-one-facing-east-increments-x ()
74+
(should (equal #s(robot 1 0 east)
75+
(move (create-robot 0 0 'east) "A"))))
76+
77+
78+
(ert-deftest moving-forward-one-facing-west-decrements-x ()
79+
(should (equal #s(robot -1 0 west)
80+
(move (create-robot 0 0 'west) "A"))))
81+
82+
83+
(ert-deftest follow-series-of-instructions-moving-east-and-north-from-README ()
84+
(should (equal #s(robot 9 4 west)
85+
(move (create-robot 7 3 'north) "RAALAL"))))
86+
87+
88+
(ert-deftest follow-series-of-instructions-moving-west-and-north ()
89+
(should (equal #s(robot -4 1 west)
90+
(move (create-robot 0 0 'north) "LAAARALA"))))
91+
92+
93+
(ert-deftest follow-series-of-instructions-moving-west-and-south ()
94+
(should (equal #s(robot -3 -8 south)
95+
(move (create-robot 2 -7 'east) "RRAAAAALA"))))
96+
97+
98+
(ert-deftest follow-series-of-instructions-moving-east-and-north ()
99+
(should (equal #s(robot 11 5 north)
100+
(move (create-robot 8 4 'south) "LAAARRRALLLL"))))
101+
102+
103+
(provide 'robot-simulator-test)
104+
;;; robot-simulator-test.el ends here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
;;; robot-simulator.el --- robot-simulator (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun create-robot (x y direction)
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
12+
(defun move (robot instructions)
13+
(error "Delete this S-Expression and write your own implementation"))
14+
15+
16+
(provide 'robot-simulator)
17+
;;; robot-simulator.el ends here

0 commit comments

Comments
 (0)