Skip to content

Instantly share code, notes, and snippets.

@JohnathanWeisner
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JohnathanWeisner/4ddf5ae72590f73e1923 to your computer and use it in GitHub Desktop.
Save JohnathanWeisner/4ddf5ae72590f73e1923 to your computer and use it in GitHub Desktop.
Shortest Ruby Sudoku Solver
# From
#
# $*.map{|a|(i=a=~/0/)?(v=*?1..?9).fill{|j|v-=[a[j+i-k=i%9],a[
# k+j*=9],a[j%26+i-i%3-i%27+k]]}+v.map{|k|$*<<$`+k+$'}:p(a)}
#
#
# To
start = Time.now
counter = 1
ARGV.map do |board|
counter += 1
puts board
system "clear"
board.chars.each_slice(9) {|row| puts row.join(" ")}
sleep 1
if index_of_zero = (board =~ /0/)
(possibilities = ('1'..'9').to_a ).fill do |step|
possibilities -=[
board[step + index_of_zero - index_within_row = index_of_zero % 9], #row
board[index_within_row + step *= 9], #col
board[step % 26 + index_of_zero - index_of_zero % 3 - index_of_zero % 27 + index_within_row ] #box
]
end
possibilities.map do |possibility|
ARGV.<< $` << possibility << $'
end
else #if solved
p board
p "Counter : #{counter}"
p "ARGV : #{ARGV.size}"
end
end
puts "Solved in #{(Time.now - start).round(3)} seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment