aoc2021/10.jl

63 lines
1.2 KiB
Julia
Raw Normal View History

2021-12-22 10:33:30 +00:00
using Pkg
Pkg.add("Match")
using Match
include("utils.jl")
function corrupt(line)
first, rest... = line
check = [first]
for char in rest
if char in "<{[("
push!(check, char)
else
match, score = @match char begin
'>' => ('<', 25137)
'}' => ('{', 1197)
']' => ('[', 57)
')' => ('(', 3)
end
if last(check) == match
pop!(check)
else
return score
end
end
end
0
end
function solution10_1()
sum(map(corrupt, non_empty_lines("10.data")))
end
function fix(line)
first, rest... = line
check = [first]
for char in rest
if char in "<{[("
push!(check, char)
else
pop!(check)
end
end
value = 0
for char in reverse(check)
value *= 5
value += @match char begin
'<' => 4
'{' => 3
'[' => 2
'(' => 1
end
end
value
end
function solution10_2()
values = sort(map(fix, filter(line->corrupt(line)==0, collect(non_empty_lines("10.data")))))
values[div(length(values), 2)+1]
end
solution10_2()