Skip to content

Instantly share code, notes, and snippets.

@lilliealbert
Created May 18, 2013 02:53
Show Gist options
  • Save lilliealbert/5603070 to your computer and use it in GitHub Desktop.
Save lilliealbert/5603070 to your computer and use it in GitHub Desktop.
# How to solve a sudoku puzzle
I. Find an empty cell
* Starting at 0,0, scan the board looking for 0
* Scan is some kind of nested loop! (Outer loop looking on x axis, inner -> y axis)
* If found
* Coordinates of first found empty cell gets stored (probably in an instance variable)
* Move on II
* If not found
* won!
II. Check the values of the cell's row, column, or square, determine range of possibilities
* scan the square, removing found numbers from possibilities
* Take the empty cell coordinate and find out which square we're looking at by asking collection_of_squares (which has all the board's coordinates divided by square)
* Once we have the coordniates of the square, move through the board based on those coordinates
* For each cell scanned, remove found numbers from possibilities
* scan the row, removing found numbers from possibilities
* scan each cell at coordinate x, (all of 1-9)
* For each cell scanned, remove found numbers from possibilities
* scan the column, removing
* scan each cell at inate (all of 1-9), y
* For each cell scanned, remove found numbers from possibilities
III. Determine if there's one solution
a. If there is only one possibility after moving in the 3 directions, fill in, and move back to step one
* After scanning row, cell, & column, if possibilities is only 1 item
* Take the coordinates of the empty cell that we're filling in and swap the content for the final item in possibilities
ie: board[0][0] = possibilities[0]
b. If no --> repeat
* go back to finding the empty cell
We call find_all_empty and set @empty_cell to the first coordinates in the array
Board Data Structure needs:
* easily/simply traversable
* can tell if a cell is empty (0)
Implementation Data Structure(s)
* we need somewhere to store our possibilities per square
@board = [
[(9 numbers)],
[],
[],
[],
[],
[],
[],
[],
[],
]
@collection_of_squares = [
[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],],
[square2],
[square3]
]
@possibility = [1, 2, 3, 4, 5, 6, 7, 8, 9]
@collection_of_squares = []
3.times do |a|
3.times do |b|
square = []
3.times do |c|
3.times do |d|
square << [c+3*a, d+3*b]
end
end
@collection_of_squares << square
end
end
The goal of collection_of_squares is to have each of the 9 square's coordinates saved so that when the program needs to check the values in a given square, it can.
The collection of squares is accessed by searching collection_of_squares for a given coordinate and then return all the coorindates
[
[1, 2, 3, 0],
[5, 1, 4, 7],
[1, 0, 4, 2],
[2, 5, 2, 2]
]
We are 2,1
x = 2, y = 1
column
0, 1 -- x-2, y
1, 1 -- x-1, y
(2,1) -- x, y
3, 1 -- x+1, y
x = 2, y = 1
row
2, 0 -- x, y-1
(2,1) -- x, y
2,2 -- x, y+1
2,3 -- x, y+2
x = 2, y = 1
box
1,0 -- x-1, y-1
1,1 -- x-1, y
1,2 -- x-1, y+1
2,0 -- x, y-1
(2,1) -- x, y
2,2 -- x, y+1
3,0 -- x+1, y-1
3,1 -- x+1, y
3,2 -- x+1, y+1
ORDER BY X COORDINATE
(2,1) -- x, y
2, 0 -- x, y-1
2,2 -- x, y+1
2,3 -- x, y+2
2,0 -- x, y-1
2,2 -- x, y+1
3, 1 -- x+1, y
3,0 -- x+1, y-1
3,1 -- x+1, y
3,2 -- x+1, y+1
1, 1 -- x-1, y
1,0 -- x-1, y-1
1,1 -- x-1, y
1,2 -- x-1, y+1
0, 1 -- x-2, y
ORDER BY Y COORDINATE
(2,1) -- x, y
0, 1 -- x-2, y
3, 1 -- x+1, y
1, 1 -- x-1, y
3,1 -- x+1, y
1,1 -- x-1, y
2,0 -- x, y-1
3,0 -- x+1, y-1
1,0 -- x-1, y-1
2, 0 -- x, y-1
2,2 -- x, y+1
2,2 -- x, y+1
3,2 -- x+1, y+1
1,2 -- x-1, y+1
2,3 -- x, y+2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment