-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstraint.py
62 lines (45 loc) · 1.49 KB
/
constraint.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
from abc import abstractmethod
__all__ = [
"Constraint", "CellConstraint", "RowConstraint",
"ColumnConstraint", "BoxConstraint"
]
class Constraint:
"""
Represents a constraint imposed on a set of columns in the
Binary Matrix. In sudoku we have 4 types of constraints which
have been defined below.
"""
@property
@abstractmethod
def _offset(self) -> int:
"""
Offset to the constraint. Sudoku can be broken into
4 constraints. Each of these constraints will need 81
columns. 81x4 = 324 columns in total. Offset will therefore
be a multiple of 81.
"""
...
@staticmethod
@abstractmethod
def column_num(x: int, y: int, sub_row: int) -> int:
...
class CellConstraint(Constraint):
_offset = 0
@staticmethod
def column_num(x: int, y: int, sub_row: int) -> int:
return CellConstraint._offset + (x * 9) + y
class RowConstraint(Constraint):
_offset = 81
@staticmethod
def column_num(x: int, y: int, sub_row: int) -> int:
return RowConstraint._offset + (x * 9) + sub_row
class ColumnConstraint(Constraint):
_offset = 162
@staticmethod
def column_num(x: int, y: int, sub_row: int) -> int:
return ColumnConstraint._offset + (y * 9) + sub_row
class BoxConstraint(Constraint):
_offset = 243
@staticmethod
def column_num(x: int, y: int, sub_row: int) -> int:
return BoxConstraint._offset + (3 * (x // 3) + y // 3) * 9 + sub_row