day 5 par 1
parent
91440cd234
commit
80969f48f3
|
@ -0,0 +1,42 @@
|
|||
function parse_input()
|
||||
rules = []
|
||||
manuals = []
|
||||
open("input.txt") do f
|
||||
for line in readlines(f)
|
||||
if length(line) == 0
|
||||
continue
|
||||
end
|
||||
if '|' in line
|
||||
num1, num2 = split(line, '|')
|
||||
push!(rules, (parse(Int, num1), parse(Int, num2)))
|
||||
else
|
||||
push!(manuals, map(s -> parse(Int, s), split(line, ',')))
|
||||
end
|
||||
end
|
||||
end
|
||||
return rules, manuals
|
||||
end
|
||||
|
||||
function check_rules(rules, page)
|
||||
for (n1, n2) in rules
|
||||
if !(n1 in page) || !(n2 in page)
|
||||
continue
|
||||
end
|
||||
n1_pos = findfirst(item -> item == n1, page)
|
||||
n2_pos = findfirst(item -> item == n2, page)
|
||||
if n1_pos > n2_pos
|
||||
return 0
|
||||
end
|
||||
end
|
||||
return page[(length(page) ÷ 2) + 1]
|
||||
end
|
||||
|
||||
function main()
|
||||
rules, manuals = parse_input()
|
||||
result = sum(map(m -> check_rules(rules, m), manuals))
|
||||
println(result)
|
||||
end
|
||||
|
||||
if abspath(PROGRAM_FILE) == @__FILE__
|
||||
main()
|
||||
end
|
Loading…
Reference in New Issue