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.58 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.57 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.56 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.55 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.54 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.53 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.52 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.51 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.50 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.49 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.48 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.47 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.46 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.45 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.44 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.43 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.42 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.41 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.40 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.39 tracks/julia/exercises/robot-simulator/example.jl