Add robot-simulator exercise

This commit is contained in:
Erik Simmler 2016-03-28 06:23:01 -04:00
parent 03be35553d
commit 46782f1552
6 changed files with 250 additions and 2 deletions

View file

@ -24,7 +24,8 @@
"nucleotide-count",
"phone-number",
"grade-school",
"allergies"
"allergies",
"robot-simulator"
],
"deprecated": [

View file

@ -25,7 +25,8 @@
"./exercises/nucleotide-count",
"./exercises/phone-number",
"./exercises/grade-school",
"./exercises/allergies"
"./exercises/allergies",
"./exercises/robot-simulator"
],
"exposed-modules": [],
"dependencies": {

View file

@ -0,0 +1 @@
module RobotSimulator (..) where

View file

@ -0,0 +1,98 @@
module RobotSimulator (..) where
import String
type Bearing
= North
| East
| South
| West
type alias Robot =
{ bearing : Bearing
, coordinates : { x : Int, y : Int }
}
defaultRobot : Robot
defaultRobot =
{ bearing = North
, coordinates = { x = 0, y = 0 }
}
turnRight : Robot -> Robot
turnRight robot =
case robot.bearing of
North ->
{ robot | bearing = East }
East ->
{ robot | bearing = South }
South ->
{ robot | bearing = West }
West ->
{ robot | bearing = North }
turnLeft : Robot -> Robot
turnLeft robot =
case robot.bearing of
North ->
{ robot | bearing = West }
West ->
{ robot | bearing = South }
South ->
{ robot | bearing = East }
East ->
{ robot | bearing = North }
advance : Robot -> Robot
advance { bearing, coordinates } =
let
updated =
case bearing of
North ->
{ coordinates | y = coordinates.y + 1 }
East ->
{ coordinates | x = coordinates.x + 1 }
South ->
{ coordinates | y = coordinates.y - 1 }
West ->
{ coordinates | x = coordinates.x - 1 }
in
{ bearing = bearing, coordinates = updated }
simulate : String -> Robot -> Robot
simulate directions robot =
let
action direction =
case direction of
'L' ->
turnLeft
'R' ->
turnRight
'A' ->
advance
_ ->
identity
in
directions
|> String.toList
|> List.map action
|> List.foldl (\a r -> a r) robot

View file

@ -0,0 +1,131 @@
module Main (..) where
import Task
import Console
import ElmTest exposing (..)
import RobotSimulator exposing (defaultRobot, Robot, Bearing(North, East, West, South), turnRight, turnLeft, advance, simulate)
tests : Test
tests =
suite
"RobotSimulator"
[ suite
"init"
(let
robot =
defaultRobot
in
[ test "coordinates" (assertEqual { x = 0, y = 0 } robot.coordinates)
, test "bearing" (assertEqual North robot.bearing)
]
)
, suite
"setup"
(let
robot =
Robot South { x = -1, y = 1 }
in
[ test "coordinates" (assertEqual { x = -1, y = 1 } robot.coordinates)
, test "bearing" (assertEqual South robot.bearing)
]
)
, suite
"turn right"
([1..3]
|> List.scanl (\_ r -> turnRight r) defaultRobot
|> List.map .bearing
|> assertionList [ North, East, South, West ]
|> List.map defaultTest
)
, suite
"turn left"
([1..3]
|> List.scanl (\_ r -> turnLeft r) defaultRobot
|> List.map .bearing
|> assertionList [ North, West, South, East ]
|> List.map defaultTest
)
, suite
"advance positive north"
(let
robot =
Robot North { x = 0, y = 0 }
|> advance
in
[ test "coordinates" (assertEqual { x = 0, y = 1 } robot.coordinates)
, test "bearing" (assertEqual North robot.bearing)
]
)
, suite
"advance positive east"
(let
robot =
Robot East { x = 0, y = 0 }
|> advance
in
[ test "coordinates" (assertEqual { x = 1, y = 0 } robot.coordinates)
, test "bearing" (assertEqual East robot.bearing)
]
)
, suite
"advance negative south"
(let
robot =
Robot South { x = 0, y = 0 }
|> advance
in
[ test "coordinates" (assertEqual { x = 0, y = -1 } robot.coordinates)
, test "bearing" (assertEqual South robot.bearing)
]
)
, suite
"advance positive west"
(let
robot =
Robot West { x = 0, y = 0 }
|> advance
in
[ test "coordinates" (assertEqual { x = -1, y = 0 } robot.coordinates)
, test "bearing" (assertEqual West robot.bearing)
]
)
, suite
"simulate prog 1"
(let
robot =
Robot North { x = 0, y = 0 }
|> simulate "LAAARALA"
in
[ test "coordinates" (assertEqual { x = -4, y = 1 } robot.coordinates)
, test "bearing" (assertEqual West robot.bearing)
]
)
, suite
"simulate prog 2"
(let
robot =
Robot East { x = 2, y = -7 }
|> simulate "RRAAAAALA"
in
[ test "coordinates" (assertEqual { x = -3, y = -8 } robot.coordinates)
, test "bearing" (assertEqual South robot.bearing)
]
)
, suite
"simulate prog 3"
(let
robot =
Robot South { x = 8, y = 4 }
|> simulate "LAAARRRALLLL"
in
[ test "coordinates" (assertEqual { x = 11, y = 5 } robot.coordinates)
, test "bearing" (assertEqual North robot.bearing)
]
)
]
port runner : Signal (Task.Task x ())
port runner =
Console.run (consoleRunner tests)

View file

@ -0,0 +1,16 @@
{
"version": "1.0.0",
"summary": "Exercism problems in Elm.",
"repository": "https://github.com/exercism/xelm.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
"elm-lang/core": "2.0.0 <= v < 4.0.0",
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
},
"elm-version": "0.15.0 <= v < 0.17.0"
}