Advertisement
DocNumFo

Sudoku solver (in progress)

Apr 4th, 2017
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. import console
  2. def columns(Numbers, emptyIndex):
  3.     columnList=[[]]
  4.     for i in range(0,9):
  5.         columnList+=[Numbers[int(9*i)+int(emptyIndex%9)]]
  6.     return columnList
  7.  
  8. def rows(Numbers, emptyIndex):
  9.     rowsList=[[]]
  10.     rowsList=[Numbers[emptyIndex-emptyIndex%9:(emptyIndex-emptyIndex%9)+9]]
  11.     return rowsList
  12.  
  13. def blocks(Numbers, emptyIndex):
  14.     block1=[0,1,2,9,10,11,18,19,20]
  15.     block2=[3,4,5,12,13,14,21,22,23]
  16.     block3=[6,7,8,15,16,17,24,25,26]
  17.     block4=[27,28,29,36,37,38,45,46,47]
  18.     block5=[30,31,32,39,40,41,48,49,50]
  19.     block6=[33,34,35,42,43,44,51,52,53]
  20.     block7=[54,55,56,63,64,65,72,73,74]
  21.     block8=[57,58,59,66,67,68,75,76,77]
  22.     block9=[60,61,62,69,70,71,78,79,80]
  23.     if emptyIndex in block1:
  24.         return [Numbers[i]for i in block1]
  25.     if emptyIndex in block2:
  26.         return [Numbers[i]for i in block2]
  27.     if emptyIndex in block3:
  28.         return [Numbers[i]for i in block3]
  29.     if emptyIndex in block4:
  30.         return [Numbers[i]for i in block4]
  31.     if emptyIndex in block5:
  32.         return [Numbers[i]for i in block5]
  33.     if emptyIndex in block6:
  34.         return [Numbers[i]for i in block6]
  35.     if emptyIndex in block7:
  36.         return [Numbers[i]for i in block7]
  37.     if emptyIndex in block8:
  38.         return [Numbers[i]for i in block8]
  39.     if emptyIndex in block9:
  40.         return [Numbers[i]for i in block9]                
  41.        
  42.     return None
  43.  
  44. def sudokuSolve(Numbers):
  45.     notin=[]
  46.     for i in range(0,80):
  47.         if Numbers[i]==[""]:
  48.                 for j in range(1,9):
  49.                     if j not in columns(Numbers,i) and j not in rows(Numbers,i) and j not in blocks(Numbers,i):
  50.                         notin+=[j]
  51.                         notin=set(notin)
  52.                         notin=list(notin)
  53.                 if len(notin)==1:
  54.                     Numbers[i]=notin[0]
  55.                     print "Number found: ", notin[0]
  56.                     notin=[]
  57.                 else:
  58.                     Numbers[i]=notin
  59.                     print("Multiple possibilities: ", notin)
  60.                     notin=[]
  61.                
  62.     while any(isinstance(x, list) for x in Numbers) or [""] in Numbers:
  63.        
  64.         for i in range(0,80):
  65.             if Numbers[i]==[""] or any(isinstance(x,list)for x in Numbers):
  66.                 for j in range(1,9):
  67.                     if j not in columns(Numbers,i) and j not in rows(Numbers,i) and j not in blocks(Numbers,i):
  68.                         notin+=[j]
  69.                         notin=set(notin)
  70.                         notin=list(notin)
  71.                 if len(notin)==1:
  72.                     Numbers[i]=notin[0]
  73.                     print "Number found: ", Numbers[i]
  74.                     notin=[]
  75.                 else:
  76.                     Numbers[i]=notin
  77.                     print"Multiple possibilities: ", notin
  78.                     notin=[]
  79.     return Numbers
  80. print sudokuSolve([[""],2,[""],5,[""],1,4,[""],3,1,[""],3,[""],6,4,[""],[""],7,[""],[""],4,3,[""],[""],[""],5,1,4,[""],[""],[""],[""],[""],5,1,6,[""],6,[""],[""],1,3,[""],4,[""],7,1,[""],[""],4,5,3,[""],2,[""],9,2,1,[""],6,[""],[""],[""],8,7,6,4,3,[""],1,2,[""],[""],[""],[""],[""],7,[""],6,[""],[""]])
  81. """[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])
  82. ["","","","","","",6,8,"","","","","",7,3,"","",9,3,"",9,"","","","",4,5,4,9,"","","","","","",8,"",3,"",5,"",9,"",2,"","","","","","","",3,6,9,6,"","","","",3,"",8,7,"","",6,8,"","","","","",2,8,"","","","","","",""])
  83. list(raw_input("Sudoku Solver: Input all numbers in the unsolved sudoku with spacers, commas, and square brackets (yes really)")))"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement