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