From 7789002a2e8cb1a2c8750a934595dfd4035c3878 Mon Sep 17 00:00:00 2001 From: Patrick Michl Date: Fri, 10 Dec 2021 15:27:27 +0100 Subject: [PATCH] cleanup day 10 a bit --- src/day_10.jl | 57 ++++++--------------------------------------------- 1 file changed, 6 insertions(+), 51 deletions(-) diff --git a/src/day_10.jl b/src/day_10.jl index 0107806..315c5c2 100644 --- a/src/day_10.jl +++ b/src/day_10.jl @@ -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