Skip to content

Instantly share code, notes, and snippets.

@EdConnell
Last active December 18, 2015 05:29
Show Gist options
  • Save EdConnell/5733018 to your computer and use it in GitHub Desktop.
Save EdConnell/5733018 to your computer and use it in GitHub Desktop.
Sudoku shizzle
board = [
[0,9,6,0,4,0,0,0,1],
[1,0,0,0,6,0,0,0,4],
[5,0,4,8,1,0,3,9,0],
[0,0,7,9,5,0,0,4,3],
[0,3,0,0,8,0,0,0,0],
[4,0,5,0,2,3,0,1,8],
[0,1,0,6,3,0,0,5,9],
[0,5,9,0,7,0,8,3,0],
[0,0,3,5,9,0,0,0,7]]
#columns = Array.new(9) {Array.new(9) {0}}
#squares = Array.new(9) #{Array.new(3) {}} #{Array.new(3) {0}}}
def build_columns(board)
columns = Array.new(9) {Array.new(9) {0}}
(0..8).each do |row|
numbers = []
(0..8).each do |col|
numbers << board[col][row]
end
columns[row] = numbers
end
columns
end
columns = build_columns(board)
columns
ranges = [
[0..2, 0..2],
[0..2, 3..5],
[0..2, 6..8],
[3..5, 0..2],
[3..5, 3..5],
[3..5, 6..8],
[6..8, 0..2],
[6..8, 3..5],
[6..8, 6..8]
]
def square_builder(board, ranges)
squares = Array.new(9)
board.each_with_index do |row, index|
square_holder = []
for x in ranges[index][0]
for y in ranges[index][1]
square_holder << board[x][y]
end
end
squares[index] = square_holder
end
return squares
end
squares = square_builder(board, ranges)
def checker(numbers)
winning_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers.sort == winning_numbers
end
squares.each do |square|
puts checker(square)
end
squares.length
squares[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment