Day 5
This commit is contained in:
parent
8008efa1d1
commit
1927f2986a
1 changed files with 20 additions and 0 deletions
20
5.jl
Normal file
20
5.jl
Normal file
|
|
@ -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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue