fork download
  1. # File: main.py
  2. # Author: Shawn Wilkinson
  3.  
  4. # Imports
  5. import time
  6.  
  7. # Welcome Message
  8. print("Welcome to Sudoku-Solver.")
  9.  
  10. # -- Main --
  11.  
  12. # Create two 9 x 9 Boards of Zeroes
  13. board = [[0 for col in range(9)] for row in range(9)]
  14. hints = [['' for col in range(9)] for row in range(9)]
  15.  
  16. # Sample Data - Set 1
  17. board[0][5] = 1
  18. board[0][6] = 5
  19. board[0][7] = 6
  20. board[0][8] = 3
  21.  
  22. board[1][2] = 7
  23. board[1][3] = 5
  24. board[1][5] = 3
  25.  
  26. board[2][3] = 4
  27. board[2][4] = 2
  28. board[2][5] = 8
  29. board[2][7] = 7
  30.  
  31. # Sample Data - Set 2
  32. board[3][1] = 3
  33. board[3][3] = 9
  34. board[3][4] = 7
  35. board[3][5] = 4
  36. board[3][6] = 6
  37. board[3][7] = 2
  38. board[3][8] = 1
  39.  
  40. board[4][1] = 9
  41. board[4][2] = 1
  42. board[4][3] = 2
  43. board[4][8] = 4
  44.  
  45. board[5][3] = 8
  46. board[5][6] = 3
  47. board[5][7] = 9
  48.  
  49. # Sample Data - Set 3
  50. board[6][1] = 2
  51. board[6][3] = 6
  52. board[6][4] = 5
  53. board[6][8] = 8
  54.  
  55. board[7][4] = 8
  56. board[7][6] = 4
  57. board[7][7] = 1
  58. board[7][8] = 6
  59.  
  60. board[8][3] = 1
  61. board[8][5] = 9
  62. board[8][6] = 2
  63. board[8][7] = 5
  64. board[8][8] = 7
  65.  
  66. # Fill Hints on Empty Spaces
  67. for x in range(9):
  68. for y in range(9):
  69. if board[x][y] == 0:
  70. hints[x][y] = '123456789'
  71.  
  72. # Infinite Solving Loop
  73. while 1:
  74. # Eliminate Horizontal Hint Duplicates
  75. for x in range(9):
  76. # Create an Empty List
  77. list = []
  78. # Get Values for That Row and Add to List
  79. for y in range(9):
  80. if board[x][y] != 0:
  81. list.append(board[x][y])
  82. # Loop through and remove that row from hints
  83. for y in range(9):
  84. for chr in list:
  85. hints[x][y] = hints[x][y].replace(str(chr),'')
  86.  
  87. # Eliminate Vertical Hint Duplicates
  88. for x in range(9):
  89. # Create an Empty List
  90. list = []
  91. # Get Values for That Row and Add to List
  92. for y in range(9):
  93. if board[y][x] != 0:
  94. list.append(board[y][x])
  95. # Loop through and remove that row from hints
  96. for y in range(9):
  97. for chr in list:
  98. hints[y][x] = hints[y][x].replace(str(chr),'')
  99.  
  100. # Sub-squares Function
  101. def subSquare(square):
  102. list = []
  103. if square == 0:
  104. for x in range(3):
  105. for y in range(3):
  106. if board[y][x] != 0:
  107. list.append(board[y][x])
  108. for y in range(3):
  109. for chr in list:
  110. hints[y][x] = hints[y][x].replace(str(chr),'')
  111. elif square == 1:
  112. for x in range(3,6):
  113. for y in range(3):
  114. if board[y][x] != 0:
  115. list.append(board[y][x])
  116. for y in range(3):
  117. for chr in list:
  118. hints[y][x] = hints[y][x].replace(str(chr),'')
  119. elif square == 2:
  120. for x in range(6,9):
  121. for y in range(3):
  122. if board[y][x] != 0:
  123. list.append(board[y][x])
  124. for y in range(3):
  125. for chr in list:
  126. hints[y][x] = hints[y][x].replace(str(chr),'')
  127.  
  128. elif square == 3:
  129. for x in range(3):
  130. for y in range(3,6):
  131. if board[y][x] != 0:
  132. list.append(board[y][x])
  133. for y in range(3,6):
  134. for chr in list:
  135. hints[y][x] = hints[y][x].replace(str(chr),'')
  136. elif square == 4:
  137. for x in range(3,6):
  138. for y in range(3,6):
  139. if board[y][x] != 0:
  140. list.append(board[y][x])
  141. for y in range(3,6):
  142. for chr in list:
  143. hints[y][x] = hints[y][x].replace(str(chr),'')
  144. elif square == 5:
  145. for x in range(6,9):
  146. for y in range(3,6):
  147. if board[y][x] != 0:
  148. list.append(board[y][x])
  149. for y in range(3,6):
  150. for chr in list:
  151. hints[y][x] = hints[y][x].replace(str(chr),'')
  152.  
  153. elif square == 6:
  154. for x in range(3):
  155. for y in range(6,9):
  156. if board[y][x] != 0:
  157. list.append(board[y][x])
  158. for y in range(6,9):
  159. for chr in list:
  160. hints[y][x] = hints[y][x].replace(str(chr),'')
  161. elif square == 7:
  162. for x in range(3,6):
  163. for y in range(6,9):
  164. if board[y][x] != 0:
  165. list.append(board[y][x])
  166. for y in range(6,9):
  167. for chr in list:
  168. hints[y][x] = hints[y][x].replace(str(chr),'')
  169. elif square == 8:
  170. for x in range(6,9):
  171. for y in range(6,9):
  172. if board[y][x] != 0:
  173. list.append(board[y][x])
  174. for y in range(6,9):
  175. for chr in list:
  176. hints[y][x] = hints[y][x].replace(str(chr),'')
  177.  
  178.  
  179. # Eliminate Sub-squares Hint Duplicates
  180. for i in range(9):
  181. pass
  182. #subSquare(i)
  183.  
  184. # Solve Single Hints
  185. for x in range(9):
  186. for y in range(9):
  187. if len(hints[x][y]) == 1:
  188. # Say Solved and Location
  189. print("Solved:", (x+1), ",", (y+1))
  190. # Set Hint to Board
  191. board[x][y] = int(hints[x][y])
  192. # Clear Hint
  193. hints[x][y] = ''
  194.  
  195. # Check For a Solved Board
  196. exitLoop = True
  197. for x in range(9):
  198. for y in range(9):
  199. # If a non-blank value is found then don't exit the loop
  200. if board[x][y] != 0:
  201. exitLoop = False
  202. # else: exit loop
  203. if exitLoop:
  204. break # Break infinite while loop
  205.  
  206.  
  207. # Pause
  208. time.sleep(1)
  209. break
  210.  
  211. # Print Board and Hints
  212. for row in board:
  213. print(row)
  214. for row in hints:
  215. print(row)
Success #stdin #stdout 0.03s 6192KB
stdin
Standard input is empty
stdout
Welcome to Sudoku-Solver.
Solved: 1 , 4
Solved: 7 , 6
[0, 0, 0, 7, 0, 1, 5, 6, 3]
[0, 0, 7, 5, 0, 3, 0, 0, 0]
[0, 0, 0, 4, 2, 8, 0, 7, 0]
[0, 3, 0, 9, 7, 4, 6, 2, 1]
[0, 9, 1, 2, 0, 0, 0, 0, 4]
[0, 0, 0, 8, 0, 0, 3, 9, 0]
[0, 2, 0, 6, 5, 7, 0, 0, 8]
[0, 0, 0, 0, 8, 0, 4, 1, 6]
[0, 0, 0, 1, 0, 9, 2, 5, 7]
['24789', '478', '2489', '', '49', '', '', '', '']
['124689', '1468', '', '', '1469', '', '189', '48', '29']
['13569', '156', '3569', '', '', '', '19', '', '59']
['58', '', '58', '', '', '', '', '', '']
['35678', '', '', '', '36', '567', '78', '38', '']
['124567', '14567', '2456', '', '146', '2567', '', '', '25']
['13479', '', '349', '', '', '', '179', '34', '']
['23579', '57', '2359', '37', '', '257', '', '', '']
['3468', '468', '3468', '', '346', '', '', '', '']