Created
September 21, 2013 01:39
-
-
Save MrBean83/6646205 to your computer and use it in GitHub Desktop.
Sudoku (Elaine & Luis)
This file contains hidden or 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(string) | |
| string = string.split('') | |
| @board = Array.new(9){Array.new(9)} | |
| @board.each_index do |row| | |
| @board[row].each_index do |column| | |
| @board[row][column] = string.shift.to_i | |
| if @board[row][column] == 0 | |
| @board[row][column] = (1..9).to_a | |
| end | |
| end | |
| end | |
| end | |
| def solve! | |
| 15.times do | |
| check_rows | |
| check_columns | |
| check_box | |
| rows_one_index | |
| columns_one_index | |
| boxes_one_index | |
| end | |
| @board | |
| end | |
| def check_rows | |
| @board.map! do |row| | |
| row.map do |index| | |
| if index.class == Array | |
| index - row | |
| else | |
| index | |
| end | |
| end | |
| end | |
| end | |
| def check_columns | |
| @board = @board.transpose | |
| @board.map! do |col| | |
| col.map do |index| | |
| if index.class == Array | |
| index - col | |
| else | |
| index | |
| end | |
| end | |
| end | |
| @board | |
| end | |
| def check_box | |
| box = @board | |
| box_1 = box[0][0..2] + box[1][0..2] + box[2][0..2] | |
| box_2 = box[0][3..5] + box[1][3..5] + box[2][3..5] | |
| box_3 = box[0][6..8] + box[1][6..8] + box[2][6..8] | |
| box_4 = box[3][0..2] + box[4][0..2] + box[5][0..2] | |
| box_5 = box[3][3..5] + box[4][3..5] + box[5][3..5] | |
| box_6 = box[3][6..8] + box[4][6..8] + box[5][6..8] | |
| box_7 = box[6][0..2] + box[7][0..2] + box[8][0..2] | |
| box_8 = box[6][3..5] + box[7][3..5] + box[8][3..5] | |
| box_9 = box[6][6..8] + box[7][6..8] + box[8][6..8] | |
| boxes = [box_1, box_2, box_3, box_4, box_5, box_6, box_7, box_8] | |
| boxes.map! do |box| | |
| box.map do |index| | |
| if index.class == Array | |
| index - box | |
| else | |
| index | |
| end | |
| end | |
| end | |
| end | |
| def rows_one_index | |
| @board.each do |row| | |
| row.map! do |index| | |
| if index.class == Array && index.size == 1 | |
| index.join.to_i | |
| else | |
| index | |
| end | |
| end | |
| end | |
| end | |
| def columns_one_index | |
| @board = @board.transpose | |
| @board.each do |column| | |
| column.map! do |index| | |
| if index.class == Array && index.size == 1 | |
| index.join.to_i | |
| else | |
| index | |
| end | |
| end | |
| end | |
| end | |
| def boxes_one_index | |
| @board.each do |box| | |
| box.map! do |index| | |
| if index.class == Array && index.size == 1 | |
| index.join.to_i | |
| else | |
| index | |
| end | |
| end | |
| 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("619030040270061008000047621486302079000014580031009060005720806320106057160400030") | |
| # Remember: this will just fill out what it can and not "guess" | |
| p game.solve! | |
| # puts game.board | |
| # p game.check_box | |
| # p game.check_columns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment