Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Set'
Browse files Browse the repository at this point in the history
  • Loading branch information
jprawiharjo committed Oct 31, 2014
2 parents 33db378 + 82f2cec commit 82fd4ad
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 65 deletions.
1 change: 0 additions & 1 deletion setupGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
@author: jerry.prawiharjo
"""

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
Expand Down
129 changes: 65 additions & 64 deletions sudoku_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import os
import sys
from collections import *
import time

inFN = "sudoku1.txt"
outFN = "sudoku1-sol.txt"
inFN = "sudoku3.txt"
outFN = "sudoku3-sol.txt"

class Sudoku(object):
__X = 'ABCDEFGHI'
Expand Down Expand Up @@ -161,7 +162,7 @@ def write_csv(self,FileName,verbose = False, Solution = True):
for kx in OutGrid:
temp = str(kx)
temp = temp[1:-1]
Wf.write(temp + '\r\n')
Wf.write(temp.replace(' ','') + '\n')
Wf.close()
if verbose: print "Successfully saved output file"
return True
Expand Down Expand Up @@ -192,24 +193,24 @@ def __Singularity(self):

def __ReduceProblemSpace(self):
for kx in self.__SudokuBoxes:
Nrange = range(1,10)
Nrange = set(range(1,10))
Box = []
for ki in kx:
Box.append(self.__SudokuDict[ki])
if Box.count(0) > 0:
Nrange = list(set(Nrange) - set(Box))
Nrange = Nrange - set(Box)
while Box.count(0) > 0:
self.__SudokuDict[kx[Box.index(0)]] = Nrange[:]
Box[Box.index(0)] = Nrange[:]
self.__SudokuDict[kx[Box.index(0)]] = Nrange.copy()
Box[Box.index(0)] = list(Nrange.copy())

Removed = True
while Removed:
for kx in self.__SudokuBoxes:
Removed = False
for ki in kx:
if isinstance(self.__SudokuDict[ki],list):
if isinstance(self.__SudokuDict[ki],set):
if len(self.__SudokuDict[ki]) == 1:
self.__SudokuDict[ki] = self.__SudokuDict[ki][0]
self.__SudokuDict[ki] = self.__SudokuDict[ki].pop()
else:
for mm in range(2):
if mm == 0:
Expand All @@ -218,14 +219,14 @@ def __ReduceProblemSpace(self):
Indexer = [x+ki[1] for x in self.__X]
Indexer.remove(ki)
for kR in Indexer:
if not isinstance(self.__SudokuDict[kR],list) and isinstance(self.__SudokuDict[ki],list):
if not isinstance(self.__SudokuDict[kR],set) and isinstance(self.__SudokuDict[ki],set):
if len(self.__SudokuDict[ki]) > 1:
orig = self.__SudokuDict[ki]
self.__SudokuDict[ki] = sorted(set(self.__SudokuDict[ki]) - set([self.__SudokuDict[kR]]))
self.__SudokuDict[ki] = self.__SudokuDict[ki] - set([self.__SudokuDict[kR]])
if orig != self.__SudokuDict[ki]:
Removed = True
if len(self.__SudokuDict[ki]) == 1:
self.__SudokuDict[ki] = self.__SudokuDict[ki][0]
self.__SudokuDict[ki] = self.__SudokuDict[ki].pop()
elif len(self.__SudokuDict[ki]) == 0:
self.__SudokuDict[ki] = 0

Expand All @@ -237,15 +238,15 @@ def __FindUniqueValue(self):
for kx in kI:
NonUniqueList = []
for ki in kx:
if isinstance(self.__SudokuDict[ki],list):
NonUniqueList.extend(self.__SudokuDict[ki])
if isinstance(self.__SudokuDict[ki],set):
NonUniqueList.extend(list(self.__SudokuDict[ki]))
else:
NonUniqueList.append(self.__SudokuDict[ki])
UniqueList = sorted(set(NonUniqueList))
UniqueList = set(NonUniqueList)
for kb in UniqueList:
if NonUniqueList.count(kb) == 1:
for ki in kx:
if isinstance(self.__SudokuDict[ki],list):
if isinstance(self.__SudokuDict[ki],set):
if kb in self.__SudokuDict[ki]:
self.__SudokuDict[ki] = kb
Found = True
Expand All @@ -255,46 +256,43 @@ def __EliminateOccupiedValues(self):
for mm in range(0,2):
for kx in self.__SudokuBoxes:
if mm == 0:
R1 = kx[0:3]
R2 = kx[3:6]
R3 = kx[6:]
R1 = set(kx[0:3])
R2 = set(kx[3:6])
R3 = set(kx[6:])
else:
R1 = kx[0::3]
R2 = kx[1::3]
R3 = kx[2::3]
x1 = []
x2 = []
x3 = []
R1 = set(kx[0::3])
R2 = set(kx[1::3])
R3 = set(kx[2::3])
x1 = set([])
x2 = set([])
x3 = set([])
for x in R1:
if isinstance(self.__SudokuDict[x],list):
x1.extend(self.__SudokuDict[x])
if isinstance(self.__SudokuDict[x],set):
x1.union(self.__SudokuDict[x])
for x in R2:
if isinstance(self.__SudokuDict[x],list):
x2.extend(self.__SudokuDict[x])
if isinstance(self.__SudokuDict[x],set):
x2.union(self.__SudokuDict[x])
for x in R3:
if isinstance(self.__SudokuDict[x],list):
x3.extend(self.__SudokuDict[x])
x1 = set(x1)
x2 = set(x2)
x3 = set(x3)
if isinstance(self.__SudokuDict[x],set):
x3.union(self.__SudokuDict[x])
if len(x1.intersection(x2)) == 0 and len(x2.intersection(x3)) ==0 and len(x1.intersection(x3)) == 0:
if mm == 0:
CC1= list(set([R1[0][0] + x for x in self.__Y]) - set(R1))
CC2= list(set([R2[0][0] + x for x in self.__Y]) - set(R2))
CC3= list(set([R3[0][0] + x for x in self.__Y]) - set(R3))
CC1= set([sorted(R1)[0][0] + x for x in self.__Y]) - R1
CC2= set([sorted(R2)[0][0] + x for x in self.__Y]) - R2
CC3= set([sorted(R3)[0][0] + x for x in self.__Y]) - R3
else:
CC1= list(set([x + R1[0][1] for x in self.__X]) - set(R1))
CC2= list(set([x + R2[0][1] for x in self.__X]) - set(R2))
CC3= list(set([x + R3[0][1] for x in self.__X]) - set(R3))
CC1= set([x + sorted(R1)[0][1] for x in self.__X]) - R1
CC2= set([x + sorted(R2)[0][1] for x in self.__X]) - R2
CC3= set([x + sorted(R3)[0][1] for x in self.__X]) - R3

Z = [[CC1,x1],[CC2,x2],[CC3,x3]]
for kz in Z:
for kc in kz[0]:
if isinstance(self.__SudokuDict[kc],list):
if isinstance(self.__SudokuDict[kc],set):
if len(self.__SudokuDict[kc]) > 1:
self.__SudokuDict[kc] = sorted(set(self.__SudokuDict[kc]) - kz[1])
self.__SudokuDict[kc] = self.__SudokuDict[kc] - kz[1]
if len(self.__SudokuDict[kc]) == 1:
self.__SudokuDict[kc] = self.__SudokuDict[kc][0]
self.__SudokuDict[kc] = self.__SudokuDict[kc].pop()
elif len(self.__SudokuDict[kc]) == 0:
self.__SudokuDict[kc] = 0
self.__RemoveDuplicates()
Expand All @@ -305,18 +303,18 @@ def __RemoveDuplicates(self):
for kI in self.__SudokuIterables:
Removed = False
for kx in kI:
Members = []
Members = set([])
for ky in kx:
if not isinstance(self.__SudokuDict[ky],list):
Members.append(self.__SudokuDict[ky])
if not isinstance(self.__SudokuDict[ky],set):
Members.add(self.__SudokuDict[ky])
for ky in kx:
if isinstance(self.__SudokuDict[ky],list):
orig = self.__SudokuDict[ky][:]
self.__SudokuDict[ky] = sorted(set(self.__SudokuDict[ky]) - set(Members))
if isinstance(self.__SudokuDict[ky],set):
orig = self.__SudokuDict[ky].copy()
self.__SudokuDict[ky] = self.__SudokuDict[ky] - Members
if orig != self.__SudokuDict[ky]:
Removed = True
if len(self.__SudokuDict[ky]) == 1:
self.__SudokuDict[ky] = self.__SudokuDict[ky][0]
self.__SudokuDict[ky] = self.__SudokuDict[ky].pop()
elif len(self.__SudokuDict[ky]) == 0:
self.__SudokuDict[ky] = 0

Expand All @@ -329,17 +327,16 @@ def __DoubleValueStrategy(self):
Index = []
Members = []
for ky in kx:
if isinstance(self.__SudokuDict[ky],list):
if isinstance(self.__SudokuDict[ky],set):
if len(self.__SudokuDict[ky]) == 2:
Index.append(ky)
Members.append(self.__SudokuDict[ky])
if len(Index) == 2 and Members[0] == Members[1]:
kxx = list(kx[:])
kxx = sorted(set(kxx) - set(Index))
kxx = set(kx) - set(Index)
for ky in kxx:
if isinstance(self.__SudokuDict[ky],list):
orig = self.__SudokuDict[ky][:]
self.__SudokuDict[ky] = sorted(set(self.__SudokuDict[ky]) - set(Members[0]))
if isinstance(self.__SudokuDict[ky],set):
orig = self.__SudokuDict[ky].copy()
self.__SudokuDict[ky] = self.__SudokuDict[ky] - set(Members[0])
if orig != self.__SudokuDict[ky]:
Removed = True
if len(self.__SudokuDict[ky]) ==0:
Expand All @@ -349,7 +346,7 @@ def __CountEmptyCell(self):
k = 0
for kx in self.__SudokuBoxes:
for ki in kx:
if isinstance(self.__SudokuDict[ki],list):
if isinstance(self.__SudokuDict[ki],set):
k += 1
return k

Expand Down Expand Up @@ -387,10 +384,12 @@ def __LogicSolve(self):
def __BruteForceSearch(self):
Odict, Qout = self.__Branch()

for Iq in Qout[1]:
for kq in range(len(Qout[1])):
Iq = Qout[1].pop()
self.__SudokuDict[Qout[0]] = Iq
self.__RemoveDuplicates()
self.__LogicSolve()
self.__FindUniqueValue()
self.__DoubleValueStrategy()
if self.__CountEmptyCell() == 0:
self.__SolutionDictToList()
if self.__Metric(self.__SolutionGrid) == 0:
Expand All @@ -412,9 +411,8 @@ def __Branch(self):
kI = next(Q)
except StopIteration:
break
if isinstance(kI[1],list):
if isinstance(kI[1],set):
break

return Original,kI

def Solve(self, verbose=False):
Expand All @@ -439,9 +437,9 @@ def Solve(self, verbose=False):
self.Solved = False
return False
else:
if verbose: print "Failed to find solution!"
self.Solved = False
return False
if verbose: print "Failed to find solution!"
self.Solved = False
return False
else:
if verbose: print "Problem is ill-posed. There are duplicate values."
return False
Expand Down Expand Up @@ -492,6 +490,9 @@ def __Metric(self,inGrid):

if A.CheckOutputFileExists(outFN):
if A.read_csv(inFN,verbose = True):
tstart = time.clock()
A.Solve(verbose = True)
telapsed = time.clock() - tstart
print telapsed
A.write_csv(outFN,verbose = True)

0 comments on commit 82fd4ad

Please sign in to comment.