Skip to content

Instantly share code, notes, and snippets.

@mie00
Created March 22, 2014 16:06
Show Gist options
  • Save mie00/9709521 to your computer and use it in GitHub Desktop.
Save mie00/9709521 to your computer and use it in GitHub Desktop.
nine-liner sudoku solver
def complement(arr):
return filter(lambda x:x not in arr,range(1,10))
def possibilities(a,arr):
x,y=a%9,a/9
return complement(
map(lambda k:arr[k],
range(x,81,9)+range(9*y,9*(y+1))+
map(lambda z:z+(x-x%3)+(y-y%3)*9,[0,1,2,9,10,11,18,19,20])
)
)
def first(fn,arr): #returns the first non empty value from applying every element of the list to fn
return None if not arr else fn(arr[0]) or first(fn,arr[1:])
def recurr(arr): #takes sudoku as a list of 81 integers with 0 indicationg an empty square
return arr if 0 not in arr else \
(lambda a:first(lambda x:recurr(arr[:a]+[x]+arr[a+1:]),possibilities(a,arr)))(arr.index(0))
print recurr(map(int,list("000600057600000000708100000005000104012405000000000000980001400026000009000390006")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment