From 2928c03ce04f034fa07feb5e96c608a777f14098 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Mon, 11 Dec 2023 21:34:35 -0800 Subject: [PATCH] [2023][ruby][12.1] --- 2023/ruby/day_12.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 2023/ruby/day_12.rb diff --git a/2023/ruby/day_12.rb b/2023/ruby/day_12.rb new file mode 100644 index 0000000..97cfc1d --- /dev/null +++ b/2023/ruby/day_12.rb @@ -0,0 +1,14 @@ +p ARGF.readlines(chomp: true).map {|row| + springs, groups = row.split(" ") + [springs, groups.scan(/\d+/).map(&:to_i)] +}.map {|springs, groups| + unknowns = springs.count(??) + (0...2**unknowns).map {|arrangement| + arrangement = arrangement.to_s(2).rjust(unknowns, ?0).chars.map { _1 == ?0 ? ?. : ?# } + candidate = springs.gsub(??) { arrangement.shift } + candidate.chars + .slice_when { _1 != _2 } + .reject { _1[0] == ?. } + .map(&:size) == groups + }.count(&:itself) +}.sum