Last active
August 29, 2015 13:57
-
-
Save Carpk/9726540 to your computer and use it in GitHub Desktop.
Sudoku solver
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
require 'benchmark' | |
class Sudoku | |
def initialize(board_string) | |
@board = create_board(board_string.split(//).map {|str| str.to_i}) | |
@potentials = (0..9).to_a | |
end | |
def create_board(num_array) | |
# num_array.map! {|zero| zero == 0 ? (1..9).to_a : zero }shift(3) | |
Array.new(9) {Array.new(3) { num_array.map! {|zero| zero == 0 ? (1..9).to_a : zero }.shift(3) } } | |
end | |
def solve! | |
@board.each_with_index do |row, row_indx| | |
row.each_with_index do |cell, cell_indx| | |
cell.each_with_index do |find_array, void_indx| | |
if find_array.class == Array | |
find_array.reject! { |i| local_values(row_indx, cell_indx, void_indx).include?(i) } | |
p "row: #{row_indx}, column: #{cell_indx}, cell: #{void_indx}, axis values:#{find_array}" | |
if find_array.length == 1 | |
@board[row_indx][cell_indx][void_indx] = find_array[0] | |
end | |
end | |
end | |
end | |
end | |
solved? | |
end | |
def local_values(row, local, column) | |
returning_values = [] | |
returning_values << @board[row] | |
@board.each do |subset| | |
returning_values << subset[local][column] | |
end | |
@board[local_cluster(row)].each do |cluster| | |
returning_values << cluster[local] | |
end | |
returning_values.reject! {|array| array.class == Array}.flatten.uniq | |
end | |
def local_cluster(position) | |
case position | |
when 0..2 | |
0..2 | |
when 3..5 | |
3..5 | |
when 6..9 | |
6..9 | |
end | |
end | |
def solved? | |
if @board.flatten(2).include?(Array) | |
solve! | |
end | |
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. | |
def board | |
p @board.flatten(2) #.each do |row| | |
# p row | |
# end | |
end | |
end | |
# The file has newlines at the end of each line, so we call | |
# String#chomp to remove them. | |
board_string = File.readlines('sample.unsolved.txt').first.chomp | |
game = Sudoku.new(board_string) | |
#puts Benchmark.measure {game.solve!} * 5 | |
# Remember: this will just fill out what it can and not "guess" | |
game.solve! | |
game.board |
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
105802000090076405200400819019007306762083090000061050007600030430020501600308900 | |
005030081902850060600004050007402830349760005008300490150087002090000600026049503 | |
105802000090076405200400819019007306762083090000061050007600030430020501600308900 | |
005030081902850060600004050007402830349760005008300490150087002090000600026049503 | |
290500007700000400004738012902003064800050070500067200309004005000080700087005109 | |
080020000040500320020309046600090004000640501134050700360004002407230600000700450 | |
608730000200000460000064820080005701900618004031000080860200039050000100100456200 | |
370000001000700005408061090000010000050090460086002030000000000694005203800149500 | |
000689100800000029150000008403000050200005000090240801084700910500000060060410000 | |
030500804504200010008009000790806103000005400050000007800000702000704600610300500 | |
096040001100060004504810390007950043030080000405023018010630059059070830003590007 | |
000075400000000008080190000300001060000000034000068170204000603900000020530200000 | |
300000000050703008000028070700000043000000000003904105400300800100040000968000200 | |
302609005500730000000000900000940000000000109000057060008500006000000003019082040 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment