Commits

Shlomi Fish committed b4dc5b2

Moved most of _calc_params_obj to Params.

Thanks to Amit Aronovich for the tip which he has given on Python-IL.

Comments (0)

Files changed (1)

kakurasu-solver/kakurasu_solver.py

             self.b_vector.append(constraints[idx])
             idx += 1
             self.e_vector.append(0)
-    
+
+    def __init__(self, width, height,
+            num_known_vert_constraints, num_known_horiz_constraints,
+            horiz_constraints, vert_constraints):
+
+        self.a_matrix = []
+        self.f_vector = []
+        self.b_vector = []
+        self.e_vector = []
+        self.lower_bounds_vector = []
+        self.upper_bounds_vector = []
+        self.xint_vector = []
+
+        for m in range(0,num_known_vert_constraints+num_known_horiz_constraints):
+            self.a_matrix.append([0] * (width*height))
+
+        y_calc = -1
+        for y in range(0,height):
+            if (horiz_constraints[y] != '?'):
+                y_calc += 1
+            x_calc = -1
+            for x in range(0,width):
+                if (vert_constraints[x] != '?'):
+                    x_calc += 1
+
+                self.f_vector.append(1)
+
+                var_num = height * y + x
+
+                if (horiz_constraints[y] != '?'):
+                    self.a_matrix[y_calc][var_num] = x+1
+
+                if (vert_constraints[x] != '?'):
+                    self.a_matrix[num_known_horiz_constraints+x_calc][var_num] \
+                            = y+1
+
+                self.lower_bounds_vector.append(0)
+                self.upper_bounds_vector.append(1)
+                self.xint_vector.append(len(self.f_vector))
+
+        for constraints in [horiz_constraints, vert_constraints]:
+            self._process_constraints(constraints)
+
 class Solver(object):
     '''This class is used to parse the input files, process the data into
     a format that lp_solve can understand and call lp_solve on it.'''
         Calculate the Params object that should be passed to the lp_solve module.
         This fills in the parameters of the equations needed to solve the puzzle.
         '''
-        width = self.width
-        height = self.height
-        num_known_vert_constraints = self.num_known_vert_constraints
-        num_known_horiz_constraints = self.num_known_horiz_constraints
-        horiz_constraints = self.horiz_constraints
-        vert_constraints = self.vert_constraints
 
-        ret = Params()
-
-        ret.a_matrix = []
-        ret.f_vector = []
-        ret.b_vector = []
-        ret.e_vector = []
-        ret.lower_bounds_vector = []
-        ret.upper_bounds_vector = []
-        ret.xint_vector = []
-
-        for m in range(0,num_known_vert_constraints+num_known_horiz_constraints):
-            ret.a_matrix.append([0] * (width*height))
-
-        y_calc = -1
-        for y in range(0,height):
-            if (horiz_constraints[y] != '?'):
-                y_calc += 1
-            x_calc = -1
-            for x in range(0,width):
-                if (vert_constraints[x] != '?'):
-                    x_calc += 1
-
-                ret.f_vector.append(1)
-
-                var_num = height * y + x
-
-                if (horiz_constraints[y] != '?'):
-                    ret.a_matrix[y_calc][var_num] = x+1
-
-                if (vert_constraints[x] != '?'):
-                    ret.a_matrix[num_known_horiz_constraints+x_calc][var_num] \
-                            = y+1
-
-                ret.lower_bounds_vector.append(0)
-                ret.upper_bounds_vector.append(1)
-                ret.xint_vector.append(len(ret.f_vector))
-
-        for constraints in [horiz_constraints, vert_constraints]:
-            ret._process_constraints(constraints)
-
-        return ret
+        return Params(
+                width=self.width,
+                height=self.height,
+                num_known_vert_constraints=self.num_known_vert_constraints,
+                num_known_horiz_constraints=self.num_known_horiz_constraints,
+                horiz_constraints = self.horiz_constraints,
+                vert_constraints = self.vert_constraints
+        )
 
     def solve(self):
         '''
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.