# At a small economics conference, a photographer wants to line up nine # participants for a photo. Two of them — Robert and Milton — insist on # standing next to each other. How many different arrangements (lineups) # are possible? keep = [] rest = [] perms = 123456789.to_s.chars.to_a.permutation.map(&:join) for i in perms if i.to_s =~ /12/ then keep.push(i.to_s); next; end if i.to_s =~ /21/ then keep.push(i.to_s); next; end rest.push(i.to_s) end puts "keep.size == #{keep.size}" puts "rest.size == #{rest.size}" puts "perms.size== #{perms.size}" # Output (first line is the answer): # keep.size == 80640 # rest.size == 282240 # perms.size== 362880