Skip to content

Instantly share code, notes, and snippets.

@ayemos
Created May 24, 2015 09:51
Show Gist options
  • Save ayemos/d49fa9ec99e2e3fb0472 to your computer and use it in GitHub Desktop.
Save ayemos/d49fa9ec99e2e3fb0472 to your computer and use it in GitHub Desktop.
'''
input >
---61----
2--7-8---
-9-4--65-
8-----3-7
97123-5--
-2386-941
318942765
759-86432
-4---5-1-
'''
def __get(x, y, grid):
return grid[y][x]
def __set(x, y, i, grid):
grid[y][x] = i
def check(grid):
for y in range(9):
hori = set()
for x in range(9):
hori.add(__get(x, y, grid))
if(len(hori) < 9):
return False
for x in range(9):
vert = set()
for y in range(9):
vert.add(__get(x, y, grid))
if(len(vert) < 9):
return False
return True
def dump(grid):
print("=========")
for y in range(9):
print(grid[y])
print("=========")
def copy(grid):
res = []
for y in range(9):
res.append(list(grid[y]))
return res
problem = []
for i in range(9):
tmp = []
for c in list(raw_input()):
tmp.append(c)
problem.append(tmp)
import random
while(True):
grid = copy(problem)
for x in range(9):
for y in range(9):
c = __get(x, y, grid)
if(c == '-'):
__set(x, y, str(random.randint(1, 9)), grid)
else:
__set(x, y, c, grid)
dump(grid)
if(check(grid)):
print("Success!")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment