Created
May 2, 2015 22:57
-
-
Save awhit012/a9a66586a5ae1978c5cf 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
# finished | |
PUZZLES = [ | |
"105802000090076405200400819019007306762083090000061050007600030430020501600308900", | |
"005030081902850060600004050007402830349760005008300490150087002090000600026049503", | |
"105802000090076405200400819019007306762083090000061050007600030430020501600308900", | |
"005030081902850060600004050007402830349760005008300490150087002090000600026049503", | |
"005030081902850060600004050007402830349760005008300490150087002090000600026049503", | |
"290500007700000400004738012902003064800050070500067200309004005000080700087005109", | |
"080020000040500320020309046600090004000640501134050700360004002407230600000700450", | |
"608730000200000460000064820080005701900618004031000080860200039050000100100456200", | |
"370000001000700005408061090000010000050090460086002030000000000694005203800149500", | |
"000689100800000029150000008403000050200005000090240801084700910500000060060410000", | |
"030500804504200010008009000790806103000005400050000007800000702000704600610300500", | |
"096040001100060004504810390007950043030080000405023018010630059059070830003590007", | |
"000075400000000008080190000300001060000000034000068170204000603900000020530200000", | |
"300000000050703008000028070700000043000000000003904105400300800100040000968000200", | |
"302609005500730000000000900000940000000000109000057060008500006000000003019082040", | |
"003020600900305001001806400008102900700000008006708200002609500800203009005010300", | |
"200080300060070084030500209000105408000000000402706000301007040720040060004010003", | |
"000000907000420180000705026100904000050000040000507009920108000034059000507000000", | |
"030050040008010500460000012070502080000603000040109030250000098001020600080060020", | |
"020810740700003100090002805009040087400208003160030200302700060005600008076051090", | |
"100920000524010000000000070050008102000000000402700090060000000000030945000071006", | |
"043080250600000000000001094900004070000608000010200003820500000000000005034090710", | |
"480006902002008001900370060840010200003704100001060049020085007700900600609200018", | |
"000900002050123400030000160908000000070000090000000205091000050007439020400007000", | |
"001900003900700160030005007050000009004302600200000070600100030042007006500006800", | |
"000125400008400000420800000030000095060902010510000060000003049000007200001298000", | |
"062340750100005600570000040000094800400000006005830000030000091006400007059083260", | |
"300000000005009000200504000020000700160000058704310600000890100000067080000005437", | |
"630000000000500008005674000000020000003401020000000345000007004080300902947100080", | |
"000020040008035000000070602031046970200000000000501203049000730000000010800004000", | |
'361025900080960010400000057008000471000603000259000800740000005020018060005470329', | |
"050807020600010090702540006070020301504000908103080070900076205060090003080103040", | |
"080005000000003457000070809060400903007010500408007020901020000842300000000100080", | |
"003502900000040000106000305900251008070408030800763001308000104000020000005104800", | |
"000000000009805100051907420290401065000000000140508093026709580005103600000000000", | |
"020030090000907000900208005004806500607000208003102900800605007000309000030020050", | |
"005000006070009020000500107804150000000803000000092805907006000030400010200000600", | |
'040000050001943600009000300600050002103000506800020007005000200002436700030000040', | |
"004000000000030002390700080400009001209801307600200008010008053900040000000000800", | |
"360020089000361000000000000803000602400603007607000108000000000000418000970030014", | |
"500400060009000800640020000000001008208000501700500000000090084003000600060003002", | |
"007256400400000005010030060000508000008060200000107000030070090200000004006312700", | |
"000000000079050180800000007007306800450708096003502700700000005016030420000000000", | |
"030000080009000500007509200700105008020090030900402001004207100002000800070000090", | |
"200170603050000100000006079000040700000801000009050000310400000005000060906037002", | |
"000000080800701040040020030374000900000030000005000321010060050050802006080000000", | |
"000000085000210009960080100500800016000000000890006007009070052300054000480000000", | |
"608070502050608070002000300500090006040302050800050003005000200010704090409060701", | |
"050010040107000602000905000208030501040070020901080406000401000304000709020060010", | |
'053000790009753400100000002090080010000907000080030070500000003007641200061000940', | |
"006080300049070250000405000600317004007000800100826009000702000075040190003090600", | |
'005080700700204005320000084060105040008000500070803010450000091600508007003010600', | |
"000900800128006400070800060800430007500000009600079008090004010003600284001007000", | |
'000080000270000054095000810009806400020403060006905100017000620460000038000090000', | |
'000602000400050001085010620038206710000000000019407350026040530900020007000809000', | |
'000900002050123400030000160908000000070000090000000205091000050007439020400007000', | |
"380000000000400785009020300060090000800302009000040070001070500495006000000000092", | |
'000158000002060800030000040027030510000000000046080790050000080004070100000325000', | |
'010500200900001000002008030500030007008000500600080004040100700000700006003004050', | |
"080000040000469000400000007005904600070608030008502100900000005000781000060000010", | |
"904200007010000000000706500000800090020904060040002000001607000000000030300005702", | |
"000700800006000031040002000024070000010030080000060290000800070860000500002006000", | |
"001007090590080001030000080000005800050060020004100000080000030100020079020700400", | |
"000003017015009008060000000100007000009000200000500004000000020500600340340200000", | |
"300200000000107000706030500070009080900020004010800050009040301000702000000008006", | |
"619030040270061008000047621486302079000014580031009060005720806320106057160400030"] | |
class Sudoku | |
attr_reader :board | |
def initialize board | |
@board = board | |
@rows = board.scan(/........./) | |
make_tables | |
valid? | |
solve! | |
end | |
def valid? | |
@board.length == 81 ? true : false | |
check_tables | |
end | |
def make_tables | |
@row_answers, @col_answers, @box_answers, @possibles = {},{},{},{} | |
fill_row_answers | |
fill_col_answers | |
fill_box_answers | |
@tables = [@row_answers, @col_answers, @box_answers] | |
end | |
def fill_row_answers | |
@rows.each_with_index {|row, index| @row_answers[index] = row} | |
end | |
def fill_col_answers | |
(0..8).each {|num| @col_answers[num] = ""} | |
@rows.each do |row| | |
row.each_char.with_index do |char, index| | |
@col_answers[index] += char | |
end | |
end | |
end | |
def fill_box_answers | |
(0..8).each {|box_number| @box_answers[box_number] = ""} | |
@board.each_char.with_index do |char, index| | |
@box_answers[which_box?(index)] += char | |
end | |
end | |
def which_box? index | |
case index/9 | |
when (0..2) | |
index % 9 / 3 | |
when (3..5) | |
index % 9 / 3 + 3 | |
when (6..8) | |
index % 9 / 3 + 6 | |
end | |
end | |
def check_tables | |
validity = true | |
@tables.each do |table| | |
(0..8).each do |index| | |
row_col_or_box = table[index].delete('0') | |
if row_col_or_box != row_col_or_box.split("").uniq.join("") | |
validity = false | |
end | |
end | |
end | |
validity | |
end | |
def solve! | |
get_unsolved | |
get_coordinates | |
get_possible_answers | |
fill_solvable_squares | |
end | |
def get_unsolved | |
@unsolved = [] | |
@board.each_char.with_index { |char, index| @unsolved << index if char == '0' } | |
end | |
def get_coordinates | |
@coordinates = {} | |
@unsolved.each do |index| | |
@coordinates[index] = [] | |
@coordinates[index] << index/ 9 << index % 9 << (which_box? index) | |
end | |
end | |
def get_possible_answers | |
@unsolved.each do |index| | |
possibles = "123456789" | |
@tables.each_with_index do |table, idx| | |
possibles.delete!(table[@coordinates[index][idx]]) | |
end | |
@possibles[index] = possibles | |
end | |
end | |
def fill_solvable_squares | |
solved = 0 | |
@possibles.each_pair do |key, value| | |
if value.split('').length == 1 | |
solved += 1 | |
@board[key] = value.split('')[0] | |
end | |
end | |
until is_solved? | |
Sudoku.new @board | |
end | |
@board | |
end | |
def is_solved? | |
true unless @board.include?('0') | |
end | |
end | |
PUZZLES.each_with_index do |puzzle, index| | |
index = Sudoku.new puzzle | |
p index.board | |
end | |
# refactored again | |
# partial | |
class Sudoku | |
def intialize(board) | |
@board = board | |
@rows = board.scan(/........./) | |
@cols = [] | |
@box = get_boxes | |
end | |
def solve! | |
@board.each_with_index do |square, index| | |
get_coordinates(square) | |
end | |
end | |
def get_coordinates(square, index) | |
row = get_row(index) | |
col = get_col(index) | |
box = get_box(index) | |
get_possibles(row, col, box) | |
end | |
def get_row(index) | |
index/9 | |
end | |
def get_col(index) | |
index % 9 | |
end | |
def get_box(index) | |
case index/9 | |
when (0..2) | |
index % 9 / 3 | |
when (3..5) | |
index % 9 / 3 + 3 | |
when (6..8) | |
index % 9 / 3 + 6 | |
end | |
end | |
def get_possibles(row, col, box) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment