From 80969f48f3fca139b141a482578438aa554a0364 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Thu, 5 Dec 2024 01:10:43 -0600 Subject: [PATCH] day 5 par 1 --- day-5/main.jl | 42 ++++++++++++++++++++++++++++++++++++++++++ readme.md | 5 ++++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 day-5/main.jl diff --git a/day-5/main.jl b/day-5/main.jl new file mode 100644 index 0000000..93e83ca --- /dev/null +++ b/day-5/main.jl @@ -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 diff --git a/readme.md b/readme.md index 9fdc33b..e321ec1 100644 --- a/readme.md +++ b/readme.md @@ -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