-
Notifications
You must be signed in to change notification settings - Fork 0
/
BruteForce.py
111 lines (88 loc) · 3.82 KB
/
BruteForce.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import math
class BruteForce:
def __init__(self,puzzle):
self.n = 9
self.b = 3
self.puzzle = puzzle
self.num_guesses = 0
self.known_indices = []
def load(self):
for row_index in range(0,9):
for col_index in range(0,9):
if self.puzzle[row_index][col_index] != 0 :
self.known_indices.append((row_index * self.n) + col_index)
self.puzzle = self.puzzle.flatten()
def solve(self):
self.load()
self.num_guesses = 0
r = self.solve_from(0, 1)
while r is not None:
r = self.solve_from(r[0], r[1])
if(r == False ):
return False
def solve_from(self, index, starting_guess):
if index < 0 or index > len(self.puzzle):
#raise Exception("Invalid puzzle index %s after %s guesses" % (index, self.num_guesses))
return False
last_valid_guess_index = None
found_valid_guess = False
for i in range(index, len(self.puzzle)):
if i not in self.known_indices:
found_valid_guess = False
for guess in range(starting_guess, self.n + 1):
self.num_guesses += 1
if self.valid(i, guess):
found_valid_guess = True
last_valid_guess_index = i
self.puzzle[i] = guess
break
starting_guess = 1
if not found_valid_guess:
break
if not found_valid_guess:
new_index = last_valid_guess_index if last_valid_guess_index is not None else index - 1
new_starting_guess = self.puzzle[new_index] + 1
self.reset_puzzle_at(new_index)
while new_starting_guess > self.n or new_index in self.known_indices:
new_index -= 1
new_starting_guess = self.puzzle[new_index] + 1
self.reset_puzzle_at(new_index)
return (new_index, new_starting_guess)
else:
return None
def reset_puzzle_at(self, index):
for i in range(index, len(self.puzzle)):
if i not in self.known_indices:
self.puzzle[i] = 0
def valid_for_row(self, index, guess):
row_index = int(math.floor(index / self.n))
start = self.n * row_index
finish = start + self.n
for c_index in range(start, finish):
if c_index != index and self.puzzle[c_index] == guess:
return False
return True
def valid_for_column(self, index, guess):
col_index = index % self.n
for r in range(0, self.n):
r_index = col_index + (self.n * r)
if r_index != index and self.puzzle[r_index] == guess:
return False
return True
def valid_for_block(self, index, guess):
row_index = int(math.floor(index / self.n))
col_index = index % self.n
block_row = int(math.floor(row_index / self.b))
block_col = int(math.floor(col_index / self.b))
row_start = block_row * self.b
row_end = row_start + self.b - 1
col_start = block_col * self.b
col_end = col_start + self.b - 1
for r in range(row_start, row_end + 1):
for c in range(col_start, col_end + 1):
i = c + (r * self.n)
if self.puzzle[i] == guess:
return False
return True
def valid(self, index, guess):
return self.valid_for_row(index, guess) and self.valid_for_column(index, guess) and self.valid_for_block(index, guess)