day 5 par 1

main
kirbylife 2024-12-05 01:10:43 -06:00
parent 91440cd234
commit 80969f48f3
2 changed files with 46 additions and 1 deletions

42
day-5/main.jl 100644
View File

@ -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

View File

@ -7,13 +7,14 @@ Día 1: Python
Día 2: Zig
Día 3: Bash
Día 4: LibreOffice Calc
Día 5: Julia
Lenguajes por usar:
1. ~~Python~~
1. ~~Zig~~
1. Golang
1. Julia
1. ~~Julia~~
1. C++
1. PHP
1. Java
@ -27,3 +28,5 @@ Lenguajes por usar:
1. D
1. ~~Bash~~
1. Crystal
1. Lua
1. C