trunk
HeNine 3 years ago
parent 482dcf4295
commit b5a80b931d

@ -0,0 +1,7 @@
name = "day02"
uuid = "285bc492-0789-41a0-9f0f-a67603503559"
authors = ["HeNine "]
version = "0.1.0"
[deps]
Match = "7eb4fadd-790c-5f42-8a69-bfa0b872bfbf"

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

File diff suppressed because it is too large Load Diff

@ -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
Loading…
Cancel
Save