diff --git a/5.jl b/5.jl new file mode 100644 index 0000000..03cd03f --- /dev/null +++ b/5.jl @@ -0,0 +1,20 @@ + +txt = strip(read("input/5", String)) +rules, updates = map(x -> map(x -> map(x -> parse(Int, x), split(x, Regex("[,|]"))), split(x, "\n")), split(txt, "\n\n")) + +partOne, partTwo = 0, 0 + +isOrdered(update) = all(p -> all(after -> [p[2],after] in values(rules), update[p[1]+1:end]), enumerate(update)) +middle(v) = v[length(v)รท2+1] + +for update in updates + if isOrdered(update) + global partOne += middle(update) + else + sort!(update, lt = (a, b) -> isOrdered([a, b])) + global partTwo += middle(update) + end +end + +@show partOne, partTwo +