Last active
December 22, 2015 04:48
-
-
Save rskelley9/6419097 to your computer and use it in GitHub Desktop.
Sudoku Solver works for all boards from beginner to advanced that don't require guessing. Yeeuh boi.
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
class Sudoku | |
#RKelley is boss. | |
def initialize(board_string) | |
board_string_array = board_string.split('') | |
@board = Array.new(9){Array.new(9)} | |
@board.each_index do |row| | |
@board[row].each_index do |column| | |
@board[row][column] = board_string_array.shift.to_i | |
if @board[row][column] == 0 | |
@board[row][column] = (1..9).to_a | |
end | |
end | |
end | |
end | |
def solve! | |
@solved_board = false | |
return @board if @solved_board == true | |
until @solved_board do | |
check_answers | |
col_solve | |
box_solve | |
print_board | |
solved? | |
sleep 0.5 | |
end | |
@board | |
end | |
def print_board | |
p @board | |
end | |
def solved? | |
@board.each do |row| | |
row.each do |element| | |
if element.class == Array then | |
return false | |
end | |
end | |
end | |
@solved_board = true | |
end | |
def check_answers | |
@board.map! do |row| | |
row.map do |element| | |
if element.class == Array | |
element -= row | |
element.length == 1 ? element[0] : element | |
else | |
element | |
end | |
end | |
end | |
@board | |
end | |
def col_solve | |
@board = @board.transpose | |
check_answers | |
@board = @board.transpose | |
end | |
def box_solve | |
@board = [@board[0][0..2] + @board[1][0..2] + @board[2][0..2], @board[0][3..5] + @board[1][3..5] + @board[2][3..5], @board[0][6..8] + @board[1][6..8] + @board[2][6..8], @board[3][0..2] + @board[4][0..2] + @board[5][0..2], @board[3][3..5] + @board[4][3..5] + @board[5][3..5], @board[3][6..8] + @board[4][6..8] + @board[5][6..8], @board[6][0..2] + @board[7][0..2] + @board[8][0..2], @board[6][3..5] + @board[7][3..5] + @board[8][3..5], @board[6][6..8] + @board[7][6..8] + @board[8][6..8]] | |
check_answers | |
@board = @board[0][0..2] + @board[1][0..2] + @board[2][0..2], @board[0][3..5] + @board[1][3..5] + @board[2][3..5], @board[0][6..8] + @board[1][6..8] + @board[2][6..8], @board[3][0..2] + @board[4][0..2] + @board[5][0..2], @board[3][3..5] + @board[4][3..5] + @board[5][3..5], @board[3][6..8] + @board[4][6..8] + @board[5][6..8], @board[6][0..2] + @board[7][0..2] + @board[8][0..2], @board[6][3..5] + @board[7][3..5] + @board[8][3..5], @board[6][6..8] + @board[7][6..8] + @board[8][6..8] | |
@board | |
end | |
end | |
# game = Sudoku.new("619030040270061008000047621486302079000014580031009060005720806320106057160400030") | |
# game = Sudoku.new("302609005500730000000000900000940000000000109000057060008500006000000003019082040") | |
game = Sudoku.new("370000001000700005408061090000010000050090460086002030000000000694005203800149500") | |
# Remember: this will just fill out what it can and not "guess" | |
p game.solve! | |
# p game.board | |
# p game.row_solve_col_solve | |
# p game.row_solve | |
# p game.col_solve | |
# p game.solve! | |
# game.board | |
#p game.box_solve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment