Created
November 27, 2014 11:40
-
-
Save icedraco/4b38fe8b467a832fd8d6 to your computer and use it in GitHub Desktop.
AFERDOWSI sudoku puzzle solver for the first Dropbox challenge that I wrote. Beats solving it the long way... :D
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
| sTable = [ | |
| [' ', 'W', 'I', ' ', ' ', 'D', ' ', ' ', 'A'], | |
| ['D', ' ', 'F', ' ', 'W', ' ', ' ', 'O', ' '], | |
| ['R', 'S', ' ', ' ', ' ', ' ', ' ', ' ', ' '], | |
| [' ', ' ', 'D', 'F', ' ', 'A', ' ', ' ', 'O'], | |
| [' ', 'A', 'O', ' ', ' ', ' ', 'D', 'R', ' '], | |
| ['S', ' ', ' ', 'E', ' ', 'O', 'A', ' ', ' '], | |
| [' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', 'W'], | |
| [' ', 'F', ' ', ' ', 'A', ' ', 'O', ' ', 'I'], | |
| ['A', ' ', ' ', 'W', ' ', ' ', 'R', 'F', ' '] | |
| ] | |
| chars = ('A','F','E','R','D','O','W','S','I') | |
| def printTable(table): | |
| for y in range(0,9): | |
| print ' '.join(table[y]) | |
| def getNext(x,y): | |
| if x >= 8: | |
| y += 1 | |
| x = 0 | |
| else: | |
| x += 1 | |
| if y > 8: | |
| return False | |
| return (x,y) | |
| def consider(table, x, y, char): | |
| for i in range(0,9): | |
| if (y != i and table[i][x] == char) or (x != i and table[y][i] == char): | |
| return False | |
| # Get block starting position from x,y | |
| (bx,by) = (3 * (x / 3), 3 * (y / 3)) | |
| for i in range(0,3): | |
| if (by+i) == y: | |
| continue | |
| for j in range(0,3): | |
| if (bx+j) == x: | |
| continue | |
| if table[by+i][bx+j] == char: | |
| return False | |
| return True | |
| def recurse(table, x, y): | |
| global chars | |
| if table[y][x] != ' ': | |
| result = getNext(x,y) | |
| if result == False: | |
| return False | |
| (x,y) = result | |
| return recurse(table, x, y) | |
| for ch in chars: | |
| if consider(table, x, y, ch): | |
| table[y][x] = ch | |
| result = getNext(x,y) | |
| if result == False: | |
| return True | |
| (tx,ty) = result | |
| if recurse(table, tx, ty): | |
| return True | |
| table[y][x] = ' ' | |
| # Out of options. | |
| return False | |
| if recurse(sTable, 0, 0): | |
| printTable(sTable) | |
| else: | |
| print "Couldn't find anything..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment