|
|
|
@ -0,0 +1,98 @@
|
|
|
|
|
import Base.+
|
|
|
|
|
|
|
|
|
|
@enum SubCommandWord::Int forward = 7 up = 2 down = 4
|
|
|
|
|
|
|
|
|
|
convert(::Type{SubCommandWord}, x::Int64) = SubCommandWord(x)
|
|
|
|
|
|
|
|
|
|
struct SubCommand
|
|
|
|
|
cmd::SubCommandWord
|
|
|
|
|
arg::Int
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
SubCommand(cmd_line::String) =
|
|
|
|
|
let cmd_arg = split(cmd_line, " ")
|
|
|
|
|
SubCommand(SubCommandWord(length(cmd_arg[1])), parse(Int, cmd_arg[2]))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
struct Sub
|
|
|
|
|
hpos::Int
|
|
|
|
|
vpos::Int
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
convert(::Type{Vector{Int64}}, sub::Sub) = [sub.hpos, sub.vpos]
|
|
|
|
|
|
|
|
|
|
Sub() = Sub(0, 0)
|
|
|
|
|
|
|
|
|
|
SubMoves = Dict(forward => [1, 0], up => [0, -1], down => [0, 1]);
|
|
|
|
|
|
|
|
|
|
move_sub(sub::Sub, command::SubCommand) = Sub((convert(Vector{Int64}, sub) + SubMoves[command.cmd] * command.arg)...)
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
macro process_input(T,V)
|
|
|
|
|
quote
|
|
|
|
|
(file) -> begin
|
|
|
|
|
sub = reduce(
|
|
|
|
|
(sub, move) -> move_sub(sub, $V(move)),
|
|
|
|
|
readlines(file),
|
|
|
|
|
init = $T()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sub.hpos * sub.vpos
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
println("Day 02 – Task 1 – Case 0")
|
|
|
|
|
open(@process_input(Sub, SubCommand), "day02\\data\\case00.txt", "r") |> println
|
|
|
|
|
|
|
|
|
|
println("Day 02 – Task 1 – Case 1")
|
|
|
|
|
open(@process_input(Sub, SubCommand), "day02\\data\\case01.txt", "r") |> println
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
using Match
|
|
|
|
|
|
|
|
|
|
struct Sub2
|
|
|
|
|
hpos::Int
|
|
|
|
|
vpos::Int
|
|
|
|
|
aim::Int
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Sub2() = Sub2(0, 0, 0)
|
|
|
|
|
|
|
|
|
|
abstract type SubCommand2 end
|
|
|
|
|
|
|
|
|
|
struct Up <: SubCommand2
|
|
|
|
|
arg::Int
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
struct Down <: SubCommand2
|
|
|
|
|
arg::Int
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
struct Forward <: SubCommand2
|
|
|
|
|
arg::Int
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
SubCommand2(cmd_line::String) =
|
|
|
|
|
let cmd_arg = split(cmd_line, " ")
|
|
|
|
|
@match cmd_arg[1] begin
|
|
|
|
|
"up" => Up(parse(Int, cmd_arg[2]))
|
|
|
|
|
"down" => Down(parse(Int, cmd_arg[2]))
|
|
|
|
|
"forward" => Forward(parse(Int, cmd_arg[2]))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
move_sub(sub::Sub2, cmd::Up) = Sub2(sub.hpos, sub.vpos, sub.aim - cmd.arg)
|
|
|
|
|
move_sub(sub::Sub2, cmd::Down) = Sub2(sub.hpos, sub.vpos, sub.aim + cmd.arg)
|
|
|
|
|
move_sub(sub::Sub2, cmd::Forward) = Sub2(sub.hpos + cmd.arg, sub.vpos + cmd.arg * sub.aim, sub.aim)
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
println("Day 02 – Task 1 – Case 0")
|
|
|
|
|
open(@process_input(Sub2, SubCommand2), "day02\\data\\case00.txt", "r") |> println
|
|
|
|
|
|
|
|
|
|
println("Day 02 – Task 1 – Case 1")
|
|
|
|
|
open(@process_input(Sub2, SubCommand2), "day02\\data\\case01.txt", "r") |> println
|