diff --git a/2021/ruby/day_12.rb b/2021/ruby/day_12.rb new file mode 100644 index 0000000..41612db --- /dev/null +++ b/2021/ruby/day_12.rb @@ -0,0 +1,25 @@ +cave = Hash.new {|h,k| h[k] = [] } +ARGF.read.scan(/(\w+)-(\w+)/).each do + cave[_1] << _2 + cave[_2] << _1 +end + +paths = [ %w[start] ] + +loop do + closed, open = paths.partition { _1.last == "end" } + break if open.empty? + + paths = closed + open.flat_map {|path| + cave.fetch(path.last) + # .reject { _1 =~ /^[a-z]+$/ && path.include?(_1) } + .reject { _1 == "start" } + .map { path + [_1] } + .reject {|path| + small = path.tally.select { _1 =~ /^[a-z]+$/ } + (small.count { _2 > 1 } > 1) || small.any? { _2 > 2 } + } + } +end + +p paths.count