Posted By

gdonald on 09/27/06

Statistics

Viewed 282 times
Favorited by 2 user(s)

Ruby sudoku solver

/ Published in: Ruby
Save to your folder(s)

Copy this code and paste it in your HTML
`#!/usr/bin/ruby 10.times do  s = 0  1.upto(9) do |x|    1.upto(9) do |y|      1.upto(9) do |z|        if x + y + z == 15 && x != y && x != z && y != z          1.upto(9) do |a|            1.upto(9) do |b|              1.upto(9) do |c|                if( ( a + b + c == 15) && ( ( a != b ) && ( a != c ) && ( b != c ) ) )                  1.upto(9) do |i|                    1.upto(9) do |o|                      1.upto(9) do |p|                        if( ( i + o + p == 15 ) && ( ( i != o ) && ( i != p ) && ( o != p ) ) )                           if( ( x + a + i == 15 ) && ( ( x != a ) && ( x != i ) && ( a != i ) ) )                            if( ( y + b + o == 15 ) && ( ( y != b ) && ( y != o ) && ( b != o ) ) )                              if( ( z + c + p == 15 ) && ( ( z != c ) && ( z != p ) && ( c != p ) ) )                                if( ( x != b ) && ( x != c ) && ( x != o ) && ( x != p ) )                                  if( ( y != a ) && ( y != i ) && ( y != c ) && ( y != p ) )                                    if( ( z != b ) && ( z != a ) && ( z != o ) && ( z != i ) )                                      s += 1                                      \$stdout.print "Solution ##{s}:\n"                                      \$stdout.print "#{x} #{y} #{z}\n"                                      \$stdout.print "#{a} #{b} #{c}\n"                                      \$stdout.print "#{i} #{o} #{p}\n\n"                                    end                                  end                                end                              end                            end                          end                        end                      end                    end                  end                end              end            end          end        end      end    end  endend`