Sha256: a5f2d12d4e33e49ae67c13a81ddb09617c1b8a5f6561809f5f90531dfd2c050d

Contents?: true

Size: 1.85 KB

Versions: 322

Compression:

Stored size: 1.85 KB

Contents

module RobotSimulator exposing (..)

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

322 entries across 322 versions & 1 rubygems

Version Path
trackler-2.2.1.104 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.103 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.102 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.101 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.100 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.99 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.98 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.97 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.96 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.95 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.94 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.93 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.92 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.91 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.90 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.89 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.88 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.87 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.86 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm
trackler-2.2.1.85 tracks/elm/exercises/robot-simulator/RobotSimulator.example.elm