Forked from JohnathanWeisner/worlds_shortest_sudoku_solver_refactored.rb
Last active
August 29, 2015 14:14
-
-
Save dahal/9dcc0dea3a1c25fa046b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
ARGV.map do |board| | |
if index_of_zero = (board =~ /0/) | |
buffer = (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 | |
steps_to_expand_from = possibilities.map do |possibility| | |
ARGV.<< $` << possibility << $' | |
end | |
buffer + steps_to_expand_from | |
else #if solved | |
puts board | |
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