[2024][ruby][5.x]
change lxpvqrvxwkyprloupoyqqyzonqxtwxot
commit 782bb3ef03bee66379b139e9be4f18d9daaa4b68
author Alpha Chen <alpha@kejadlen.dev>
date
parent runktvys
diff --git a/2024/ruby/day_05.rb b/2024/ruby/day_05.rb
new file mode 100644
index 0000000..7abad82
--- /dev/null
+++ b/2024/ruby/day_05.rb
@@ -0,0 +1,16 @@
+rules, updates = DATA.read.split("\n\n")
+
+rules = rules.scan(/(\d+)\|(\d+)/).map { _1.map(&:to_i) }
+updates = updates.lines.map { _1.split(?,).map(&:to_i) }
+
+correct, incorrect = updates.partition {|pages|
+  pages.each_cons(2).all? { rules.include?(_1) }
+}
+
+pp correct.sum { _1[_1.length/2] }
+
+pp incorrect.map {|pages|
+  pages.sort {|a,b| rules.include?([a,b]) ? -1 : 1 }
+}.sum { _1[_1.length/2] }
+
+__END__