Advertisement
here2share

# basic_valid_sudoku_gen.py ZZZ

Nov 8th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. # basic_valid_sudoku_gen.py -- ZZZ *generator 2 replacement now avails!
  2.  
  3. import random
  4. import time
  5.  
  6. Lv1 = 36
  7. Lv2 = 32
  8. Lv3 = 29
  9. Lv4 = 27
  10.  
  11. hints = 81-Lv4
  12. def progress(z):
  13.     sec=time.time()+1
  14.     '''while sec-time.time() > 0:
  15.         pass'''
  16.     for i in range(9):
  17.         print
  18.         for ii in range(9):
  19.             print z[i*9+ii],
  20.     print
  21.     print
  22. def twodee(z):
  23.     return [[z[ii*9+i] for i in range(9)] for ii in range(9)]
  24. def shfl(z):
  25.     z=zip(*z)
  26.     z=[list(i) for i in z]
  27.     z=list([z[i*3:i*3+3] for i in range(3)])
  28.     #'''
  29.     for i in range(3):
  30.         random.shuffle(z[i])
  31.     random.shuffle(z)
  32.     #'''
  33.     return sum(sum(z,[]),[])
  34. def rcb(b,x,c):
  35.     i,j=divmod(c, 9)
  36.     ii, jj=i-i%3, j-j%3
  37.     b=twodee(b)
  38.     row = b[i]
  39.     col = [r[j] for r in b]
  40.     block = sum([r[jj:jj+3] for r in b[ii:ii+3]],[])
  41.     if str(x) in row + col + block:
  42.         return True
  43.     return False
  44. def rndplot(blank,mask):
  45.     for z in blank:
  46.         if mask.count('.') is hints: break
  47.         mask=plot(z,mask)
  48.     return mask
  49. def plot(z,mask):
  50.     new=mask[:]
  51.     new[z]='.'
  52.     rec=[new[:]]*81
  53.    
  54.     sq=[i for i in range(81) if mask[i] is '.'][::-1]
  55.     c=0
  56.     chk=0
  57.     while c > -1:
  58.         i=sq[c]
  59.         bbb=rec[i][:]
  60.         if bbb[i] is '.': number=1
  61.         else: number=int(bbb[i])+1
  62.         while number < 10:
  63.             if not rcb(bbb,number,i):
  64.                 bbb[i]=str(number)
  65.                 rec[i]=bbb[:]
  66.                 for r in range(1,10):
  67.                     if len(set(bbb[r*9:r*9+9])) is 1:
  68.                         print "???"
  69.                         return mask
  70.                 if c+1 is len(sq):
  71.                     number=99
  72.                     chk+=1
  73.                     if chk > 1:
  74.                         return mask
  75.                 break
  76.             number+=1
  77.         if number != 99:
  78.             if number < 10:
  79.                 c+=1
  80.                 try: rec[sq[c]]=bbb[:]
  81.                 except: pass
  82.             else:
  83.                 c-=1
  84.     return new
  85. def init():
  86.     a='abcdefghxfexghcdabhdgxabefcbxaedgfchghdcfabxeecfhbxagdcfbaxehdgdgefchxbaxahbgdcef'
  87.     #
  88.     numbers=range(1,10)
  89.     #random.shuffle(numbers)
  90.     for i in range(9):
  91.         a=a.replace(a[i],str(numbers[i]))
  92.     solution=shfl(twodee(a))
  93.     progress(solution)
  94.     mask=solution[:]
  95.     mask[:9]=['.']*9
  96.     for i in range(9,9*9,9): mask[i]='.'
  97.     c=str(numbers[0])
  98.     mask=''.join(mask)
  99.     mask=mask.replace(c,'.')
  100.     mask=list(mask)
  101.     blank=[i for i in range(81) if mask[i] is not '.'][::-1]
  102.     random.shuffle(blank)
  103.     mask=rndplot(blank,mask)
  104.     progress(mask)
  105.     print 81-hints, 'hints'
  106. #
  107. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement