Repository navigation
Expand file tree
/
Copy pathclasses.py
More file actions
117 lines (88 loc) · 3.62 KB
/
Copy pathclasses.py
File metadata and controls
117 lines (88 loc) · 3.62 KB
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
112
113
114
class Given:
def __init__(self, board):
self.board = board
self.rows, self.cols = len(board), len(board[0])
self.boxSize = int(len(board)**0.5)
def getNumber(self, row, col):
return self.board[row][col]
class Input:
manualCandidates = dict()
mistakes = 0
for row in range(9): #hardcode??
for col in range(9):
manualCandidates[(row, col)] = set()
def __init__(self, board):
self.board = board
self.rows, self.cols = len(board), len(board[0])
self.boxSize = int(len(board)**0.5)
def __repr__(self):
return str(self.board)
def __eq__(self, other):
if not isinstance(other, Input): return False
for row in range(self.rows):
if self.getNumInRow(row) != other.getNumInRow(row):
return False
return True
def getNumber(self, row, col):
return self.board[row][col]
def putNumberIn(self, row, col, num):
self.board[row][col] = num
#get cell regions
def getRow(self, row):
return [(row, col) for col in range(self.cols)]
def getCol(self, col):
return [(row, col) for row in range(self.rows)]
def getBox(self, row, col):
cells = []
startRow = int(row // self.boxSize * self.boxSize)
startCol = int(col // self.boxSize * self.boxSize)
for r in range(startRow, startRow + self.boxSize):
for c in range(startCol, startCol + self.boxSize):
cells.append((r, c))
return cells
def getCellRegion(self, row, col):
return Input.getRow(self, row) + Input.getCol(self, col) + Input.getBox(self, row, col)
#get numbers in each region
def getNumInBox(self, row, col):
numInBox = []
for r, c in Input.getBox(self, row, col):
numInCell = Input.getNumber(self, r, c)
numInBox.append(numInCell)
return numInBox
def getNumInRow(self, row):
numInRow = []
for r, c in Input.getRow(self, row):
numInCell = Input.getNumber(self, r, c)
numInRow.append(numInCell)
return numInRow
def getNumInCol(self, col):
numInCol = []
for r, c in Input.getCol(self, col):
numInCell = Input.getNumber(self, r, c)
numInCol.append(numInCell)
return numInCol
def getNumInCellRegion(self, row, col):
return (set(Input.getNumInBox(self, row, col)) | set(Input.getNumInCol(self, col))
| set(Input.getNumInRow(self, row)))
#find/manage legals in each cell
def getLegalValue(self):
legalValue = dict()
for row in range(self.rows):
for col in range(self.cols):
if Input.getNumber(self, row, col) == 0 or Input.getNumber(self, row, col) == None:
candidates = set(i for i in range(1, 10)) - Input.getNumInCellRegion(self, row, col)
else: candidates = set()
legalValue[(row, col)] = candidates
return legalValue
def takeOffLegals(self, num):
legalValue = Input.getLegalValue(self)
def autoPossibleCandidates(self, row, col):
LegalValue = Input.getLegalValue(self)
return LegalValue[(row, col)]
#manualPossibleCandidates
def manualPossibleCandidates(self, row, col):
return Input.manualCandidates[(row, col)]
def addManualPossibleCandidates(self, row, col, num):
Input.manualCandidates[(row, col)].add(num)
def clearManualPossibleCandidates(self, row, col):
Input.manualCandidates[(row, col)] = set()