Skip to content

Instantly share code, notes, and snippets.

@danalexilewis
Created July 28, 2015 23:57
Show Gist options
  • Save danalexilewis/c371f080269b9d25b57e to your computer and use it in GitHub Desktop.
Save danalexilewis/c371f080269b9d25b57e to your computer and use it in GitHub Desktop.
recursive sudoku solver
105802000090076405200400819019007306762083090000061050007600030430020501600308900
005030081902850060600004050007402830349760005008300490150087002090000600026049503
105802000090076405200400819019007306762083090000061050007600030430020501600308900
005030081902850060600004050007402830349760005008300490150087002090000600026049503
290500007700000400004738012902003064800050070500067200309004005000080700087005109
080020000040500320020309046600090004000640501134050700360004002407230600000700450
608730000200000460000064820080005701900618004031000080860200039050000100100456200
370000001000700005408061090000010000050090460086002030000000000694005203800149500
000689100800000029150000008403000050200005000090240801084700910500000060060410000
030500804504200010008009000790806103000005400050000007800000702000704600610300500
096040001100060004504810390007950043030080000405023018010630059059070830003590007
000075400000000008080190000300001060000000034000068170204000603900000020530200000
300000000050703008000028070700000043000000000003904105400300800100040000968000200
302609005500730000000000900000940000000000109000057060008500006000000003019082040
class Sudoku
def initialize(board_string)
@board = board_string.chars.to_a.map { |e| e.to_i }
end
def solve!
@board = solver(@board)
end
def to_s
puts "Start a Sudoku"
@board.each_with_index do |value, index|
if index == 0
puts "---------"
end
print value
if (index+1) %9 == 0
puts
end
if index == 26 || index == 53 || index == 80
puts "---------"
end
end
puts "Solved!!!"
end
private
def solver(board)
board.each_with_index do |value, index|
if value == 0
posib = possibles(index, board)
if posib.length == 0
return Array.new(1, 1)
end
until posib.length == 0 do
copy = board.dup
copy[index] = posib.pop
guess = solver(copy)
if guess.length == 1 && posib.length == 0
return Array.new(1, 1)
elsif check(guess)
return guess
end
end
end
end
end
def check(board)
board.reduce(:+) == 405
end
def possibles(index, board)
nums =* (1..9)
current = row(index, board).concat col(index, board).concat box(index, board)
nums - [current.uniq!].flatten
end
def row(index, board)
index = (index/9)*9
board[index..index+8].select{|v| v != 0}
end
def col(index, board)
board.select.with_index{|v,i| i%9 == index%9 && v != 0}
end
def box(index, board)
board.select.with_index{|v,i| boxcalc?(i, index) && v != 0}
end
def boxcalc?(i, index)
(i/3+3)%3 == (index/3+3)%3 && i/27 == index/27
end
# Returns a string representing the current state of the board
# Don't spend too much time on this method; flag someone from staff
# if you are.
end
# The file has newlines at the end of each line, so we call
# String#chomp to remove them.
File.open('sample.unsolved.txt').each do |line|
game = Sudoku.new(line.chomp)
# Remember: this will just fill out what it can and not "guess"
game.solve!
game.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment