Created
June 26, 2012 04:31
-
-
Save Pcushing/2993295 to your computer and use it in GitHub Desktop.
solving sudoku... sorta
This file contains 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 Cell | |
attr_reader :number, :row, :column, :box | |
attr_accessor :value | |
# cell = {num: 1, value: [1,3,5], row: 2, column: 2, box: 9} | |
def initialize(cell_number, cell_value) | |
# Cells, rows, columns, and boxes all start at 0 | |
cell_to_box_map = { 0 => 0, 1 => 0, 2 => 0, 9 => 0, 10 => 0, 11 => 0, 18 => 0, 19 => 0, 20 => 0, | |
3 => 1, 4 => 1, 5 => 1, 12 => 1, 13 => 1, 14 => 1, 21 => 1, 22 => 1, 23 => 1, | |
6 => 2, 7 => 2, 8 => 2, 15 => 2, 16 => 2, 17 => 2, 24 => 2, 25 => 2, 26 => 2, | |
27 => 3, 28 => 3, 29 => 3, 36 => 3, 37 => 3, 38 => 3, 45 => 3, 46 => 3, 47 => 3, | |
30 => 4, 31 => 4, 32 => 4, 39 => 4, 40 => 4, 41 => 4, 48 => 4, 49 => 4, 50 => 4, | |
33 => 5, 34 => 5, 35 => 5, 42 => 5, 43 => 5, 44 => 5, 51 => 5, 52 => 5, 53 => 5, | |
54 => 6, 55 => 6, 56 => 6, 63 => 6, 64 => 6, 65 => 6, 72 => 6, 73 => 6, 74 => 6, | |
57 => 7, 58 => 7, 59 => 7, 66 => 7, 67 => 7, 68 => 7, 75 => 7, 76 => 7, 77 => 7, | |
60 => 8, 61 => 8, 62 => 8, 69 => 8, 70 => 8, 71 => 8, 78 => 8, 79 => 8, 80 => 8 } | |
@number = cell_number | |
@value = cell_value | |
@row = @number / 9 #any reason to use @number over cell_number? | |
@column = @number % 9 | |
@box = cell_to_box_map[cell_number] | |
end | |
# def to_s(cell_number) | |
# "number is #{ @number }, value is #{ @value },row is #{ @row }, column is #{ @column }, and the box is #{ @box }" | |
# end | |
def update(column, row, box) | |
potential = [1,2,3,4,5,6,7,8,9] | |
column.delete(0); row.delete(0); box.delete(0) | |
if ((potential - (column + box + row).uniq)).length == 1 | |
self.value = (potential - (column + box + row).uniq)[0] | |
end | |
end | |
end | |
class Board | |
def initialize(string) | |
@sudoku_string = string | |
@sudoku_array = string.split('') | |
end | |
def make_cells | |
@all_cells = [] | |
@sudoku_array.each_with_index { |value, cell| @all_cells << Cell.new(cell, value.to_i) } | |
end | |
def row_vals(number) | |
@row_values = [] | |
starting_point = number*9 | |
(starting_point..starting_point+8).each { |index| @row_values << @all_cells[index].value } | |
@row_values | |
end | |
def column_vals(number) | |
@column_values = [] | |
column_array = (0..8).collect { |x| x * 9 + number } | |
column_array.each { |cell| @column_values << @all_cells[cell].value } | |
@column_values | |
end | |
def box_vals(number) | |
@box_values = [] | |
starting_point = (number/3)*27 + (number%3)*3 | |
box_array = (0..8).collect { |x| starting_point + (x/3)*9 + x%3 } | |
box_array.each { |index| @box_values << @all_cells[index].value } | |
@box_values | |
end | |
def solve | |
#Get rid of the multiple iterations if we can | |
array_of_values = @all_cells.collect {|cell| cell.value } | |
while array_of_values.include?(0) do | |
@all_cells.each do |cell| | |
cell.update(row_vals(cell.row), column_vals(cell.column), box_vals(cell.box)) if cell.value == 0 | |
end | |
array_of_values = @all_cells.collect {|cell| cell.value } | |
end | |
@all_cells.collect! { |cell| cell.value } | |
puts "-" * 90 | |
puts @sudoku_string | |
puts @all_cells.join.to_s | |
puts "-" * 90 | |
end | |
end | |
# puzzles = File.readlines('sample.unsolved.txt') | |
# puts puzzles.inspect | |
new_board = Board.new("619030040270061008000047621486302079000014580031009060005720806320106057160400030") | |
new_board.make_cells | |
new_board.solve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment