s = gets.chomp.split("").map(&:to_i).sort def f(n,g) return g.all?{|k, v| v == 0 } if n > 9 if g[n] == 0 f(n+1,g) else (if n <= 7 && g[n] > 0 && g[n+1] > 0 && g[n+2] > 0 g[n] -= 1;g[n+1] -= 1;g[n+2] -= 1 f(n,g).tap{ g[n] += 1;g[n+1] += 1;g[n+2] += 1 } end)|| (if g[n] >= 3;g[n] -= 3;f(n,g).tap{ g[n] += 3 } end ) end end p *(1..9).select{|n| a =s+[n] h = a.group_by(&:itself).map{|k,v|[k,v.size]}.to_h h.default = 0 next if (1..9).any?{|x| h[x] >4} if h.all?{|k,v| v ==2} true else (1..9).any?{|j| next if h[j] < 2 g = h.dup; g[j] -= 2 f(1,g) } end }