From b5a80b931d05721b2bdd78d203b37a05ee420bb7 Mon Sep 17 00:00:00 2001 From: HeNine <> Date: Thu, 2 Dec 2021 10:57:08 +0100 Subject: [PATCH] Day 2 --- day02/Project.toml | 7 + day02/data/case00.txt | 6 + day02/data/case01.txt | 1000 +++++++++++++++++++++++++++++++++++++++++ day02/src/day02.jl | 98 ++++ 4 files changed, 1111 insertions(+) create mode 100644 day02/Project.toml create mode 100644 day02/data/case00.txt create mode 100644 day02/data/case01.txt create mode 100644 day02/src/day02.jl diff --git a/day02/Project.toml b/day02/Project.toml new file mode 100644 index 0000000..24f09e1 --- /dev/null +++ b/day02/Project.toml @@ -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" diff --git a/day02/data/case00.txt b/day02/data/case00.txt new file mode 100644 index 0000000..0ed302d --- /dev/null +++ b/day02/data/case00.txt @@ -0,0 +1,6 @@ +forward 5 +down 5 +forward 8 +up 3 +down 8 +forward 2 \ No newline at end of file diff --git a/day02/data/case01.txt b/day02/data/case01.txt new file mode 100644 index 0000000..671a4d0 --- /dev/null +++ b/day02/data/case01.txt @@ -0,0 +1,1000 @@ +forward 6 +down 2 +forward 2 +down 8 +forward 3 +down 6 +down 8 +down 9 +forward 7 +forward 8 +down 9 +down 8 +down 9 +up 8 +forward 1 +down 7 +down 3 +forward 3 +forward 1 +down 3 +forward 3 +forward 1 +up 8 +down 5 +down 1 +forward 6 +forward 2 +up 9 +down 3 +down 8 +down 3 +down 3 +up 2 +down 7 +down 3 +up 5 +forward 4 +down 9 +forward 6 +forward 3 +forward 1 +forward 3 +down 2 +up 9 +down 4 +forward 6 +down 3 +forward 2 +down 2 +up 5 +up 1 +forward 3 +forward 6 +down 6 +forward 7 +forward 1 +down 3 +down 8 +forward 2 +down 7 +up 1 +up 2 +forward 5 +down 8 +down 8 +forward 9 +forward 7 +forward 2 +forward 7 +up 6 +up 9 +down 4 +forward 4 +forward 4 +up 1 +down 7 +forward 9 +forward 3 +down 6 +down 9 +forward 7 +forward 4 +up 7 +up 6 +up 8 +down 9 +forward 1 +down 1 +forward 8 +down 7 +forward 5 +down 3 +down 3 +down 8 +down 8 +down 4 +up 4 +forward 3 +down 8 +down 9 +up 3 +up 8 +down 9 +up 5 +forward 2 +forward 5 +forward 5 +down 8 +forward 9 +forward 8 +down 5 +down 9 +forward 6 +forward 2 +forward 3 +up 1 +forward 1 +up 2 +up 2 +forward 4 +forward 8 +forward 5 +down 1 +up 4 +forward 5 +up 7 +down 5 +down 5 +forward 8 +up 2 +down 7 +down 6 +down 5 +down 5 +down 1 +down 8 +forward 9 +forward 2 +up 6 +up 4 +down 8 +forward 1 +forward 2 +down 2 +forward 7 +forward 7 +forward 3 +forward 6 +forward 8 +down 3 +forward 6 +up 5 +down 3 +down 8 +up 1 +forward 1 +down 7 +down 3 +up 5 +forward 6 +forward 8 +forward 9 +up 5 +up 5 +up 5 +forward 8 +up 5 +down 6 +down 7 +down 5 +up 7 +up 1 +up 3 +forward 8 +up 9 +down 7 +down 4 +up 6 +up 8 +up 9 +up 9 +forward 5 +up 5 +forward 2 +forward 2 +forward 6 +up 2 +down 8 +up 2 +forward 5 +down 9 +up 7 +down 9 +forward 1 +forward 8 +up 1 +forward 7 +forward 2 +down 3 +forward 3 +forward 2 +up 9 +forward 4 +forward 9 +down 9 +forward 5 +forward 1 +forward 5 +forward 8 +up 5 +forward 1 +down 4 +up 8 +up 4 +up 7 +forward 4 +down 1 +up 6 +forward 6 +down 2 +down 7 +forward 4 +up 7 +forward 7 +forward 9 +down 5 +up 5 +forward 4 +down 6 +forward 1 +up 8 +up 8 +down 8 +down 7 +forward 7 +down 3 +forward 7 +down 3 +down 5 +down 4 +up 8 +down 2 +down 2 +up 5 +forward 9 +up 9 +forward 2 +up 4 +forward 4 +down 2 +down 7 +forward 7 +down 1 +down 6 +down 4 +forward 6 +up 4 +forward 4 +down 6 +down 8 +down 3 +forward 7 +down 3 +forward 7 +down 7 +forward 4 +up 9 +down 5 +forward 7 +forward 7 +up 6 +down 3 +forward 9 +down 1 +forward 4 +up 9 +down 3 +up 9 +down 5 +up 6 +forward 1 +forward 9 +up 4 +down 3 +forward 1 +down 7 +down 2 +forward 2 +down 6 +up 4 +down 4 +up 9 +down 3 +down 9 +down 4 +down 1 +up 8 +down 2 +up 1 +forward 5 +forward 9 +forward 1 +up 4 +forward 5 +down 7 +up 6 +down 3 +forward 8 +down 1 +down 5 +forward 5 +down 5 +down 7 +down 8 +down 7 +up 6 +forward 8 +down 8 +forward 6 +down 6 +down 7 +down 3 +forward 2 +down 6 +down 8 +down 7 +down 3 +up 1 +down 7 +forward 8 +forward 2 +forward 5 +down 4 +up 4 +forward 9 +down 9 +forward 6 +down 7 +down 4 +down 8 +up 9 +forward 7 +down 4 +forward 7 +forward 1 +forward 7 +down 9 +down 7 +forward 3 +forward 3 +forward 2 +down 5 +up 5 +forward 5 +down 2 +forward 7 +forward 9 +forward 7 +down 7 +down 9 +down 5 +forward 2 +up 5 +down 3 +forward 7 +down 4 +down 3 +up 5 +down 6 +down 3 +up 4 +forward 3 +down 1 +forward 6 +forward 6 +down 8 +forward 9 +down 2 +up 3 +down 4 +down 5 +forward 3 +down 9 +forward 2 +up 3 +up 4 +forward 9 +down 2 +forward 9 +forward 3 +down 4 +down 2 +down 5 +down 4 +forward 4 +down 1 +down 9 +down 2 +forward 8 +down 5 +forward 5 +up 7 +down 5 +down 2 +forward 5 +up 4 +down 5 +up 3 +forward 7 +down 9 +forward 5 +forward 2 +forward 1 +down 7 +down 9 +down 2 +up 2 +up 2 +up 4 +down 4 +down 7 +down 3 +forward 5 +forward 3 +up 6 +down 6 +up 6 +up 9 +forward 8 +forward 4 +up 3 +forward 1 +forward 2 +up 5 +forward 5 +forward 8 +forward 7 +forward 4 +down 1 +down 8 +down 1 +forward 3 +up 1 +forward 7 +forward 4 +down 8 +forward 7 +forward 9 +forward 3 +down 9 +down 9 +down 3 +up 6 +up 1 +down 4 +forward 5 +forward 4 +forward 6 +forward 8 +down 6 +down 3 +forward 5 +forward 6 +down 4 +down 2 +up 3 +down 3 +down 7 +down 5 +down 5 +forward 6 +down 4 +forward 1 +up 2 +forward 3 +down 1 +down 4 +down 9 +down 7 +down 9 +forward 9 +down 6 +down 3 +down 2 +down 5 +up 8 +forward 5 +forward 5 +forward 4 +up 5 +forward 1 +down 9 +down 1 +up 5 +forward 8 +forward 6 +forward 5 +down 1 +up 5 +down 8 +up 7 +down 8 +down 2 +down 3 +forward 2 +up 4 +down 6 +up 6 +down 3 +down 7 +up 3 +forward 4 +down 3 +forward 4 +up 9 +forward 5 +down 2 +forward 7 +forward 5 +up 3 +up 2 +forward 2 +down 8 +down 1 +down 3 +up 5 +down 4 +forward 4 +down 1 +forward 9 +down 3 +down 7 +down 4 +down 4 +forward 7 +up 5 +forward 4 +down 8 +up 4 +forward 6 +down 1 +up 4 +forward 4 +down 6 +up 5 +up 1 +forward 2 +down 5 +forward 8 +forward 6 +down 8 +down 7 +down 7 +down 1 +forward 5 +forward 7 +forward 7 +forward 7 +up 3 +forward 9 +forward 1 +down 9 +forward 4 +up 8 +forward 1 +forward 5 +forward 4 +down 2 +forward 4 +forward 9 +forward 3 +down 1 +forward 4 +forward 9 +forward 5 +down 5 +down 5 +forward 7 +down 3 +forward 4 +down 6 +forward 7 +down 2 +down 1 +down 5 +forward 4 +forward 9 +down 4 +forward 2 +down 8 +up 5 +down 9 +forward 8 +down 3 +up 6 +down 2 +down 4 +forward 4 +up 2 +down 4 +down 4 +up 7 +down 6 +forward 4 +down 7 +forward 3 +down 1 +up 1 +down 2 +down 6 +down 4 +up 3 +down 6 +up 2 +down 6 +forward 3 +down 9 +forward 5 +down 5 +down 9 +down 9 +down 7 +forward 9 +forward 8 +forward 9 +up 9 +forward 7 +forward 4 +forward 4 +up 5 +forward 2 +down 1 +up 9 +forward 2 +forward 7 +forward 1 +down 9 +forward 9 +up 8 +up 1 +up 7 +up 7 +down 5 +forward 2 +forward 8 +forward 6 +down 7 +forward 1 +down 9 +down 4 +down 4 +down 1 +up 7 +forward 4 +forward 6 +up 5 +forward 2 +down 9 +down 7 +forward 1 +forward 2 +down 5 +forward 3 +forward 8 +forward 6 +forward 3 +forward 2 +down 1 +forward 1 +forward 1 +forward 3 +down 9 +up 9 +down 9 +down 6 +forward 7 +down 6 +forward 9 +down 9 +down 7 +down 1 +down 9 +up 9 +down 6 +forward 9 +down 6 +forward 3 +down 8 +up 5 +forward 5 +forward 8 +up 3 +down 8 +up 6 +forward 4 +down 2 +forward 6 +down 9 +forward 6 +forward 4 +forward 9 +forward 3 +down 2 +down 4 +forward 5 +down 9 +up 7 +forward 4 +up 1 +forward 1 +down 6 +forward 3 +forward 7 +forward 2 +forward 2 +down 5 +down 9 +down 3 +down 5 +up 3 +forward 1 +down 2 +down 4 +down 1 +up 9 +up 5 +up 1 +down 1 +up 9 +down 5 +up 3 +up 3 +down 7 +forward 4 +down 6 +forward 2 +forward 7 +forward 4 +down 2 +forward 6 +forward 2 +down 3 +up 3 +up 9 +forward 9 +forward 9 +forward 6 +down 8 +down 1 +forward 9 +up 1 +down 6 +forward 6 +up 5 +forward 2 +forward 6 +down 9 +forward 1 +forward 8 +down 8 +forward 4 +forward 7 +up 6 +up 1 +forward 7 +forward 3 +forward 2 +down 4 +down 7 +down 7 +down 1 +down 6 +forward 1 +down 9 +up 9 +up 9 +down 2 +down 2 +forward 5 +up 2 +forward 7 +up 5 +down 9 +forward 7 +forward 2 +down 8 +up 1 +down 5 +forward 6 +down 8 +down 7 +forward 4 +up 2 +down 8 +forward 2 +down 5 +down 4 +down 9 +down 1 +down 9 +down 6 +down 3 +forward 1 +forward 6 +up 1 +up 1 +up 9 +down 2 +down 2 +forward 5 +down 3 +forward 4 +down 3 +down 7 +down 7 +forward 4 +up 3 +forward 4 +down 3 +forward 8 +forward 1 +up 2 +up 1 +forward 1 +down 6 +down 1 +down 3 +forward 7 +down 7 +forward 4 +forward 5 +forward 3 +down 5 +forward 9 +forward 5 +down 7 +forward 6 +down 4 +down 4 +down 9 +down 3 +up 9 +forward 7 +down 7 +forward 6 +down 2 +down 9 +forward 4 +forward 1 +forward 4 +down 5 +forward 7 +down 9 +down 8 +forward 9 +forward 1 +down 9 +forward 6 +up 5 +forward 9 +down 1 +down 5 +forward 4 +forward 5 +forward 8 +down 5 +forward 9 +down 6 +down 2 +up 4 +up 8 +forward 3 +forward 4 +down 3 +forward 4 +up 6 +forward 3 +forward 8 +forward 7 +down 1 +down 9 +down 8 +down 8 +down 1 +forward 9 +up 4 +down 5 +forward 7 +down 8 +down 3 +forward 9 +down 5 +forward 7 +forward 2 +down 4 +forward 2 +forward 7 +down 6 +forward 7 +down 2 +forward 9 +down 9 +forward 8 +forward 8 +down 6 +forward 7 +down 8 +forward 7 +forward 3 +down 1 +up 8 +down 5 +down 6 +up 5 +forward 5 +forward 5 +up 5 +up 3 +up 7 +down 6 +forward 8 +forward 4 +down 2 +up 5 +forward 8 +down 6 +forward 4 +forward 2 +up 8 +down 8 +down 5 +down 4 +forward 9 +forward 9 +forward 6 +forward 6 +down 3 +up 1 +down 4 +down 8 +down 9 +down 1 +forward 3 +forward 1 +down 9 +down 3 +down 7 +forward 6 +forward 9 +down 8 +down 8 +forward 6 +forward 1 +down 3 +forward 1 +down 8 +down 3 +down 9 +up 1 +forward 6 +up 2 +down 3 +forward 4 +forward 2 +up 2 +down 5 +forward 1 +down 3 +forward 9 +forward 4 +forward 6 +down 3 +forward 7 +down 6 +up 3 +up 7 +up 5 +down 4 +forward 4 +up 1 +forward 7 +up 9 +forward 3 +up 1 +down 3 +down 4 +forward 4 +up 3 +down 6 +down 9 +down 6 +forward 4 +down 9 +down 6 +forward 4 +forward 3 +down 3 +up 7 +down 9 +forward 8 \ No newline at end of file diff --git a/day02/src/day02.jl b/day02/src/day02.jl new file mode 100644 index 0000000..70576f3 --- /dev/null +++ b/day02/src/day02.jl @@ -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 \ No newline at end of file