Created
May 16, 2017 15:53
-
-
Save TApicella/f602bbb611ff16a952df1639a9729f5e to your computer and use it in GitHub Desktop.
RosettaCode- Sudoku created by tapicella - https://repl.it/HzMK/82
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
''' | |
http://rosettacode.org/wiki/Sudoku | |
''' | |
import copy | |
square_ranges = { | |
0: [0, 1, 2], | |
1: [0, 1, 2], | |
2: [0, 1, 2], | |
3: [3, 4, 5], | |
4: [3, 4, 5], | |
5: [3, 4, 5], | |
6: [6, 7, 8], | |
7: [6, 7, 8], | |
8: [6, 7, 8] | |
} | |
sumnums = sum([1, 2, 3, 4, 5, 6, 7, 8, 9]) | |
def checkHoriz(sudoku, y): | |
valid = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
relatives = [] | |
for x in range(len(sudoku[y])): | |
relatives.append((x, y)) | |
if sudoku[y][x]!= '': | |
try: valid.remove(sudoku[y][x]) | |
except: | |
pass | |
return valid, relatives | |
def checkVert(sudoku, x): | |
valid = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
relatives = [] | |
for y in range(len(sudoku)): | |
relatives.append((x, y)) | |
if sudoku[y][x] != '': | |
try: valid.remove(sudoku[y][x]) | |
except: | |
pass | |
return valid, relatives | |
def checkSquare(sudoku, coord): | |
valid = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
relatives = [] | |
xranges = square_ranges[coord[0]] | |
yranges = square_ranges[coord[1]] | |
for x in xranges: | |
for y in yranges: | |
relatives.append((x, y)) | |
if sudoku[y][x] != '': | |
try: valid.remove(sudoku[y][x]) | |
except: | |
pass | |
return valid, relatives | |
def solve(sudoku): | |
relationships = {} | |
prevsudoku = [] | |
for y in range(len(sudoku)): | |
for x in range(len(sudoku[y])): | |
if sudoku[y][x]=='': | |
valid_h, relatives_h = checkHoriz(sudoku, y) | |
valid_v, relatives_v = checkVert(sudoku, x) | |
valid_s, relatives_s = checkSquare(sudoku, (x, y)) | |
valid = list(set(valid_h) & set(valid_v) & set(valid_s)) | |
relatives = list(set(relatives_h+relatives_v+relatives_s)) | |
relationships[(x, y)] = {"valid" : valid, "relatives":relatives} | |
while len(relationships.keys()) > 0: | |
for k in relationships: | |
k_valid = relationships[k]["valid"] | |
if len(k_valid)==1: | |
k_val = k_valid[0] | |
sudoku[k[1]][k[0]] = k_val | |
for k2 in relationships: | |
if k in relationships[k2]["relatives"]: | |
neighbor_found = relationships[k2]["relatives"] | |
try: | |
relationships[k2]["valid"].remove(k_val) | |
except: | |
pass | |
relationships.pop(k) | |
break; | |
if prevsudoku == sudoku: | |
print("infinite loop") | |
return False | |
prevsudoku = copy.deepcopy(sudoku) | |
print(sudoku) | |
sudoku = [[5, 3, '', '', 7, '', '', '' ,''], | |
[6, '', '', 1, 9, 5, '', '', ''], | |
['', 9, 8, '' ,'' ,'', '', 6, ''], | |
[8, '', '', '', 6, '', '', '', 3], | |
[4, '', '', 8, '', 3, '', '', 1], | |
[7, '', '', '', 2, '', '', '', 6], | |
['', 6, '', '', '', '', 2, 8, ''], | |
['', '', '', 4, 1, 9, '', '', 5], | |
['', '', '', '', 8, '', '', 7, 9]] | |
solve(sudoku) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment