Created
June 8, 2013 16:25
-
-
Save andmcgregor/5735728 to your computer and use it in GitHub Desktop.
sudoku
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 Sudoku | |
def initialize(sudoku_string) | |
@sudoku = sudoku_string.split("").map {|elem| elem.to_i } | |
9.times { @sudoku << @sudoku.shift(9) } | |
insert_possible_values | |
end | |
def solve! | |
100.times do | |
@sudoku.each_with_index do |row, row_i| | |
row.each_with_index do |cell, cell_i| | |
remove_impossible_values(cell, cell_i, row_i, row) if cell.is_a? Array | |
@sudoku[row_i][cell_i] = cell[0] if cell.is_a?(Array) && cell.length == 1 | |
end | |
end | |
end | |
end | |
def solved? | |
acc = 0 | |
@sudoku.each do |line| | |
line.each do |cell| | |
acc += cell if cell.is_a? Integer | |
end | |
end | |
acc == 405 | |
end | |
def insert_possible_values | |
@sudoku.each_with_index do |row, y| | |
row.each_with_index do |cell, x| | |
@sudoku[y][x] = (1..9).to_a if cell == 0 | |
end | |
end | |
end | |
def remove_impossible_values(cell, cell_i, row_i, row) | |
#show_array | |
impossible_cell_values = [] | |
@sudoku[row_i].select {|elem| impossible_cell_values << elem if elem.is_a? Integer } | |
@sudoku.each {|row| impossible_cell_values << row[cell_i] if row[cell_i].is_a? Integer } | |
# [row_i, cell_i] | |
top_left = [[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]] | |
top_middle = [] | |
top_right = [] | |
middle_left = [] | |
middle_middle = [] | |
middle_right = [] | |
bottom_left = [] | |
bottom_middle = [] | |
bottom_right = [] | |
top_left.each do |row_i, cell_i| | |
top_middle << [row_i, cell_i + 3] | |
top_right << [row_i, cell_i + 6] | |
middle_left << [row_i + 3, cell_i] | |
middle_middle << [row_i + 3, cell_i + 3] | |
middle_right << [row_i + 3, cell_i + 6] | |
bottom_left << [row_i + 6, cell_i] | |
bottom_middle << [row_i + 6, cell_i + 3] | |
bottom_right << [row_i + 6, cell_i + 6] | |
end | |
if row_i <= 2 | |
if cell_i <= 2 | |
region_coords = top_left | |
elsif cell_i <=5 | |
region_coords = top_middle | |
else | |
region_coords = top_right | |
end | |
elsif row_i <=5 | |
if cell_i <= 2 | |
region_coords = middle_left | |
elsif cell_i <=5 | |
region_coords = middle_middle | |
else | |
region_coords = middle_right | |
end | |
else | |
if cell_i <= 2 | |
region_coords = bottom_left | |
elsif cell_i <=5 | |
region_coords = bottom_middle | |
else | |
region_coords = bottom_right | |
end | |
end | |
region_coords.each do |row_i, cell_i| | |
impossible_cell_values << @sudoku[row_i][cell_i] if @sudoku[row_i][cell_i].is_a? Integer | |
end | |
@sudoku[row_i][cell_i] = cell - impossible_cell_values.uniq | |
end | |
def show_array | |
@sudoku.each do |row| | |
p row | |
end | |
puts | |
end | |
def to_s | |
@sudoku.each_with_index do |row, y| | |
row.each_with_index do |cell, x| | |
if cell.is_a? Integer | |
print cell.to_s.ljust(2) | |
else | |
print '-'.ljust(2) | |
end | |
if x == 2 || x == 5 | |
print ' | ' | |
end | |
end | |
puts | |
if y == 2 || y == 5 | |
puts '-' * 23 | |
end | |
end | |
end | |
end | |
game1 = Sudoku.new('003020600900305001001806400008102900700000008006708200002609500800203009005010300') | |
game2 = Sudoku.new('096040001100060004504810390007950043030080000405023018010630059059070830003590007') | |
game3 = Sudoku.new('105802000090076405200400819019007306762083090000061050007600030430020501600308900') | |
game4 = Sudoku.new('005030081902850060600004050007402830349760005008300490150087002090000600026049503') | |
game5 = Sudoku.new('096040001100060004504810390007950043030080000405023018010630059059070830003590007') | |
game6 = Sudoku.new('105802000090076405200400819019007306762083090000061050007600030430020501600308900') | |
game7 = Sudoku.new('005030081902850060600004050007402830349760005008300490150087002090000600026049503') | |
game8 = Sudoku.new('290500007700000400004738012902003064800050070500067200309004005000080700087005109') | |
game9 = Sudoku.new('080020000040500320020309046600090004000640501134050700360004002407230600000700450') | |
game10 = Sudoku.new('608730000200000460000064820080005701900618004031000080860200039050000100100456200') | |
game11 = Sudoku.new('370000001000700005408061090000010000050090460086002030000000000694005203800149500') | |
game12 = Sudoku.new('000689100800000029150000008403000050200005000090240801084700910500000060060410000') | |
game13 = Sudoku.new('030500804504200010008009000790806103000005400050000007800000702000704600610300500') | |
game14 = Sudoku.new('000075400000000008080190000300001060000000034000068170204000603900000020530200000') | |
game15 = Sudoku.new('300000000050703008000028070700000043000000000003904105400300800100040000968000200') | |
game16 = Sudoku.new('302609005500730000000000900000940000000000109000057060008500006000000003019082040') | |
game1.solve! | |
puts game1 | |
puts "SOLVED? #{game1.solved?}" | |
game2.solve! | |
puts game2 | |
puts "SOLVED? #{game2.solved?}" | |
game3.solve! | |
puts game3 | |
puts "SOLVED? #{game3.solved?}" | |
game4.solve! | |
puts game4 | |
puts "SOLVED? #{game4.solved?}" | |
game5.solve! | |
puts game5 | |
puts "SOLVED? #{game5.solved?}" | |
game6.solve! | |
puts game6 | |
puts "SOLVED? #{game6.solved?}" | |
game7.solve! | |
puts game7 | |
puts "SOLVED? #{game7.solved?}" | |
game8.solve! | |
puts game8 | |
puts "SOLVED? #{game8.solved?}" | |
game9.solve! | |
puts game9 | |
puts "SOLVED? #{game9.solved?}" | |
game10.solve! | |
puts game10 | |
puts "SOLVED? #{game10.solved?}" | |
game11.solve! | |
puts game11 | |
puts "SOLVED? #{game11.solved?}" | |
game12.solve! | |
puts game12 | |
puts "SOLVED? #{game12.solved?}" | |
game13.solve! | |
puts game13 | |
puts "SOLVED? #{game13.solved?}" | |
game14.solve! | |
puts game14 | |
puts "SOLVED? #{game14.solved?}" | |
game15.solve! | |
puts game15 | |
puts "SOLVED? #{game15.solved?}" | |
game16.solve! | |
puts game16 | |
puts "SOLVED? #{game16.solved?}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment