Commits

Shlomi Fish committed 0d5fbb7

Extract calc_params_obj().

Comments (0)

Files changed (1)

kakurasu-solver/kakurasu-solver.py

             sys.stdout.write ('█' if (cell != 0) else '⨯')
         sys.stdout.write("\n")
 
+class Params:
+    pass
+
 class Solver:
     def __init__(self, width, height):
         self.width = width
                 self.vert_constraints.append(int(v))
                 self.num_known_vert_constraints += 1
 
+    def calc_params_obj(self):
+        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))
+
+        eq = 0
+        y = 0
+        for h_eq in range(0,num_known_horiz_constraints):
+            while (horiz_constraints[y] == '?'):
+                y += 1
+            ret.b_vector.append(horiz_constraints[y])
+            y += 1
+            ret.e_vector.append(0)
+            eq += 1
+        x = 0
+        for v_eq in range(0,num_known_vert_constraints):
+            while (vert_constraints[x] == '?'):
+                x += 1
+            ret.b_vector.append(vert_constraints[x])
+            x += 1
+            ret.e_vector.append(0)
+            eq += 1
+
+        return ret
+
+
+        
 def kakurasu_main(args):
     input_fn = args.pop(1)
 
     solver_obj = Solver.parse_input_file(input_fn)
-    sol = solve_file(solver_obj)
+    sol = solve_object(solver_obj)
 
     print_sol(sol)
 
     return 0
 
-def solve_file(solve_obj):
+def solve_object(solve_obj):
 
-    width = solve_obj.width
-    height = solve_obj.height
-    num_known_vert_constraints = solve_obj.num_known_vert_constraints
-    num_known_horiz_constraints = solve_obj.num_known_horiz_constraints
-    horiz_constraints = solve_obj.horiz_constraints
-    vert_constraints = solve_obj.vert_constraints
+    lp_solve_params = solve_obj.calc_params_obj()
 
-    a_matrix = []
-    f_vector = []
-    b_vector = []
-    e_vector = []
-    lower_bounds_vector = []
-    upper_bounds_vector = []
-    xint_vector = []
-
-    for m in range(0,num_known_vert_constraints+num_known_horiz_constraints):
-        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
-
-            f_vector.append(1)
-
-            var_num = height * y + x
-            
-            if (horiz_constraints[y] != '?'):
-                a_matrix[y_calc][var_num] = x+1
-
-            if (vert_constraints[x] != '?'):
-                a_matrix[num_known_horiz_constraints+x_calc][var_num] = y+1
-            
-            lower_bounds_vector.append(0)
-            upper_bounds_vector.append(1)
-            xint_vector.append(len(f_vector))
-
-    eq = 0
-    y = 0
-    for h_eq in range(0,num_known_horiz_constraints):
-        while (horiz_constraints[y] == '?'):
-            y += 1
-        b_vector.append(horiz_constraints[y])
-        y += 1
-        e_vector.append(0)
-        eq += 1
-    x = 0
-    for v_eq in range(0,num_known_vert_constraints):
-        while (vert_constraints[x] == '?'):
-            x += 1
-        b_vector.append(vert_constraints[x])
-        x += 1
-        e_vector.append(0)
-        eq += 1
-    
-    ret = lp_solve(f_vector, a_matrix, b_vector, e_vector, \
-            lower_bounds_vector, upper_bounds_vector, xint_vector)
+    lp_solve_ret = lp_solve(
+            lp_solve_params.f_vector,
+            lp_solve_params.a_matrix, 
+            lp_solve_params.b_vector, 
+            lp_solve_params.e_vector,
+            lp_solve_params.lower_bounds_vector, 
+            lp_solve_params.upper_bounds_vector, 
+            lp_solve_params.xint_vector
+    )
    
-    flat_sol = ret[1]
+    flat_sol = lp_solve_ret[1]
 
     if (len(flat_sol) == 0):
         raise "Could not find a solution for this puzzle."
 
+    width  = solve_obj.width
+    height = solve_obj.height
+
     return [[flat_sol[y*width+x] for x in range(width)] for y in range(height)]
 
 
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.