cleanup day 10 a bit

This commit is contained in:
Patrick Michl 2021-12-10 15:27:27 +01:00
parent 5094014b93
commit 7789002a2e

View File

@ -13,11 +13,7 @@ function check_line(l, dict, scores)
end
if length(braces) != 0 && b == dict[braces[end]]
pop!(braces)
continue
else
@info "found $b"
t = braces[end]
@info "should be $t"
return scores[b]
end
end
@ -25,39 +21,9 @@ function check_line(l, dict, scores)
end
function p1(input::Vector{String})
dict = Dict(
'(' => ')',
'[' => ']',
'<' => '>',
'{' => '}'
)
scores = Dict(
')' => 3,
']' => 57,
'>' => 25137,
'}' => 1197)
sum = 0
for l in input
sum += check_line(l, dict, scores)
end
return sum
end
function corrupted(l, dict)
braces = []
for b in l
if b in keys(dict)
push!(braces, b)
continue
end
if length(braces) != 0 && b == dict[braces[end]]
pop!(braces)
continue
else
return true
end
end
return false
dict = Dict('(' => ')', '[' => ']', '<' => '>', '{' => '}')
scores = Dict(')' => 3, ']' => 57, '>' => 25137, '}' => 1197)
return sum([ check_line(l, dict, scores) for l in input ])
end
function complete(l, dict)
@ -69,7 +35,6 @@ function complete(l, dict)
end
if length(braces) != 0 && b == dict[braces[end]]
pop!(braces)
continue
end
end
out = []
@ -80,19 +45,9 @@ function complete(l, dict)
end
function p2(input::Vector{String})
dict = Dict(
'(' => ')',
'[' => ']',
'<' => '>',
'{' => '}'
)
scores = Dict(
')' => 1,
']' => 2,
'}' => 3,
'>' => 4)
line_scores = []
remaining = filter(x -> !corrupted(x, dict), input)
dict = Dict('(' => ')','[' => ']', '<' => '>', '{' => '}')
scores = Dict(')' => 1,']' => 2,'}' => 3,'>' => 4)
remaining = filter(x -> check_line(x, dict, scores) == 0, input)
a = [complete(x, dict) for x in remaining]
out = []
for e in a