Last active
December 15, 2015 15:09
-
-
Save Stephenitis/5279731 to your computer and use it in GitHub Desktop.
sudoku columns and rows from a intial input(data) of a string or array of a Sudoku problem. testing the regex, use of instance variables
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 SudokuBoard | |
attr_reader :rows, :columns, :data | |
attr_writer :rows, :columns, :data | |
def initialize(data) | |
if (data.respond_to? :join) | |
@data = data.join | |
else # If argument looks like an array of lines | |
@data = data | |
end | |
processed_rows | |
processed_columns | |
end | |
def processed_rows | |
array1 = @data.gsub!(/[\.\-\_\D]/, "0").scan(/.{9}/) | |
rows = [] | |
puts array1.inspect | |
array1.each { |x| rows << x.scan(/\d/) } | |
@rows = rows | |
puts @rows.inspect | |
end | |
def processed_columns | |
@columns = @rows.transpose #this is magical I <3 | |
end | |
end | |
#test string | |
test1 = SudokuBoard.new(".11111111-22222222_33333.334444444445555555-566666_66677777777_888888888999_99999") | |
# # => | |
# @columns=[ | |
# ["0", "2", "3", "4", "5", "6", "7", "8", "9"], | |
# ["1", "2", "3", "4", "5", "6", "7", "8", "9"], | |
# ["1", "2", "3", "4", "5", "6", "7", "8", "9"], | |
# ["1", "2", "3", "4", "5", "6", "7", "8", "9"], | |
# ["1", "2", "3", "4", "5", "6", "7", "8", "9"], | |
# ["1", "2", "3", "4", "5", "6", "7", "8", "9"], | |
# ["1", "2", "3", "4", "5", "6", "7", "8", "9"], | |
# ["1", "2", "3", "4", "5", "6", "7", "8", "9"], | |
# ["1", "2", "3", "4", "5", "6", "7", "8", "9"]], | |
# @rows=[ | |
# ["0", "1", "1", "1", "1", "1", "1", "1", "1"], | |
# ["2", "2", "2", "2", "2", "2", "2", "2", "2"], | |
# ["3", "3", "3", "3", "3", "3", "3", "3", "3"], | |
# ["4", "4", "4", "4", "4", "4", "4", "4", "4"], | |
# ["5", "5", "5", "5", "5", "5", "5", "5", "5"], | |
# ["6", "6", "6", "6", "6", "6", "6", "6", "6"], | |
# ["7", "7", "7", "7", "7", "7", "7", "7", "7"], | |
# ["8", "8", "8", "8", "8", "8", "8", "8", "8"], | |
# ["9", "9", "9", "9", "9", "9", "9", "9", "9"]]> | |
test1.rows | |
#good | |
test1.columns | |
#good | |
test3 = SudokuBoard.new( | |
[ | |
["A", "1", "1", "1", "1", "B", 7, "1", "1"], | |
["2", 2, "-", "2", "2", "2", "2", "2", "2"], | |
["3", "3", "3", 2, "3", ".", "3", "3", "3"], | |
["4", "4", "4", "4", "4", "C", ".", "4", "4"], | |
["5", "5", ".", "5", "_", "_", "5", ".", "5"], | |
["6", "6", ".", "_", "6", "6", "6", "6", "."], | |
["7", "7", ".", "7", "7", "7", "_", "7", "7"], | |
["8", 8, "8", "8", "8", "_", "_", "_", "8"], | |
["9", "9", "9", 9, "_", "9", "9", "9", "_"] | |
]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment