Skip to content

Instantly share code, notes, and snippets.

@kierarad
Created September 2, 2013 09:09
Show Gist options
  • Save kierarad/6410848 to your computer and use it in GitHub Desktop.
Save kierarad/6410848 to your computer and use it in GitHub Desktop.
class Sudoku
attr_reader :position, :current_cell_value
def initialize(board_string)
@board = []
@board_string = board_string
@position = [7,6] #[row, col]
@board_string.split(//).each_slice(9) {|slice| @board << slice}
@current_cell_value = @board[@position[0]][@position[1]]
@box_coord_hash = { box1: [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]],
box2: [[0,3],[0,4],[0,5],[1,3],[1,4],[1,5],[2,3], [2,4], [2,5]],
box3: [[0,6],[0,7],[0,8],[1,6],[1,7],[1,8],[2,6],[2,7],[2,8]],
box4: [[3,0],[3,1],[3,2],[4,0],[4,1],[4,2],[5,0],[5,1],[5,2]],
box5: [[3,3],[3,4],[3,5],[4,3],[4,4],[4,5],[5,3],[5,4],[5,5]],
box6: [[3,6],[3,7],[3,8],[4,6],[4,7],[4,8],[5,6],[5,7],[5,8]],
box7: [[6,0],[6,1],[6,2],[7,0],[7,1],[7,2],[8,0],[8,1],[8,2]],
box8: [[6,3],[6,4],[6,5],[7,3],[7,4],[7,5],[8,3],[8,4],[8,5]],
box9: [[6,6],[6,7],[6,8],[7,6],[7,7],[7,8],[8,6],[8,7],[8,8]]
}
end
def solve!
until the_whole_board_has_been_solved?
possibility_holder = []
unless occupied?
(1..9).each do |num|
@current_cell_value = num.to_s
unless already_in_the_row? || already_in_the_col? || already_in_the_box?
possibility_holder << @current_cell_value
end #check
end
end #occupied?
if possibility_holder.length == 1
@board[@position[0]][@position[1]] = possibility_holder.pop
end
update_position!
end
end
def the_whole_board_has_been_solved?
@board.each {|row| return false if row.include?("0")}
true
end
def board
@board
end
def call_row
row_arry = []
@board[@position[0]].each_with_index do |value, index|
row_arry << value unless index == @position[1]
end
row_arry
end
def call_column
col_arry = []
@board.each_with_index do |row, index|
col_arry << row[@position[1]] unless index == @position[0]
end
col_arry
end
def find_box
@box_coord_hash.each do |box_num, coord|
return box_num if coord.include?(@position)
end
end
def already_in_the_row?
call_row.each do |value|
if @current_cell_value == value
return true
end
end
false
end
def already_in_the_col?
call_column.each do |value|
if @current_cell_value == value
return true
end
end
false
end
def already_in_the_box?
box_num = find_box
box_grid_indices = @box_coord_hash[box_num].clone
box_without_current_cell = box_grid_indices.delete_if do |x|
x == @position
end
box_without_current_cell.each do |cell_coord|
if @current_cell_value == @board[cell_coord[0]][cell_coord[1]]
return true
end
end
false
end
def occupied?
@board[@position[0]][@position[1]] == "0" ? false : true
end
def update_position!
if @position[1] < 8
@position[1] += 1
elsif @position[1] == 8
if @position[0] == 8
@position = [0,0]
else
@position[1] = 0
@position[0] += 1
end
end
@current_cell_value = @board[@position[0]][@position[1]]
end
end
board_string = File.readlines('sample.unsolved.txt').first.chomp
game = Sudoku.new(board_string)
# # Remember: this will just fill out what it can and not "guess"
p game.solve!
p game.board
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment