Ruby sudoku solver


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



Copy this code and paste it in your HTML
  1. #!/usr/bin/ruby
  2.  
  3. 10.times do
  4. s = 0
  5. 1.upto(9) do |x|
  6. 1.upto(9) do |y|
  7. 1.upto(9) do |z|
  8. if x + y + z == 15 && x != y && x != z && y != z
  9. 1.upto(9) do |a|
  10. 1.upto(9) do |b|
  11. 1.upto(9) do |c|
  12. if( ( a + b + c == 15) && ( ( a != b ) && ( a != c ) && ( b != c ) ) )
  13. 1.upto(9) do |i|
  14. 1.upto(9) do |o|
  15. 1.upto(9) do |p|
  16. if( ( i + o + p == 15 ) && ( ( i != o ) && ( i != p ) && ( o != p ) ) )
  17. if( ( x + a + i == 15 ) && ( ( x != a ) && ( x != i ) && ( a != i ) ) )
  18. if( ( y + b + o == 15 ) && ( ( y != b ) && ( y != o ) && ( b != o ) ) )
  19. if( ( z + c + p == 15 ) && ( ( z != c ) && ( z != p ) && ( c != p ) ) )
  20. if( ( x != b ) && ( x != c ) && ( x != o ) && ( x != p ) )
  21. if( ( y != a ) && ( y != i ) && ( y != c ) && ( y != p ) )
  22. if( ( z != b ) && ( z != a ) && ( z != o ) && ( z != i ) )
  23. s += 1
  24. $stdout.print "Solution ##{s}:\n"
  25. $stdout.print "#{x} #{y} #{z}\n"
  26. $stdout.print "#{a} #{b} #{c}\n"
  27. $stdout.print "#{i} #{o} #{p}\n\n"
  28. end
  29. end
  30. end
  31. end
  32. end
  33. end
  34. end
  35. end
  36. end
  37. end
  38. end
  39. end
  40. end
  41. end
  42. end
  43. end
  44. end
  45. end
  46. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.