rohacik = [ [00,10,01], [00,10,11], [10,11,01], [00,01,11] ] stvorcek = [ [00,10,01,11] ] hadik = [ [00,01,11,12], [01,11,10,20] ] tecko = [ [01,11,10,12], [01,11,00,02], [10,11,00,20], [10,11,01,21] ] krizik = [ [10,01,11,21,12] ] ciarka = [ [00,01,02], [00,10,20] ] elko = [ [00,01,11,21], [00,10,20,21], [00,10,01,02], [02,10,11,12] ] $piece_shapes = [ krizik, krizik, hadik, hadik, tecko, tecko, stvorcek, stvorcek, elko, elko, ciarka, ciarka, rohacik, rohacik, rohacik, rohacik ] def try_piece(field, ptype, piece) pdir, px, py = piece $piece_shapes[ptype][pdir].each do |c| xoff = c/10; yoff = c%10 return false if px+xoff >= 10 or py+yoff >= 6 return false if field[py+yoff][px+xoff] end return true end def render_field(pieces) field = Array.new(6).map{ Array.new(10) } pieces.each_with_index do |piece, ptype| pdir, px, py = piece $piece_shapes[ptype][pdir].each do |c| xoff = c/10; yoff = c%10 field[py+yoff][px+xoff] = ptype end end return field end def found_solution(pieces) print `clear` puts "---------------" field = render_field(pieces) field.each { |row| puts row.map{ |f| f ? (65+f).chr : "." }.join } exit if pieces.length == $piece_shapes.length end def try_solution(pieces) return found_solution(pieces) if pieces.length == $piece_shapes.length field = render_field(pieces); next_ptype = pieces.length $timer = ($timer+1)%50; found_solution(pieces) if $timer == 0 $piece_shapes[next_ptype].length.times do |pdir| 10.times do |px| 6.times do |py| $solutions_to_try << (pieces + [[pdir, px, py]]) if try_piece(field, next_ptype, [pdir, px, py]) end end end end $progress = 0; $timer = 0 $solutions_to_try = [ [] ] while $solutions_to_try.length > 0 do try_solution($solutions_to_try.pop) end