-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature.py
223 lines (182 loc) · 7.86 KB
/
feature.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from __future__ import annotations
import abc
import atexit
from collections import defaultdict, deque
from collections.abc import Callable, Iterable, Sequence
from itertools import product, zip_longest
from typing import ClassVar, Mapping, Optional, Union, cast
from cell import Cell, CellValue, House
from draw_context import DrawContext
from grid import Grid
Square = tuple[int, int]
SquaresParseable = Union[str, int, Sequence[Square]]
OneSquareParseable = Union[str, int, Square]
CheckFunction = Callable[['Feature'], bool]
class Feature(abc.ABC):
name: str
grid: Grid
__prefix_count: ClassVar[dict[str, int]] = defaultdict(int)
def __init__(self, *, name: Optional[str] = None, prefix: Optional[str] = None) -> None:
if not name:
prefix = prefix or self.__class__.__name__.removesuffix("Feature").removeprefix("_")
self.__prefix_count[prefix] += 1
name = f'{prefix} #{self.__prefix_count[prefix]}'
self.name = name
def initialize(self, grid: Grid) -> None:
self.grid = grid
def start(self) -> None:
pass
def get_neighbors(self, cell: Cell) -> Iterable[Cell]:
return ()
def get_neighbors_for_value(self, cell: Cell, value: int) -> Iterable[Cell]:
return ()
def check(self) -> bool:
return False
def check_special(self) -> bool:
return False
# noinspection PyMethodMayBeStatic
def get_strong_pairs(self, _cell_value: CellValue) -> Iterable[CellValue]:
"""
cell1=value1 and cell2=value2 are a strong pair if at least one of them is always True.
For now, we only generate results in which exactly one of them is always True.
"""
return ()
def get_weak_pairs(self, _cell_value: CellValue) -> Iterable[CellValue]:
"""
cell1=value1 and cell2=value2 are a weak pair if both of them cannot be simultaneously True.
If either of them is true, then the other must be False.
"""
return ()
# noinspection PyMethodMayBeStatic
def get_xor_pairs(self, cell_value: CellValue) -> Iterable[CellValue]:
"""
Exactly one of cell1=value and cell2=value is true. These are both strong and weak.
"""
# In the lack of better information, we can just intersection strong_pairs and weak_pairs. Most features
# don't even bother having strong pairs, so find that first.
strong_pairs = set(self.get_strong_pairs(cell_value))
if strong_pairs:
yield from (w for w in self.get_weak_pairs(cell_value) if w in strong_pairs)
def verify(self, grid: Mapping[Square, int]) -> None:
pass
def draw(self, context: DrawContext) -> None:
pass
def __str__(self) -> str:
return self.name
def __repr__(self) -> str:
return self.name
def __matmul__(self, square: Square) -> Cell:
return self.grid.matrix[square]
def neighbors_from_offsets(self, cell: Cell, offsets: Iterable[Square]) -> Iterable[Cell]:
row, column = cell.square
for dr, dc in offsets:
if 1 <= row + dr <= 9 and 1 <= column + dc <= 9:
yield self.grid.matrix[row + dr, column + dc]
__DESCRIPTORS = dict(N=(-1, 0), S=(1, 0), E=(0, 1), W=(0, -1), NE=(-1, 1), NW=(-1, -1), SE=(1, 1), SW=(1, -1))
@staticmethod
def parse_squares(descriptor: SquaresParseable) -> Sequence[Square]:
if isinstance(descriptor, int):
descriptor = str(descriptor)
if not isinstance(descriptor, str):
return descriptor
descriptors = Feature.__DESCRIPTORS
pieces = deque(piece.strip() for piece in descriptor.split(','))
squares: list[Square] = []
while pieces:
if pieces[0][0] in "123456789":
value = int(pieces.popleft())
if value <= 9:
row, column = value, int(pieces.popleft())
else:
row, column = divmod(value, 10)
assert 1 <= row <= 9 and 1 <= column <= 9
squares.append((row, column))
else:
piece = pieces.popleft().upper()
if piece == 'R':
squares = [(col, 10 - row) for row, col in squares]
elif piece == 'T':
squares = [(col, row) for row, col in squares]
else:
dr, dc = descriptors[piece]
row, column = squares[-1]
assert 1 <= row + dr <= 9 and 1 <= column + dc <= 9
squares.append((row + dr, column + dc))
return squares
@staticmethod
def parse_square(descriptor: OneSquareParseable) -> Square:
if isinstance(descriptor, str) or isinstance(descriptor, int):
temp = Feature.parse_squares(descriptor)
assert len(temp) == 1
return temp[0]
else:
return descriptor
@staticmethod
def parse_direction(descriptor: Square | str) -> Square:
if isinstance(descriptor, str):
return Feature.__DESCRIPTORS[descriptor.upper()]
else:
return descriptor
@staticmethod
def between(square1: Square, square2: Square) -> list[Square]:
(r1, c1), (r2, c2) = square1, square2
dr, dc = r2 - r1, c2 - c1
distance = max(abs(dr), abs(dc))
dr, dc = dr // distance, dc // distance
squares = [square1]
while squares[-1] != square2:
r1, c1 = r1 + dr, c1 + dc
squares.append((r1, c1))
return squares
@staticmethod
def get_house_squares(htype: House.Type, index: int) -> Sequence[Square]:
if htype == House.Type.ROW:
return [(index, i) for i in range(1, 10)]
if htype == House.Type.COLUMN:
return [(i, index) for i in range(1, 10)]
if htype == House.Type.BOX:
q, r = divmod(index - 1, 3)
start_row = 3 * q + 1
start_column = 3 * r + 1
return [(row, column)
for row in range(start_row, start_row + 3)
for column in range(start_column, start_column + 3)]
assert False, f'Bad argument {htype}'
@staticmethod
def box_for_square(square: Square) -> int:
row, column = square
return 3 * ((row - 1) // 3) + ((column - 1) // 3) + 1
@staticmethod
def all_squares() -> Iterable[Square]:
return cast(Iterable[Square], product(range(1, 10), repeat=2))
@classmethod
def has_neighbor_method(cls) -> bool:
return cls.get_neighbors != Feature.get_neighbors or \
cls.get_neighbors_for_value != Feature.get_neighbors_for_value
@classmethod
def has_check_method(cls) -> bool:
return cls.check != Feature.check or cls.check_special != Feature.check_special
@classmethod
def has_any_pair_method(cls) -> bool:
return cls.get_weak_pairs != Feature.get_weak_pairs or cls.get_strong_pairs != Feature.get_strong_pairs or \
cls.get_xor_pairs != Feature.get_xor_pairs
check_elided: ClassVar[int] = 0
check_called: ClassVar[int] = 0
@staticmethod
def cells_changed_since_last_invocation(location: list[int], cells: Sequence[Cell]) -> bool:
generator = (-1 if cell.is_known else cell.bitmap for cell in cells)
if all(x == y for x, y in zip_longest(location, generator)):
Feature.check_elided += 1
return False
else:
Feature.check_called += 1
location[:] = (-1 if cell.is_known else cell.bitmap for cell in cells)
return True
@atexit.register
def print_counters() -> None:
total = Feature.check_called + Feature.check_elided
if total > 0:
elision = 100.0 * Feature.check_elided / total
print(f'Method feature.check() called {total} times; {Feature.check_elided} ({elision:.2f}%) were elided')
if __name__ == '__main__':
pass