SmtC: Show me the Code
Ole Peter Smith
Instituto de Matemática e Estatística
Universidade Federal de Goiás
http://www.olesmith.com.br

Solution Algorithm
O aspecto mais triste da humanidade hoje
É que ela cresce mais em ciência do que em sabedoria.
Isaac Asimov

Solver, Iterate

  1. Locate Uniques and Apply.
    Python Listing: Unique.py.
        def Topology_Uniques_Apply(self,topology):
            res=True
            n=0
            while (res and n<25):
                n+=1
                res=self.Topology_Unique_Apply(topology)
    
            topology=self.Topology_Initial()
    
            return res
    
                
        ##!
        ##! Locate 'uniques'.
        ##!
        
        def Topology_Unique(self,topology):
            uniques=[]
            
            for r in range(self.M):
                for s in range(self.M):
                    if (self.S[r][s]==0):
                        if (len(topology[r][s])==1):
                            uniques.append([r,s,topology[r][s]])
    
            return uniques
        
        ##!
        ##! Apply
        ##!
        
        def Topology_Unique_Apply(self,topology):
            uniques=self.Topology_Unique(topology)
            for unique in uniques:
                r=unique[0]
                s=unique[1]
                value=unique[2].pop()
    
                self.S[r][s]=value
                
            return uniques
    
        
        ##!
        ##! Remove (zero) cell, for puzzle generation.
        ##!
        
    
  2. Locate by Existence and Apply
    Python Listing: Existence.py.
    class Sudoku_Topology_Existence(
        ):
        
        
        def Topology_Existence_Apply(self,topology):
            musts=self.Topology_Existence(topology)
            
            texts=[]
            for must in musts:
                r=must[0]
                s=must[1]
                value=must[2]
                descriptor=must[3]
    
                self.S[r][s]=value
                
                texts.append("S["+str(r)+","+str(s)+"]="+str(value)+" "+descriptor)
                
            if (len(musts)>0):
                print "Setting by Existence:\n\t"+"\n\t".join(texts)
            else:
                print "No more by Existence"
                
            topology=self.Topology_Initial()
    
            return musts
            
        
        def Topology_Existence(self,topology):
            musts=[]
            musts=musts+self.Topology_Existence_Rows(topology)
            musts=musts+self.Topology_Existence_Columns(topology)
            musts=musts+self.Topology_Existence_Boxes(topology)
    
            musts_dict={}
            rmusts=[]
            for must in musts:
                key=str(must[0])+"_"+str(must[1])
                
                if (not musts_dict.has_key(key)):
                    rmusts.append(must)
                    musts_dict[ key ]=True
                    
            return rmusts
        
        def Topology_Existence_Rows(self,topology):
            musts=[]
            for r in range(self.M):
                musts=musts+self.Topology_Existence_Elements(
                    topology,
                    self.Sudoku_Row(r),
                    "R"+str(r+1)
                )
    
            return musts
        
        def Topology_Existence_Columns(self,topology):
            musts=[]
            for s in range(self.M):
                musts=musts+self.Topology_Existence_Elements(
                    topology,
                    self.Sudoku_Column(s),
                    "C"+str(s+1)
                )
    
            return musts
        
        def Topology_Existence_Boxes(self,topology):
            musts=[]
            for rr in range(self.N):
                for ss in range(self.N):
                    musts=musts+self.Topology_Existence_Elements(
                        topology,
                        self.Sudoku_Box(rr,ss),
                        "B"+str(rr+1)+""+str(ss+1)
                    )
    
            return musts
        
        def Topology_Existence_Elements(self,topology,elements,descriptor):
            values=set()
            for element in elements:
                r=element[0]
                s=element[1]
                
                if (self.S[r][s]==0):
                    for value in topology[r][s]:
                        values.add(value)
                
            musts=[]
            for value in values:
                n=0
                rr=ss=-1
    
                for element in elements:
                    r=element[0]
                    s=element[1]
    
                    if (self.S[r][s]==0):
                        if (value in topology[r][s]):
                            n+=1
                            rr=r
                            ss=s
    
                #Unique
                if (n==1):
                    musts.append([rr,ss,value,descriptor])
                    
                   
            return musts
    
  3. Sudoku Puzzle Example
Messages:
0 secs.