-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxline.cpp
258 lines (227 loc) · 6.56 KB
/
boxline.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "boxline.h"
#include "keyrank.h"
const int BOXLINE_ROW = 1;
const int BOXLINE_COL = 2;
BoxLine::BoxLine(std::vector<Target> _targets, std::vector<Key> _keys, std::vector<Spot> _subset, std::vector<int> _vals, int _type) {
targets = _targets;
keys = _keys;
subset = _subset;
vals = _vals;
type = _type;
}
void BoxLine::apply(BitMatrix* bits) {
for(int i=0; i < targets.size(); i++) {
targets[i].apply(bits);
}
}
void BoxLine::full_display(Output* out) {
out->highlight_box(boxid_of_spot(subset[0]), 0.9, 0.9, 0.9);
if(type == BOXLINE_ROW) {
out->highlight_row(subset[0].row, 0.9, 0.9, 0.9);
} else if(type == BOXLINE_COL) {
out->highlight_col(subset[0].col, 0.9, 0.9, 0.9);
}
for(int i=0; i < targets.size(); i++) {
Target target = targets[i];
for(int j=0; j < target.candidates.size(); j++) {
out->highlight_candidate(target.spot, target.candidates[j], 1.0, 0.5, 1.0);
}
}
for(int i=0; i < subset.size(); i++) {
out->highlight_cell(subset[i], 0.8, 0.8, 0.8);
}
}
void BoxLine::display_keys(Output* out) {
for(int i=0; i < keys.size(); i++) {
float weight = (keys[i].multiplicity == 1 ? 0.0 : (keys[i].multiplicity == 2 ? 0.5 : 0.8));
out->outline_cell(keys[i].spot, 1.5, 1.0, weight, weight);
}
}
std::vector<Target> BoxLine::target_list() {
return targets;
}
std::vector<Key> BoxLine::key_list() {
return keys;
}
Pattern* BoxLine::clone() {
return (new BoxLine(*this));
}
void BoxLine::describe(std::ostream& out) {
out << this;
}
std::ostream& operator<<(std::ostream& out, BoxLine* bl) {
out << "Box Line Interaction (" << (bl->type == BOXLINE_ROW ? "row" : "col") << ")" << std::endl;
out << "\tIntersection:";
for(int i=0; i < bl->subset.size(); i++) {
out << std::endl << "\t\t" << bl->subset[i];
}
out << std::endl << "\tTargets:";
for(int i=0; i < bl->targets.size(); i++) {
out << std::endl << "\t\t" << bl->targets[i];
}
return out;
}
std::vector<BoxLine> find_boxlines(Bits* bits) {
std::vector<BoxLine> ret;
for(int i = 0; i < 9; i++) {
// for box i
std::vector<Spot> boxspots = spots_of_box(i);
int boxrow = boxspots[0].row;
int boxcol = boxspots[0].col;
// check rows
for(int j=0; j < 3; j++) {
// row is absolute row
int row = boxrow + j;
std::vector<Spot> others;
std::vector<Spot> spots;
std::vector<Spot> empty_spots;
std::vector<Spot> complements;
// classify all necessary spots
for(int k=0; k < 9; k++) {
Spot boxspot = spot_of_boxid(i, k);
Spot rowspot = Spot(row, k);
if(boxid_of_spot(rowspot) != i) {
others.push_back(rowspot);
}
if(boxspot.row == row) {
spots.push_back(boxspot);
if(!bits->has_value(boxspot)) {
empty_spots.push_back(boxspot);
}
} else {
if(!bits->has_value(boxspot)) {
complements.push_back(boxspot);
}
}
}
int emptybits = bits->bit_of_spots(empty_spots);
int otherbits = bits->bit_of_spots(others);
int compbits = bits->bit_of_spots(complements);
int boxedbits = bit_subtract_int(emptybits, otherbits);
if(boxedbits == 0) { continue; }
// we have aligned
int targetbits = boxedbits & compbits;
if(targetbits == 0) { continue; }
// and will remove candidates
std::vector<int> vals = vals_of_int(targetbits);
// identify targets
std::vector<Target> targets;
for(int k=0; k < complements.size(); k++) {
Spot comp = complements[k];
std::vector<int> candidates;
for(int m=0; m < vals.size(); m++) {
int mbit = vals[m];
if(bits->has_candidate(comp, mbit)) {
candidates.push_back(mbit);
}
}
if(candidates.size() > 0) {
targets.push_back(Target(comp, candidates, 0));
}
}
// identify subset
std::vector<Spot> subset;
for(int k=0; k < empty_spots.size(); k++) {
bool part = false;
for(int m=0; m < vals.size(); m++) {
int mbit = vals[m];
if(bits->has_candidate(empty_spots[k], mbit)) {
part = true;
}
}
if(part) {
subset.push_back(empty_spots[k]);
}
}
// identify keys
// OLD NOTE: mostly copied from pointing.cpp
KeyRank keyrank;
for(int k=0; k < others.size(); k++) {
Spot spot = others[k];
for(int m=0; m < vals.size(); m++) {
int mbit = vals[m];
keyrank.add_possible_covers(bits, spot, mbit);
}
}
ret.push_back(BoxLine(targets, keyrank.keys, subset, vals, BOXLINE_ROW));
}
// check cols
for(int j=0; j < 3; j++) {
// col is absolute col
int col = boxcol + j;
std::vector<Spot> others;
std::vector<Spot> spots;
std::vector<Spot> empty_spots;
std::vector<Spot> complements;
// classify all necessary spots
for(int k=0; k < 9; k++) {
Spot boxspot = spot_of_boxid(i, k);
Spot rowspot = Spot(k, col);
if(boxid_of_spot(rowspot) != i) {
others.push_back(rowspot);
}
if(boxspot.col == col) {
spots.push_back(boxspot);
if(!bits->has_value(boxspot)) {
empty_spots.push_back(boxspot);
}
} else {
if(!bits->has_value(boxspot)) {
complements.push_back(boxspot);
}
}
}
int emptybits = bits->bit_of_spots(empty_spots);
int otherbits = bits->bit_of_spots(others);
int compbits = bits->bit_of_spots(complements);
int boxedbits = bit_subtract_int(emptybits, otherbits);
if(boxedbits == 0) { continue; }
// we have aligned
int targetbits = boxedbits & compbits;
if(targetbits == 0) { continue; }
// and will remove candidates
std::vector<int> vals = vals_of_int(targetbits);
// identify targets
std::vector<Target> targets;
for(int k=0; k < complements.size(); k++) {
Spot comp = complements[k];
std::vector<int> candidates;
for(int m=0; m < vals.size(); m++) {
int mbit = vals[m];
if(bits->has_candidate(comp, mbit)) {
candidates.push_back(mbit);
}
}
if(candidates.size() > 0) {
targets.push_back(Target(comp, candidates, 0));
}
}
// identify subset
std::vector<Spot> subset;
for(int k=0; k < empty_spots.size(); k++) {
bool part = false;
for(int m=0; m < vals.size(); m++) {
int mbit = vals[m];
if(bits->has_candidate(empty_spots[k], mbit)) {
part = true;
}
}
if(part) {
subset.push_back(empty_spots[k]);
}
}
// identify keys
// OLD NOTE: mostly copied from pointing.cpp
KeyRank keyrank;
for(int k=0; k < others.size(); k++) {
Spot spot = others[k];
for(int m=0; m < vals.size(); m++) {
int mbit = vals[m];
keyrank.add_possible_covers(bits, spot, mbit);
}
}
ret.push_back(BoxLine(targets, keyrank.keys, subset, vals, BOXLINE_COL));
}
}
return ret;
}