Browse Source

counting now works

tarfeef101 6 years ago
parent
commit
f1f7e60d78
1 changed files with 16 additions and 13 deletions
  1. 16 13
      a1/sudoku.py

+ 16 - 13
a1/sudoku.py

@@ -72,13 +72,12 @@ def naive(start):
         # iterate over all values in the domain list
         while solution[index[0]][index[1]]:
             i = solution[index[0]][index[1]].pop()
-            count += 1
-            # took too long
-            if (count >= 10,000):
-              print("took too long")
-              return False
             # check if this part of the domain(solution) is valid
             if (valid(working, index[0], index[1], i)):
+                count += 1
+                if (count >= 10000):
+                  print("took too long")
+                  return False
                 solution[index[0]][index[1]].append(i) # keep in the domain
                 working[index[0]][index[1]] = i
                 assumptions.append(index)
@@ -147,12 +146,12 @@ def solve(working, domains, unassigned, count):
     # for every value in the domain, check if using it works. if all fail, backtrack.
     for i in domains[index[0]][index[1]]:
         working[index[0]][index[1]] = i
+        newdomains = infer(domains, working, index[0], index[1], i)
         count += 1
         # took too long
-        if (count >= 10,000):
+        if (count >= 10000):
           print("took too long")
           return False
-        newdomains = infer(domains, working, index[0], index[1], i)
         result = solve(working, newdomains, copy.deepcopy(unassigned), count)
         if (result):
             return result
@@ -284,12 +283,6 @@ def solveh(working, domains, unassigned, count):
         working[index[0]][index[1]] = val
         unassigned.remove(index)
         
-        count += 1
-        # took too long
-        if (count >= 10,000):
-          print("took too long")
-          return False
-    
         # check for invalidated nodes (empty domain)
         flag = True
         result = False
@@ -299,6 +292,12 @@ def solveh(working, domains, unassigned, count):
                 if (not domains[i][j]):
                     flag = False
         
+        count += 1
+        # took too long
+        if (count >= 10000):
+          print("took too long")
+          return False
+
         # success! recurse
         if (flag): result = solveh(working, newdomains, copy.deepcopy(unassigned), count)
         if (result):
@@ -345,6 +344,10 @@ def main():
     else:
       print("No valid algorithm selected. RIP.")
 
+    if (not result):
+      print("No board to print")
+      return
+
     print("###########")
     print(*result, sep='\n')
     print("##########")