-
-
Save jackieiscool/2992641 to your computer and use it in GitHub Desktop.
Sudoku...started
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(puzzle) # puzzle == String | |
@puzzle = puzzle | |
@cell = Cell.new(puzzle) | |
@rows = Row.new(puzzle) | |
@column = Column.new(puzzle) | |
@three_by_three = ThreeByThree.new(puzzle) | |
end | |
end | |
class Cell | |
def initialize(puzzle) | |
@puzzle = puzzle.index('0') | |
end | |
end | |
class Row | |
attr_accessor :row_num, :row, :index_of_row | |
def initialize(puzzle, cell = '0') | |
@row_num = puzzle.index(cell) / 9 | |
@row = puzzle[(0+(row_num*9)), 9].split('') | |
@index_of_row = @row.index(cell) % 9 | |
end | |
end | |
class Column | |
def initialize(puzzle, cell = '0') | |
@col_num = puzzle.index(cell) % 9 | |
@col = @col_num.step(puzzle.size - 1, 9).map { |i| puzzle[i] } | |
@index_of_col = @col.index(cell) % 9 | |
end | |
end | |
class ThreeByThree | |
attr_accessor :grid | |
def initialize(puzzle, cell = '0') | |
@grid = [] | |
end | |
def col_num(puzzle, cell) | |
puzzle.index(cell) % 9 | |
end | |
def row_num(puzzle, cell) | |
puzzle.index(cell) / 9 | |
end | |
def grid_pos(puzzle, cell) | |
[row_num(puzzle, cell), col_num(puzzle, cell)] # Use 0 index for all locations of cell | |
end | |
# def find_grid(puzzle, cell) | |
# puzzle.split('').collect do |num_str| | |
# grid_pos(puzzle, num_str) | |
# end | |
# end | |
def grid_no(puzzle, cell) | |
cell_index = puzzle.index(cell) | |
end | |
def subgrid_at(id) | |
if id == 0 | |
id += 1 | |
end | |
hash = {1 => 0, 2 => 3, 3 => 6, 4 => 27, 5 => 30, 6 => 33, 7 => 54, 8 => 57, 9 => 60} | |
[hash[id],hash[id]+9,hash[id]+18].inject([]) do |array, offset| | |
array + puzzle[offset..offset+2] | |
end | |
end | |
def list_indexices(puzzle, cell) | |
puzzle.split('').each_with_index do |num_str, index| | |
puts index if num_str == cell | |
end | |
end | |
end | |
puzzle = '619030040270061008000047621486302079000014580031009060005720806320106057160400030' | |
cell = '0' | |
grid = ThreeByThree.new(puzzle, cell) | |
# p grid.find_grid(puzzle,cell) | |
# p grid.list_indexices(puzzle,cell) | |
# p grid.grid_pos(puzzle,cell) | |
def subgrid_at(id) | |
if id == 0 | |
id += 1 | |
end | |
hash = {1 => 0, 2 => 3, 3 => 6, 4 => 27, 5 => 30, 6 => 33, 7 => 54, 8 => 57, 9 => 60} | |
[hash[id],hash[id]+9,hash[id]+18].inject([]) do |array, offset| | |
array + puzzle[offset..offset+2] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment