-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Instructions | ||
|
||
Write a robot simulator. | ||
|
||
A robot factory's test facility needs a program to verify robot movements. | ||
|
||
The robots have three possible movements: | ||
|
||
- turn right | ||
- turn left | ||
- advance | ||
|
||
Robots are placed on a hypothetical infinite grid, facing a particular direction (north, east, south, or west) at a set of {x,y} coordinates, | ||
e.g., {3,8}, with coordinates increasing to the north and east. | ||
|
||
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. | ||
|
||
- The letter-string "RAALAL" means: | ||
- Turn right | ||
- Advance twice | ||
- Turn left | ||
- Advance once | ||
- Turn left yet again | ||
- Say a robot starts at {7, 3} facing north. | ||
Then running this stream of instructions should leave it at {9, 4} facing west. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"authors": [ | ||
"BNAndras" | ||
], | ||
"files": { | ||
"solution": [ | ||
"robot-simulator.el" | ||
], | ||
"test": [ | ||
"robot-simulator-test.el" | ||
], | ||
"example": [ | ||
".meta/example.el" | ||
] | ||
}, | ||
"blurb": "Write a robot simulator.", | ||
"source": "Inspired by an interview question at a famous company." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
;;; robot-simulator.el --- robot-simulator Exercise (exercism) -*- lexical-binding: t; -*- | ||
|
||
;;; Commentary: | ||
|
||
;;; Code: | ||
|
||
|
||
(defconst directions [north east south west]) | ||
|
||
|
||
(defconst steps '((north . (0 1)) | ||
(east . (1 0)) | ||
(south . (0 -1)) | ||
(west . (-1 0)))) | ||
|
||
|
||
(defun create-robot (x y direction) | ||
(record 'robot x y direction)) | ||
|
||
|
||
(defun move (robot instructions) | ||
(let ((state robot)) | ||
(seq-doseq (instruction instructions) | ||
(cond ((equal ?L instruction) | ||
(rotate state -1)) | ||
((equal ?R instruction) | ||
(rotate state 1)) | ||
((equal ?A instruction) | ||
(advance state)))) | ||
state)) | ||
|
||
|
||
(defun rotate (robot offset) | ||
(let* ((old-index (cl-position (aref robot 3) directions)) | ||
(new-index (mod (+ old-index offset) 4))) | ||
(aset robot 3 (aref directions new-index)))) | ||
|
||
|
||
(defun advance (robot) | ||
(let* ((delta (assq (aref robot 3) steps)) | ||
(delta-x (cadr delta)) | ||
(delta-y (caddr delta))) | ||
(aset robot 1 (+ delta-x (aref robot 1))) | ||
(aset robot 2 (+ delta-y (aref robot 2))))) | ||
|
||
|
||
(provide 'robot-simulator) | ||
;;; robot-simulator.el ends here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[c557c16d-26c1-4e06-827c-f6602cd0785c] | ||
description = "Create robot -> at origin facing north" | ||
|
||
[bf0dffce-f11c-4cdb-8a5e-2c89d8a5a67d] | ||
description = "Create robot -> at negative position facing south" | ||
|
||
[8cbd0086-6392-4680-b9b9-73cf491e67e5] | ||
description = "Rotating clockwise -> changes north to east" | ||
|
||
[8abc87fc-eab2-4276-93b7-9c009e866ba1] | ||
description = "Rotating clockwise -> changes east to south" | ||
|
||
[3cfe1b85-bbf2-4bae-b54d-d73e7e93617a] | ||
description = "Rotating clockwise -> changes south to west" | ||
|
||
[5ea9fb99-3f2c-47bd-86f7-46b7d8c3c716] | ||
description = "Rotating clockwise -> changes west to north" | ||
|
||
[fa0c40f5-6ba3-443d-a4b3-58cbd6cb8d63] | ||
description = "Rotating counter-clockwise -> changes north to west" | ||
|
||
[da33d734-831f-445c-9907-d66d7d2a92e2] | ||
description = "Rotating counter-clockwise -> changes west to south" | ||
|
||
[bd1ca4b9-4548-45f4-b32e-900fc7c19389] | ||
description = "Rotating counter-clockwise -> changes south to east" | ||
|
||
[2de27b67-a25c-4b59-9883-bc03b1b55bba] | ||
description = "Rotating counter-clockwise -> changes east to north" | ||
|
||
[f0dc2388-cddc-4f83-9bed-bcf46b8fc7b8] | ||
description = "Moving forward one -> facing north increments Y" | ||
|
||
[2786cf80-5bbf-44b0-9503-a89a9c5789da] | ||
description = "Moving forward one -> facing south decrements Y" | ||
|
||
[84bf3c8c-241f-434d-883d-69817dbd6a48] | ||
description = "Moving forward one -> facing east increments X" | ||
|
||
[bb69c4a7-3bbf-4f64-b415-666fa72d7b04] | ||
description = "Moving forward one -> facing west decrements X" | ||
|
||
[e34ac672-4ed4-4be3-a0b8-d9af259cbaa1] | ||
description = "Follow series of instructions -> moving east and north from README" | ||
|
||
[f30e4955-4b47-4aa3-8b39-ae98cfbd515b] | ||
description = "Follow series of instructions -> moving west and north" | ||
|
||
[3e466bf6-20ab-4d79-8b51-264165182fca] | ||
description = "Follow series of instructions -> moving west and south" | ||
|
||
[41f0bb96-c617-4e6b-acff-a4b279d44514] | ||
description = "Follow series of instructions -> moving east and north" |
104 changes: 104 additions & 0 deletions
104
exercises/practice/robot-simulator/robot-simulator-test.el
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
;;; robot-simulator-test.el --- Tests for robot-simulator (exercism) -*- lexical-binding: t; -*- | ||
|
||
;;; Commentary: | ||
|
||
;;; Code: | ||
|
||
|
||
(load-file "robot-simulator.el") | ||
(declare-function create-robot "robot-simulator.el" (x y direction)) | ||
(declare-function move "robot-simulator.el" (robot instructions)) | ||
|
||
|
||
(ert-deftest create-robot-at-origin-facing-north () | ||
(should (equal #s(robot 0 0 north) | ||
(create-robot 0 0 'north)))) | ||
|
||
|
||
(ert-deftest create-robot-at-negative-position-facing-south () | ||
(should (equal #s(robot -1 -1 south) | ||
(create-robot -1 -1 'south)))) | ||
|
||
|
||
(ert-deftest rotating-clockwise-changes-north-to-east () | ||
(should (equal #s(robot 0 0 east) | ||
(move (create-robot 0 0 'north) "R")))) | ||
|
||
|
||
(ert-deftest rotating-clockwise-changes-east-to-south () | ||
(should (equal #s(robot 0 0 south) | ||
(move (create-robot 0 0 'east) "R")))) | ||
|
||
|
||
(ert-deftest rotating-clockwise-changes-south-to-west () | ||
(should (equal #s(robot 0 0 west) | ||
(move (create-robot 0 0 'south) "R")))) | ||
|
||
|
||
(ert-deftest rotating-clockwise-changes-west-to-north () | ||
(should (equal #s(robot 0 0 north) | ||
(move (create-robot 0 0 'west) "R")))) | ||
|
||
|
||
(ert-deftest rotating-counter-clockwise-changes-north-to-west () | ||
(should (equal #s(robot 0 0 west) | ||
(move (create-robot 0 0 'north) "L")))) | ||
|
||
|
||
(ert-deftest rotating-counter-clockwise-changes-west-to-south () | ||
(should (equal #s(robot 0 0 south) | ||
(move (create-robot 0 0 'west) "L")))) | ||
|
||
|
||
(ert-deftest rotating-counter-clockwise-changes-south-to-east () | ||
(should (equal #s(robot 0 0 east) | ||
(move (create-robot 0 0 'south) "L")))) | ||
|
||
|
||
(ert-deftest rotating-counter-clockwise-changes-east-to-north () | ||
(should (equal #s(robot 0 0 north) | ||
(move (create-robot 0 0 'east) "L")))) | ||
|
||
|
||
(ert-deftest moving-forward-one-facing-north-increments-y () | ||
(should (equal #s(robot 0 1 north) | ||
(move (create-robot 0 0 'north) "A")))) | ||
|
||
|
||
(ert-deftest moving-forward-one-facing-south-decrements-y () | ||
(should (equal #s(robot 0 -1 south) | ||
(move (create-robot 0 0 'south) "A")))) | ||
|
||
|
||
(ert-deftest moving-forward-one-facing-east-increments-x () | ||
(should (equal #s(robot 1 0 east) | ||
(move (create-robot 0 0 'east) "A")))) | ||
|
||
|
||
(ert-deftest moving-forward-one-facing-west-decrements-x () | ||
(should (equal #s(robot -1 0 west) | ||
(move (create-robot 0 0 'west) "A")))) | ||
|
||
|
||
(ert-deftest follow-series-of-instructions-moving-east-and-north-from-README () | ||
(should (equal #s(robot 9 4 west) | ||
(move (create-robot 7 3 'north) "RAALAL")))) | ||
|
||
|
||
(ert-deftest follow-series-of-instructions-moving-west-and-north () | ||
(should (equal #s(robot -4 1 west) | ||
(move (create-robot 0 0 'north) "LAAARALA")))) | ||
|
||
|
||
(ert-deftest follow-series-of-instructions-moving-west-and-south () | ||
(should (equal #s(robot -3 -8 south) | ||
(move (create-robot 2 -7 'east) "RRAAAAALA")))) | ||
|
||
|
||
(ert-deftest follow-series-of-instructions-moving-east-and-north () | ||
(should (equal #s(robot 11 5 north) | ||
(move (create-robot 8 4 'south) "LAAARRRALLLL")))) | ||
|
||
|
||
(provide 'robot-simulator-test) | ||
;;; robot-simulator-test.el ends here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
;;; robot-simulator.el --- robot-simulator (exercism) -*- lexical-binding: t; -*- | ||
|
||
;;; Commentary: | ||
|
||
;;; Code: | ||
|
||
|
||
(defun create-robot (x y direction) | ||
(error "Delete this S-Expression and write your own implementation")) | ||
|
||
|
||
(defun move (robot instructions) | ||
(error "Delete this S-Expression and write your own implementation")) | ||
|
||
|
||
(provide 'robot-simulator) | ||
;;; robot-simulator.el ends here |