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.159 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.158 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.157 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.156 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.155 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.154 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.153 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.152 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.151 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.150 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.149 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.148 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.147 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.146 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.145 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.144 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.143 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.142 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.141 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.140 tracks/julia/exercises/robot-simulator/example.jl