# # 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