-
Notifications
You must be signed in to change notification settings - Fork 0
/
solverFuncs.py
74 lines (67 loc) · 1.74 KB
/
solverFuncs.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
def get_cages():
numcages = int(input("Number of cages: "))
cages = []
x = 0
for x in range(numcages):
info = input("Cage number %d: " %x).split()
new = []
for i in info:
new.append(int(i))
cages.append(new)
return cages
def check_row(row):
seen = []
for num in row:
if num in seen and num != 0 or num > 5:
return False
else:
seen.append(num)
return True
def check_rows_valid(puzzle):
for row in puzzle:
if check_row(row) == False:
return False
return True
def check_col(puzzle, index):
seen = []
for row in puzzle:
if row[index] in seen and row[index] != 0 or row[index] > 5:
return False
else:
seen.append(row[index])
return True
def check_columns_valid(puzzle):
index = 0
for col in puzzle:
if check_col(puzzle, index) == False:
return False
index += 1
return True
def check_cage(puzzle, cage):
cagesum = 0
complete = True
for index in cage[2:]:
row = index // 5
col = index % 5
if puzzle[row][col] == 0:
complete = False
cagesum += puzzle[row][col]
if cagesum != cage[0] and complete:
return False
if cagesum >= cage[0] and not complete:
return False
return True
def check_cages_valid(puzzle, cages):
for cage in cages:
if check_cage(puzzle, cage) == False:
return False
return True
def check_valid(puzzle, cages):
if check_rows_valid(puzzle) == False:
return False
elif check_columns_valid(puzzle) == False:
return False
elif check_cages_valid(puzzle, cages) == False:
return False
else:
return True