def check_2x7(a) cnt = (0..4).map { a.count(_1) } cnt == [3, 0, 7, 0, 0] end def check_3x4(a) flag = true (0...10).each do |i| a[i] -= 3 if a[i] >= 3 if a[i] > 0 minus = a[i] (i...i + 3).each do |j| if j < 10 if minus > a[j] flag = false break end a[j] -= minus else flag = false end end end end flag end def check(a) flag = false flag = true if check_2x7(a.dup) (0...10).each do |i| if a[i] >= 2 a[i] -= 2 flag = true if check_3x4(a.dup) a[i] += 2 end end flag end s = gets.chomp.chars.map(&:to_i) a = (0...10).map { s.count(_1) } puts (0...10).select { |i| if i >= 1 && a[i] <= 3 a[i] += 1 res = check(a.dup) # p [a, res] a[i] -= 1 res else false end }