Sha256: bbca7d1523128f72a4ecffb7a2d1cb23c714a3975ca48ea61aa6601cfb9e8933

Contents?: true

Size: 1.05 KB

Versions: 141

Compression:

Stored size: 1.05 KB

Contents

import Base: ==

mutable struct Point{T<:Number}
    x::T
    y::T
end

==(p::Point, q::Point) = p.x == q.x && p.y == q.y

@enum Heading NORTH=1 EAST=2 SOUTH=3 WEST=4

mutable struct Robot
    position::Point
    heading::Heading
end

function Robot(position::Tuple{T, T}, heading::Heading) where T<:Number
    Robot(Point(position...), heading)
end

heading(r::Robot) = r.heading
position(r::Robot) = r.position

function turn_right!(r::Robot)
    r.heading = Heading(mod1(Int(r.heading) + 1, 4))
    r
end

function turn_left!(r::Robot)
    r.heading = Heading(mod1(Int(r.heading) - 1, 4))
    r
end

function advance!(r::Robot)
    if heading(r) == NORTH
        r.position.y += 1
    elseif heading(r) == SOUTH
        r.position.y -= 1
    elseif heading(r) == EAST
        r.position.x += 1
    else
        r.position.x -= 1
    end
    r
end

function move!(r::Robot, instructions::AbstractString)
    moves = Dict('A' => advance!, 'R' => turn_right!, 'L' => turn_left!)
    for move in instructions
        move in keys(moves) && moves[move](r)
    end
    r
end

Version data entries

141 entries across 141 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.138 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.137 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.136 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.135 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.134 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.133 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.132 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.131 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.130 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.129 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.128 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.127 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.126 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.125 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.124 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.123 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.122 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.121 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.120 tracks/julia/exercises/robot-simulator/example.jl