Skip to content

Instantly share code, notes, and snippets.

@thfm
Last active December 7, 2023 01:10
Show Gist options
  • Save thfm/8df3a99f84c7feee91e188dd7ebd9607 to your computer and use it in GitHub Desktop.
Save thfm/8df3a99f84c7feee91e188dd7ebd9607 to your computer and use it in GitHub Desktop.
Sudoku Solver (576 bytes)
import copy
e,z=enumerate,range
def s(b):
l=[(x,y)for y,r in e(b)for x,c in e(r)if c==0]
if len(l)==0:
return b
x,y=l[0]
for g in set(z(1,10))-set(b[y]+[r[x]for r in b]+[c for i in z(y//3*3,y//3*3+3)for c in b[i][x//3*3:x//3*3+3]])-{0}:
b[y][x]=g
n=s(copy.deepcopy(b))
if n!=None:
return n
print('\n'.join([' '.join(['-'if c==0 else str(c)for c in r])for r in s([[8,0,1,2,9,6,0,0,0],[3,0,0,0,8,1,7,0,6],[2,0,0,0,0,0,0,9,0],[1,0,2,0,6,5,4,7,9],[0,0,0,1,0,9,0,3,0],[5,9,0,3,0,0,0,6,2],[4,0,0,0,0,0,0,1,3],[9,1,0,0,0,0,6,8,7],[0,8,0,0,0,7,0,5,0]])]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment