From 21e41740516c986762ccbc21e5b52a1e7122c032 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 12 Dec 2023 21:59:35 -0800 Subject: [PATCH] [2023][ruby][13.1] --- 2023/ruby/day_13.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 2023/ruby/day_13.rb diff --git a/2023/ruby/day_13.rb b/2023/ruby/day_13.rb new file mode 100644 index 0000000..7a90360 --- /dev/null +++ b/2023/ruby/day_13.rb @@ -0,0 +1,14 @@ +patterns = ARGF.read.split("\n\n") + .map { _1.split("\n").map(&:chars) } + +def find_fold(pattern) + (0.5...pattern.length-0.5).step(1).find {|fold| + a = pattern[0..fold.floor] + b = pattern[fold.ceil..] + a.reverse.zip(b).reject { _1.any?(&:nil?) }.all? { _1 == _2 } + }&.ceil +end + +verts = patterns.map { find_fold(_1.transpose) }.compact +hors = patterns.map { find_fold(_1) }.compact +p verts.sum + hors.sum { _1 * 100 }