solution day 2

This commit is contained in:
Huanzo 2021-12-02 06:26:00 +01:00
parent 865bb99c90
commit 6e5155d169
2 changed files with 1041 additions and 0 deletions

1000
data/day_2.txt Normal file

File diff suppressed because it is too large Load Diff

41
src/day_2.jl Normal file
View File

@ -0,0 +1,41 @@
include(joinpath(readchomp(`git rev-parse --show-toplevel`), "aoc.jl"))
import .Aoc: @aoc
function sliding(in, w::Int64; step::Int = 1)
((@view in[i:i+w-1]) for i in 1:step:length(in)-w+1)
end
function p1(input::Vector{String})
d = [0, 0]
for i input
c = match(r"(\w+)\s(\d+)", i).captures
if c[1] == "forward"
d[1] += parse(Int, c[2])
elseif c[1] == "down"
d[2] += parse(Int, c[2])
elseif c[1] == "up"
d[2] -= parse(Int, c[2])
end
end
prod(d)
end
function p2(input::Vector{String})
d = [0, 0, 0]
for i input
c = match(r"(\w+)\s(\d+)", i).captures
if c[1] == "forward"
d[1] += parse(Int, c[2])
d[2] += parse(Int, c[2]) * d[3]
elseif c[1] == "down"
d[3] += parse(Int, c[2])
elseif c[1] == "up"
d[3] -= parse(Int, c[2])
end
end
prod(d[1:2])
end
@aoc(2021, 2)