Here is the smallest Ruby Sudoku solver I could come up with. I’ve

seen a bunch that are really big, but this is the most simplistic. I’d

like to shrink it a bit more, but it works well for now. Things like

reading in the ARGV line as Integers could remove all the “.to_i” tags.

Another line could be removed if the hash is declared within the loop

instead of the top of the subroutine. Is there are way to loop through

numbers 1…9 using the 10.times notation so I don’t have to skip ‘0’ –

it’d be nice to remove that line too? Can we GOLF this anymore? I’m

going to keep refining it, so I’ll post any updates later on. This was

a fun problem - Ruby Rocks!

/----------------------------------------------------------------------------------------------------/

# A ruby script to solve sudoku puzzle

# USAGE: ruby sudoku.rb <Sudoku puzzle on one line/no spaces with 0’s

being the blanks>

# Example:ruby sudoku.rb 000201600…09605000

# Written by: Studlee2 at Gmail dot com

# algorithm by http://www.ecclestoad.co.uk/

$puzzle = ARGV.shift.split(//)

def sudoku_solver

hash = Hash.new()

80.times do |j|

next if $puzzle[j].to_i != 0

80.times {|k| hash[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 ?$puzzle[k.to_i]:0] = 1}

10.times {|v| next if v==0 || hash.has_key?(v.to_s); $puzzle[j] =

v.to_s; sudoku_solver}

return $puzzle[j] = 0;

end

return puts “The solution is:#{$puzzle}”

end

sudoku_solver

/----------------------------------------------------------------------------------------------------/

Enjoy,

_Steve