Sha256: c2f7bf1493c9a1aa28d0248a26c5a4e92ebb42353f1931c075d2e02408a30cc8

Contents?: true

Size: 1.99 KB

Versions: 66

Compression:

Stored size: 1.99 KB

Contents

module RobotSimulator
    exposing
        ( Bearing(East, North, South, West)
        , Robot
        , advance
        , defaultRobot
        , simulate
        , turnLeft
        , turnRight
        )

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

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.179 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.178 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.177 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.176 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.175 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.174 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.173 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.172 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.171 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.170 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.169 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.167 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.166 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.165 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.164 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.163 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.162 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.161 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.160 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm