Created
March 30, 2020 20:26
-
-
Save dzaczek/5117bbbc9d5e8e6c2c545b0e04541d7f to your computer and use it in GitHub Desktop.
sudoku
This file contains 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
#!/usr/bin/python3.7 | |
import numpy as np | |
import os | |
import time | |
#grid=[ [0,0,0,2,6,0,7,0,1],[6,8,0,0,7,0,0,9,0],[1,9,0,0,0,4,5,0,0],[8,2,0,1,0,0,0,4,0],[0,0,4,6,0,2,9,0,0],[0,5,0,0,0,3,0,2,8],[0,0,9,3,0,0,0,7,4],[0,4,0,0,5,0,0,3,6],[7,0,3,0,1,8,0,0,0] ] | |
#grid=np.zeros((9, 9)) | |
#World's hardest sudoku | |
grid=[[8,0,0,0,0,0,0,0,0],[0,3,6,0,0,0,0,0,0],[0,7,0,0,9,0,2,0,0],[0,5,0,0,0,7,0,0,0],[0,0,0,0,4,5,7,0,0],[0,0,0,1,0,0,0,3,0],[0,0,1,0,0,0,0,6,8],[0,0,8,5,0,0,0,1,0],[0,9,0,0,0,0,4,0,0]] | |
print(np.matrix(grid)) | |
def possible(y,x,n): | |
global grid | |
for i in range(0,9): | |
if grid[y][i]==n: | |
return False | |
for i in range(0,9): | |
if grid[i][x]==n: | |
return False | |
x0 = (x//3)*3 | |
y0 = (y//3)*3 | |
for i in range(0,3): | |
for j in range(0,3): | |
if grid[y0+i][x0+j] == n: | |
return False | |
return True | |
def solve(): | |
start_time = time.time() | |
global grid | |
for y in range(9): | |
for x in range(9): | |
if grid[y][x]==0: | |
for n in range(1,10): | |
if possible(y,x,n): | |
grid[y][x]=n | |
os.system('clear') | |
print(np.matrix(grid)) | |
time.sleep(0.2) | |
solve() | |
grid[y][x]=0 | |
return | |
print("FINISH:") | |
print("--- %s seconds ---" % (time.time() - start_time)) | |
print(np.matrix(grid)) | |
solve() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment