Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Tight Ruby Sudoku Solver (See related posts)

// Small Sudoku Solver

#
#   A  ruby script to solve a sudoku puzzle
#  USAGE: ruby sudoku.rb 
#  Example:ruby sudoku.rb 000201600.....09605000
#
#    Written by:  steve at r3lax.com
#
#    Using the algorithm by http://www.ecclestoad.co.uk/
#        
$p = ARGV.shift.split(//)

def s
  h=Hash.new() 
  81.times do |j|
    next if $p[j].to_i!=0
    80.times{|k|h[k.to_i/9==j/9||k.to_i%9==j%9||k.to_i/27==j/27&&k.to_i%9/3==j%9/3?$p[k.to_i]:0]=1}
    1.upto(9){|v|next if h.has_key?(v.to_s);$p[j]=v.to_s;s}
      return $p[j]=0
  end
  return (puts "\nSolution:#{$p}")
end

s

You need to create an account or log in to post comments to this site.


Related Posts