Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robot simulator for Racket #358

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
"uuid": "0abd9912-29e0-4eef-bd7d-0a17a9d3bfe3",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
]
},
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/robot-simulator/.docs/instructions.md
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.
16 changes: 16 additions & 0 deletions exercises/practice/robot-simulator/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"authors": ["blakelewis"],
"files": {
"solution": [
"robot-simulator.rkt"
],
"test": [
"robot-simulator-test.rkt"
],
"example": [
".meta/example.rkt"
]
},
"blurb": "Write a robot simulator.",
"source": "Inspired by an interview question at a famous company."
}
34 changes: 34 additions & 0 deletions exercises/practice/robot-simulator/.meta/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#lang racket

(provide robot%)

(define dirs '(north east south west))
(define step (hash 'north '(0 1) 'east '(1 0) 'south '(0 -1) 'west '(-1 0)))

(define robot%
(class object%
(super-new)
(init-field [position '(0 0)]
[direction 'north])

(define (turn t)
(let* ([old-dir (index-of dirs direction)]
[new-dir (modulo (+ old-dir t) 4)])
(set! direction (list-ref dirs new-dir))))

(define (move-step point delta)
(let ([x (+ (car point) (car delta))]
[y (+ (cadr point) (cadr delta))])
(list x y)))

(define (advance)
(let ([s (hash-ref step direction)])
(set! position (move-step position s))))

(define/public (move instructions)
(for ([cmd (in-string instructions)])
(cond [(eq? cmd #\L) (turn -1)]
[(eq? cmd #\R) (turn 1)]
[(eq? cmd #\A) (advance)])))))


64 changes: 64 additions & 0 deletions exercises/practice/robot-simulator/.meta/tests.toml
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"
118 changes: 118 additions & 0 deletions exercises/practice/robot-simulator/robot-simulator-test.rkt
BNAndras marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#lang racket/base

(require racket/class)
(require "robot-simulator.rkt")

(module+ test
(require rackunit rackunit/text-ui))

(module+ test
(define suite
(test-suite "robot simulator tests"
(test-case "at origin facing north"
(define robot (new robot% [position '(0 0)] [direction 'north]))
(check-equal? (get-field position robot) '(0 0))
(check-equal? (get-field direction robot) 'north))

(test-case "at negative position facing south"
(define robot (new robot% [position '(-1 -1)] [direction 'south]))
(check-equal? (get-field position robot) '(-1 -1))
(check-equal? (get-field direction robot) 'south))

(test-case "changes north to east"
(define robot (new robot% [position '(0 0)] [direction 'north]))
(send robot move "R")
(check-equal? (get-field position robot) '(0 0))
(check-equal? (get-field direction robot) 'east))

(test-case "changes east to south"
(define robot (new robot% [position '(0 0)] [direction 'east]))
(send robot move "R")
(check-equal? (get-field position robot) '(0 0))
(check-equal? (get-field direction robot) 'south))

(test-case "changes south to west"
(define robot (new robot% [position '(0 0)] [direction 'south]))
(send robot move "R")
(check-equal? (get-field position robot) '(0 0))
(check-equal? (get-field direction robot) 'west))

(test-case "changes west to north"
(define robot (new robot% [position '(0 0)] [direction 'west]))
(send robot move "R")
(check-equal? (get-field position robot) '(0 0))
(check-equal? (get-field direction robot) 'north))

(test-case "changes north to west"
(define robot (new robot% [position '(0 0)] [direction 'north]))
(send robot move "L")
(check-equal? (get-field position robot) '(0 0))
(check-equal? (get-field direction robot) 'west))

(test-case "changes west to south"
(define robot (new robot% [position '(0 0)] [direction 'west]))
(send robot move "L")
(check-equal? (get-field position robot) '(0 0))
(check-equal? (get-field direction robot) 'south))

(test-case "changes south to east"
(define robot (new robot% [position '(0 0)] [direction 'south]))
(send robot move "L")
(check-equal? (get-field position robot) '(0 0))
(check-equal? (get-field direction robot) 'east))

(test-case "changes east to north"
(define robot (new robot% [position '(0 0)] [direction 'east]))
(send robot move "L")
(check-equal? (get-field position robot) '(0 0))
(check-equal? (get-field direction robot) 'north))

(test-case "facing north increments Y"
(define robot (new robot% [position '(0 0)] [direction 'north]))
(send robot move "A")
(check-equal? (get-field position robot) '(0 1))
(check-equal? (get-field direction robot) 'north))

(test-case "facing south decrements Y"
(define robot (new robot% [position '(0 0)] [direction 'south]))
(send robot move "A")
(check-equal? (get-field position robot) '(0 -1))
(check-equal? (get-field direction robot) 'south))

(test-case "facing east increments X"
(define robot (new robot% [position '(0 0)] [direction 'east]))
(send robot move "A")
(check-equal? (get-field position robot) '(1 0))
(check-equal? (get-field direction robot) 'east))

(test-case "facing west decrements X"
(define robot (new robot% [position '(0 0)] [direction 'west]))
(send robot move "A")
(check-equal? (get-field position robot) '(-1 0))
(check-equal? (get-field direction robot) 'west))

(test-case "moving east and north from README"
(define robot (new robot% [position '(7 3)] [direction 'north]))
(send robot move "RAALAL")
(check-equal? (get-field position robot) '(9 4))
(check-equal? (get-field direction robot) 'west))

(test-case "moving west and north"
(define robot (new robot% [position '(0 0)] [direction 'north]))
(send robot move "LAAARALA")
(check-equal? (get-field position robot) '(-4 1))
(check-equal? (get-field direction robot) 'west))

(test-case "moving west and south"
(define robot (new robot% [position '(2 -7)] [direction 'east]))
(send robot move "RRAAAAALA")
(check-equal? (get-field position robot) '(-3 -8))
(check-equal? (get-field direction robot) 'south))

(test-case "moving east and north"
(define robot (new robot% [position '(8 4)] [direction 'south]))
(send robot move "LAAARRRALLLL")
(check-equal? (get-field position robot) '(11 5))
(check-equal? (get-field direction robot) 'north))))

(run-tests suite))
7 changes: 7 additions & 0 deletions exercises/practice/robot-simulator/robot-simulator.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#lang racket

(provide robot%)

(define robot%
(class object%
(error "Not implemented yet")))