aoc2021/2.jl

52 lines
1.1 KiB
Julia

using Pkg
Pkg.add("Match")
using Match
include("utils.jl")
function solution2_1()
input_lines = non_empty_lines("2.data")
function parse_line(line)
(command, amount) = split(line, " ")
parsed_amount = parse(Int, amount)
end
map(fline->split(line, " "), input_lines)
x = 0
y = 0
for line in input_lines
(command, amount) = split(line, " ")
parsed_amount = parse(Int, amount)
@match command begin
"forward" => begin x += parsed_amount end
"up" => begin y -= parsed_amount end
"down" => begin y += parsed_amount end
end
end
(x, y, x*y)
end
function solution2_2()
input_lines = non_empty_lines("2.data")
aim = 0
x = 0
y = 0
for line in input_lines
(command, amount) = split(line, " ")
parsed_amount = parse(Int, amount)
@match command begin
"forward" => begin x += parsed_amount; y += parsed_amount * aim end
"up" => begin aim -= parsed_amount end
"down" => begin aim += parsed_amount end
end
end
(x, y, x*y)
end
(solution2_1(), solution2_2())