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.78 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.77 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.76 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.75 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.74 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.73 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.72 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.71 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.70 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.69 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.68 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.67 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.66 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.65 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.64 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.63 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.62 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.61 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.60 tracks/julia/exercises/robot-simulator/example.jl
trackler-2.2.1.59 tracks/julia/exercises/robot-simulator/example.jl