Skip to content

Instantly share code, notes, and snippets.

@larsr
Last active November 8, 2018 10:40
Show Gist options
  • Save larsr/5487603 to your computer and use it in GitHub Desktop.
Save larsr/5487603 to your computer and use it in GitHub Desktop.
If you want to write a sudoku solver, you can start with this code. It loads a game from a string, and can pretty-print the "possible values" of the fields.
# sudoku-starter-kit, lars.rasmusson@gmail.com, 2013-04-30
game = '.8...6...|32.9.47..|.4...7.6.|93..1....|......8.9|8..2.....|...4..17.|.7...89..|..4......'
def rc(r,c): return (r-1)*9+(c-1)
def init_sud(sud):
v=[]
for c in sud:
if c in "123456789": v.append([int(c)])
elif c in ".": v.append(range(1,10))
assert(len(v)==81)
return v
def pprint(v):
for rr in range(27):
ss=""
for cc in range(27):
r,c=rr//3+1,cc//3+1
i=3*(rr%3)+(cc%3)+1
ss += str(i) if i in v[rc(r,c)] else '.'
if (cc+1)%3==0: ss+=' '
if cc+1 in [9,18] : ss += ' | '
print(ss)
if (rr+1)%3==0: print()
if rr+1 in [9,18] : print('-'*41+'\n')
v=init_sud(game)
pprint(v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment