-
Notifications
You must be signed in to change notification settings - Fork 0
/
snail.human.cc
1551 lines (1506 loc) · 42.7 KB
/
snail.human.cc
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <optional>
#include <sstream>
#include <map>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <utility>
#include <memory>
#include <queue>
#include <type_traits>
using namespace std;
using namespace rel_ops;
struct Maybe {
int value, group;
bool operator<(const Maybe &b) const {
return make_pair(value, group) < make_pair(b.value, b.group);
}
bool operator==(const Maybe &b) const {
return make_pair(value, group) == make_pair(b.value, b.group);
}
Maybe next() const {
if (value < 3) {
return Maybe{value + 1, group};
}
return Maybe{1, group + 1};
}
Maybe prev() const {
if (value > 1) {
return Maybe{value - 1, group};
}
return Maybe{3, group - 1};
}
};
struct Coord {
int y, x;
};
template<typename T>
struct PositionContainer {
template<typename Type, typename Action>
Type accumulate(Type initial, Action action) const {
return std::accumulate(convert()->begin(), convert()->end(), initial,
[action](Type x, const auto &y) {
return x + action(y);
});
}
private:
auto convert() const {
return static_cast<const T*>(this);
}
};
struct PositionVector : public PositionContainer<PositionVector> {
const vector<int> line;
PositionVector(vector<int> line) : line(move(line)) {
}
auto begin() const {
return line.cbegin();
}
auto end() const {
return line.cend();
}
auto rbegin() const {
return line.crbegin();
}
auto rend() const {
return line.crend();
}
template<typename Action>
PositionVector filter(Action action) {
vector<int> filtered;
copy_if(begin(), end(), back_inserter(filtered), action);
return PositionVector(filtered);
}
};
struct Line : PositionVector {
};
struct Path : PositionVector {
};
struct Sequence : PositionVector {
};
template<typename T>
struct TrimmedLine : public PositionContainer<TrimmedLine<T>> {
using category = typename iterator_traits<T>::iterator_category;
TrimmedLine(T begin, T end) : begin_(begin), end_(end) {
static_assert(is_base_of<bidirectional_iterator_tag, category>::value);
}
auto begin() const {
return begin_;
}
auto end() const {
return end_;
}
auto rbegin() const {
return make_reverse_iterator(end_);
}
auto rend() const {
return make_reverse_iterator(begin_);
}
private:
const T begin_, end_;
};
template<typename T>
struct SpaceTrimmedLine : TrimmedLine<T> {
SpaceTrimmedLine(T begin, T end) : TrimmedLine<T>(begin, end) {
}
};
template<typename T>
struct SpaceValidTrimmedLine : TrimmedLine<T> {
SpaceValidTrimmedLine(T begin, T end) : TrimmedLine<T>(begin, end) {
}
};
struct Geom {
const int n;
const vector<Coord> grid;
const vector<vector<int>> line;
const vector<Line> row, column;
const Path forward, backward;
int modinc(int a) const {
return 1 + ((a - 1) + 1) % 3;
}
optional<int> move(int j, int i, int dir) const {
int nj = j + dy[dir];
int ni = i + dx[dir];
if (ni < 0 || ni >= n || nj < 0 || nj >= n) {
return {};
}
return line[nj][ni];
}
int row_getter(int i, int j) const {
return row[i].line[j];
}
int column_getter(int i, int j) const {
return column[i].line[j];
}
constexpr static int dx[4]{1, 0, -1, 0};
constexpr static int dy[4]{0, 1, 0, -1};
};
struct GeomBuilder {
GeomBuilder(int n): n(n), grid(n * n), line(n, vector<int>(n)) {
}
Geom build() {
int miny = -1, maxy = n;
int minx = -1, maxx = n;
for (int i = 0; i < n * n; i++) {
grid[i] = Coord{cury, curx};
line[cury][curx] = i;
curx += Geom::dx[dir];
cury += Geom::dy[dir];
if (curx >= maxx) {
change();
miny++;
} else if (curx <= minx) {
change();
maxy--;
} else if (cury >= maxy) {
change();
maxx--;
} else if (cury <= miny) {
change();
minx++;
}
}
for (int i = 0; i < n; i++) {
row.push_back(build_row(i));
column.push_back(build_column(i));
}
return Geom{n, grid, line, row, column, build_forward(), build_backward()};
}
private:
void change() {
curx -= Geom::dx[dir];
cury -= Geom::dy[dir];
dir = (dir + 1) % 4;
curx += Geom::dx[dir];
cury += Geom::dy[dir];
}
void print() const {
for (int j = 0; j < n; j++) {
for (int i = 0; i < n; i++) {
cout << line[j][i] << " ";
}
cout << "\n";
}
}
const Path build_forward() const {
vector<int> ans(n * n);
iota(begin(ans), end(ans), 0);
return {ans};
}
const Path build_backward() const {
Path forward = build_forward();
return {vector<int>(rbegin(forward), rend(forward))};
}
Line build_column(int c) const {
vector<int> ans;
ans.reserve(n);
for (int i = 0; i < n; i++) {
ans.push_back(line[i][c]);
}
return {ans};
}
Line build_row(int r) const {
vector<int> ans;
ans.reserve(n);
for (int i = 0; i < n; i++) {
ans.push_back(line[r][i]);
}
return {ans};
}
const int n;
vector<Coord> grid;
vector<vector<int>> line;
vector<Line> row, column;
int cury = 0, curx = 0;
int dir = 0;
};
struct MaybeSet {
set<Maybe> maybe;
bool operator==(const MaybeSet &b) const {
return equal(begin(maybe), end(maybe), begin(b.maybe), end(b.maybe));
}
bool has_value(int v) const {
return end(maybe) != find_if(begin(maybe), end(maybe), [&](auto &m) {
return m.value == v;
});
}
bool has_maybe(const Maybe &m) const {
return maybe.find(m) != end(maybe);
}
bool empty() const {
return maybe.empty();
}
template<typename T>
MaybeSet filter(T action) const {
set<Maybe> ans;
copy_if(begin(maybe), end(maybe), inserter(ans, begin(ans)), action);
return MaybeSet{ans};
}
void fill(int d, int n) {
for (int i = 1; i <= d; i++) {
for (int g = 1; g <= n; g++) {
maybe.insert(Maybe{i, g});
}
}
}
auto size() const {
return maybe.size();
}
template<typename T>
auto count(T action) const {
return count_if(begin(maybe), end(maybe), action);
}
template<typename T>
set<int> extract(T action) const {
set<int> seq;
std::transform(begin(maybe), end(maybe),
inserter(seq, begin(seq)), action);
return seq;
}
template<typename T>
MaybeSet transform(T action) const {
set<Maybe> seq;
std::transform(begin(maybe), end(maybe),
inserter(seq, begin(seq)), action);
return {seq};
}
MaybeSet valid_groups(int max_group) const {
return filter([max_group](const auto &m) {
return m.group >= 1 && m.group <= max_group;
});
}
template<typename T>
void iterate(T action) const {
for (const auto &m : maybe) {
action(m);
}
}
};
struct Cell {
MaybeSet maybe;
optional<int> value;
MaybeSet head, tail;
Cell() = default;
Cell(int n) {
maybe.fill(3, n);
}
bool operator==(const Cell& b) const {
return value == b.value && maybe == b.maybe &&
head == b.head && tail == b.tail;
}
bool empty() const {
return maybe.empty();
}
Cell with_maybe(MaybeSet newmaybe) const {
return Cell{move(newmaybe), value, head, tail};
}
template<typename T>
Cell filter_maybe(T filter) const {
return Cell{maybe.filter(filter), value, head, tail};
}
Cell set_value(int new_value) const {
return Cell{maybe.filter([new_value](const Maybe &m) {
return m.value == new_value;
}), new_value, head, tail};
}
Cell set_head(const MaybeSet &newhead) const {
return Cell{maybe, value, newhead, tail};
}
Cell set_tail(const MaybeSet &newtail) const {
return Cell{maybe, value, head, newtail};
}
bool found() const {
return value && maybe.size() == 1;
}
private:
Cell(MaybeSet maybe, optional<int> value, MaybeSet head, MaybeSet tail)
: maybe(maybe), value(value),
head(value.has_value() ? maybe : head),
tail(value.has_value() ? maybe : tail) {
}
};
struct CellPrinter {
string maybe_values(const Cell &cell) const {
return print_values(cell.maybe.extract([](auto &m) {
return m.value;
}));
}
string maybe_groups(const Cell &cell) const {
return format_sequence(cell.maybe.extract([](auto &m) {
return m.group;
}));
}
string print_value(const Cell& cell) const {
if (cell.value) {
return string(1, '0' + *cell.value);
}
return " ";
}
template<class T>
string print_values(const T &values) const {
ostringstream oss;
for (auto &v : values) {
oss << v << " ";
}
return oss.str();
}
string groups_from_value(const MaybeSet &maybes, int v) const {
vector<int> groups;
for (auto &m: maybes.maybe) {
if (m.value == v) {
groups.push_back(m.group);
}
}
return format_sequence(groups);
}
template<class T>
string format_sequence(const T& seq) const {
ostringstream oss;
int start = -1, current = -1;
for (int i : seq) {
if (start == -1) {
start = i;
current = i;
} else if (i == current + 1) {
current = i;
} else {
dump_sequence(start, current, oss);
start = i;
current = i;
}
}
dump_sequence(start, current, oss);
return oss.str();
}
void dump_sequence(int start, int current, ostringstream &oss) const {
if (current > start) {
oss << start << "-" << current << " ";
} else {
oss << start << " ";
}
}
};
struct State {
State(const Geom &geom)
: n(geom.n), pos_(n * n, Cell(n)), geom(geom) {
}
const Cell &pos(int i) const {
return pos_[i];
}
const Cell &pos(int j, int i) const {
return pos_[geom.line[j][i]];
}
Cell &pos(int i) {
return pos_[i];
}
Cell &pos(int j, int i) {
return pos_[geom.line[j][i]];
}
template<typename T>
int count_valid(const T& line) const {
return count_if(begin(line), end(line), [&](auto i) {
return pos(i).value.has_value();
});
}
template<typename T>
bool has_value(const T &line, int value) const {
return end(line) != find_if(begin(line), end(line), [&](int i) {
return pos(i).value == value;
});
}
template<typename T>
int count_maybe(const T& line, const Maybe &m) const {
return count_if(begin(line), end(line), [&](int i) {
return pos(i).maybe.has_maybe(m);
});
}
template<typename T>
auto first_non_empty(const T& order) const {
return find_if(begin(order), end(order), [&](int i) {
return !pos(i).empty();
});
}
template<typename T>
bool has_single_digit(const T& line, int digit) const {
return 1 == count_if(begin(line), end(line), [&](auto i) {
return pos(i).maybe.has_value(digit);
});
}
template<typename T>
void iter_grid(T action) const {
for (int j = 0; j < n; j++) {
for (int i = 0; i < n; i++) {
action(j, i);
}
}
}
template<typename Filter>
auto trim(const Line& line, Filter filter) const {
auto first = find_if(begin(line), end(line), filter);
auto last = find_if(rbegin(line), rend(line), filter).base();
return TrimmedLine(first, last);
}
auto space_trim(const Line& line) const {
auto notempty = [&](auto i) { return !pos(i).empty(); };
auto trimmed = trim(line, notempty);
return SpaceTrimmedLine(trimmed.begin(), trimmed.end());
}
auto space_valid_trim(const Line& line) const {
auto notempty = [&](auto i) {
return !pos(i).empty() && !pos(i).value.has_value();
};
auto trimmed = trim(line, notempty);
return SpaceValidTrimmedLine(trimmed.begin(), trimmed.end());
}
template<typename T>
optional<Sequence> sequence(const TrimmedLine<T> &line) const {
vector<int> sorted(begin(line), end(line));
sort(begin(sorted), end(sorted));
auto check = adjacent_find(begin(sorted), end(sorted),
[](const auto &a, const auto &b) { return b != a + 1; });
if (check == end(sorted)) {
return Sequence{sorted};
}
return {};
}
bool has_before(int start, const Maybe &m) const {
for (int i = 0; i < start; i++) {
if (pos(i).maybe.has_maybe(m)) {
return true;
}
}
return false;
}
bool done() const {
return 3 * n == count_if(begin(pos_), end(pos_), [](auto &cell) {
return cell.value.has_value();
});
}
template<typename Line>
set<Maybe> head_candidates(const Line& line) const {
set<Maybe> valid;
const auto start = *min_element(begin(line), end(line)) - 1;
for (int i = start; i >= 0; i--) {
if (pos(i).value) {
pos(i).maybe.iterate([&](auto &m) {
valid.insert(m.next());
});
return valid;
}
pos(i).maybe.iterate([&](auto &m) {
valid.insert(m);
valid.insert(m.next());
});
}
valid.insert(Maybe{1, 1});
return valid;
}
const int n;
vector<Cell> pos_;
const Geom &geom;
};
struct StatePrinter {
StatePrinter() : printer() {
}
string border(const Geom &geom, int j, int i) const {
ostringstream oss;
const static string name[4]{"right", "bottom", "left", "top"};
for (int d = 1; d <= 4; d++) {
auto coord = geom.move(j, i, d % 4);
oss << "border-" << name[d % 4] << "-width: ";
oss << (coord && (abs(*coord - geom.line[j][i]) == 1) ? 1 : 3);
oss << "px;";
}
return oss.str();
}
string print(const State &state, const map<int, Cell> &changed) const {
ostringstream oss;
oss << "<div><table>";
for (int j = 0; j < state.geom.n; j++) {
oss << "<tr>";
for (int i = 0; i < state.geom.n; i++) {
oss << "<td style=\"border-style: solid; border-color: black;";
oss << border(state.geom, j, i);
if (changed.find(state.geom.line[j][i]) != end(changed)) {
oss << "background-color: yellow;";
}
oss << "\"><div class=\"outercontent\">";
if (!state.pos(j, i).head.empty()) {
oss << "<div class=\"head\">";
oss << print_maybes(state.pos(j, i).head);
oss << "</div>";
}
oss << "<div class=\"content\">";
if (state.pos(j, i).value) {
oss << "<div class=\"value\">";
oss << printer.print_value(state.pos(j, i)) << "</div>";
oss << "<div class=\"maybe-groups\">";
oss << printer.maybe_groups(state.pos(j, i)) << "</div>";
} else {
oss << print_maybes(state.pos(j, i).maybe);
}
oss << "</div>";
if (!state.pos(j, i).tail.empty()) {
oss << "<div class=\"tail\">";
oss << print_maybes(state.pos(j, i).tail);
oss << "</div>";
}
oss << "</div></td>";
}
oss << "</tr>";
}
oss << "</table></div>\n";
return oss.str();
}
string print_maybes(const MaybeSet &maybes) const {
ostringstream oss;
oss << "<div class=\"maybe-values\">";
for (int d = 1; d <= 3; d++) {
if (maybes.has_value(d)) {
oss << d << " : <span class=\"maybe-groups\">";
oss << printer.groups_from_value(maybes, d);
oss << "</span><br>";
}
}
oss << "</div>";
return oss.str();
}
const CellPrinter printer;
};
struct Strategy {
virtual string name() = 0;
virtual map<int, Cell> strategy(const State &state) = 0;
virtual ~Strategy() = default;
bool skip = false;
};
struct Filter {
const State &state;
map<int, Cell> ans;
void put(int j, int i, Cell &&cell) {
if (state.pos(j, i) != cell) {
ans.insert(make_pair(state.geom.line[j][i], cell));
}
}
void put(int i, Cell &&cell) {
if (state.pos(i) != cell) {
ans.insert(make_pair(i, cell));
}
}
bool empty() {
return ans.empty();
}
map<int, Cell> flush() {
return ans;
}
void print() {
for (auto &x : ans) {
cout << x.first << " ";
}
cout << "<br>";
}
};
struct AddGivens final : public Strategy {
const vector<string> &grid;
AddGivens(const vector<string> &grid) : grid(grid) {
}
string name() override {
return "Add givens";
}
map<int, Cell> strategy(const State &state) override {
Filter filter{state};
state.iter_grid([&](int j, int i) {
if (grid[j][i] != '.') {
int value = grid[j][i] - '0';
filter.put(j, i, state.pos(j, i).set_value(value));
}
});
skip = true;
return filter.flush();
}
};
struct RemoveCross final : public Strategy {
int bj, bi;
RemoveCross(int bj, int bi) : bj(bj), bi(bi) {
}
string name() override {
ostringstream oss;
oss << "Remove from row " << bj << " and column "<< bi;
return oss.str();
}
map<int, Cell> strategy(const State &state) override {
Filter filter{state};
if (!state.pos(bj, bi).value) {
return {};
}
state.iter_grid([&](int j, int i) {
if ((j != bj || i != bi) && (j == bj || i == bi)) {
filter.put(j, i, state.pos(j, i).filter_maybe([&](const Maybe &m) {
return m.value != *state.pos(bj, bi).value;
}));
}
});
if (!filter.empty()) {
skip = true;
}
return filter.flush();
}
};
struct DuplicateGroup final : public Strategy {
int digit, group;
DuplicateGroup(int d, int g) : digit(d), group(g) {
}
string name() override {
ostringstream oss;
oss << "Digit " << digit << " from group ";
oss << group << " was already found";
return oss.str();
}
map<int, Cell> strategy(const State &state) override {
Filter filter{state};
Maybe goal{digit, group};
const Path &order = state.geom.forward;
auto it = find_if(begin(order), end(order), [&](int i) {
return state.pos(i).found() && state.pos(i).maybe.has_maybe(goal);
});
if (it == end(order)) {
return {};
}
int ri = distance(begin(order), it);
for (int i = 0; i < state.n * state.n; i++) {
if (i != ri) {
filter.put(i, state.pos(i).filter_maybe([&](auto &m) {
return goal != m;
}));
}
}
skip = true;
return filter.flush();
}
};
struct LimitSequence final : public Strategy {
const Line &line;
const string line_name;
LimitSequence(const Line &line, const string &line_name_, int pos)
: line(line), line_name(build_name(line_name_, pos)) {
}
string name() override {
return line_name;
}
string build_name(const string &line_name, int line_pos) const {
ostringstream oss;
oss << "Can't have more than 3 digits in " << line_name << " " << line_pos;
return oss.str();
}
map<int, Cell> strategy(const State &state) override {
auto seq = state.sequence(state.space_trim(line));
if (!seq.has_value()) {
return {};
}
Filter filter{state};
set<Maybe> maybes;
int start = state.n * state.n;
for (int i : *seq) {
state.pos(i).maybe.iterate([&](auto &m) {
maybes.insert(m);
});
if (!state.pos(i).empty() && i < start) {
start = i;
}
}
set<Maybe> firsts;
for (auto &m : maybes) {
if (!state.has_before(start, m)) {
firsts.insert(m);
}
}
set<Maybe> avoid;
for (auto &f : firsts) {
for (auto &m : maybes) {
if (f.value == m.value && f.group < m.group) {
avoid.insert(m);
}
}
}
for (int i : *seq) {
filter.put(i, state.pos(i).filter_maybe([&](auto &m) {
return find(begin(avoid), end(avoid), m) == end(avoid);
}));
}
return filter.flush();
}
};
struct BoundedSequence final : public Strategy {
int start, end, middle;
const Line &line;
const string line_name;
BoundedSequence(int start, int end, int middle,
const Line &line, const string &line_name, int pos)
: start(start), end(end), middle(middle),
line(line), line_name(build_name(line_name, start, end, middle, pos)) {
}
string name() override {
return line_name;
}
string build_name(const string &line_name,
int start, int end, int middle, int line_pos) const {
ostringstream oss;
oss << "Digit " << middle << " between " << start << " and ";
oss << end << " only appear on " << line_name << " " << line_pos;
return oss.str();
}
map<int, Cell> strategy(const State &state) override {
Filter filter{state};
int current = -1;
for (int i = 0; i < state.n * state.n; i++) {
if (state.pos(i).value) {
if (current == -1) {
current = i;
continue;
}
if (state.pos(current).value == start && state.pos(i).value == end) {
if (done.find(make_pair(current, i)) != std::end(done)) {
current = i;
continue;
}
if (bounded(current + 1, i - 1, state)) {
for (int j : line) {
if (state.pos(j).maybe.has_value(middle) &&
(j < current || j > i)) {
filter.put(j, state.pos(j).filter_maybe([&](auto &m) {
return m.value != middle;
}));
}
}
done.insert(make_pair(current, i));
break;
}
}
current = i;
}
}
return filter.flush();
}
bool bounded(int a, int b, const State &state) {
bool present = false;
for (int i = a; i <= b; i++) {
if (state.pos(i).maybe.has_value(middle)) {
if (find(begin(line), std::end(line), i) == std::end(line)) {
return false;
}
present = true;
}
}
return present;
}
set<pair<int, int>> done;
};
struct OnlyValue final : public Strategy {
int digit, group;
const Line &line;
const string line_name;
int line_pos;
OnlyValue(int d, int g, const Line &line,
const string &line_name, int pos)
: digit(d), group(g), line(line),
line_name(line_name), line_pos(pos) {
}
string name() override {
ostringstream oss;
oss << "Digit " << digit << " in group " << group;
oss << " only appears on " << line_name << " " << line_pos;
return oss.str();
}
map<int, Cell> strategy(const State &state) override {
Filter filter{state};
if (state.has_value(line, digit)) {
skip = true;
return {};
}
Maybe m{digit, group};
int line_count = state.count_maybe(line, m);
int grid_count = state.count_maybe(state.geom.forward, m);
if (line_count != grid_count) {
return {};
}
for (int i : line) {
filter.put(i, state.pos(i).filter_maybe([&](const Maybe &m) {
return m.value != digit || m.group == group;
}));
}
skip = true;
return filter.flush();
}
};
struct OnlyGroup final : public Strategy {
int digit, group;
const Line &line;
const string line_name;
int line_pos;
OnlyGroup(int d, int g, const Line &line,
const string &line_name, int pos)
: digit(d), group(g), line(line),
line_name(line_name), line_pos(pos) {
}
string name() override {
ostringstream oss;
oss << line_name << " " << line_pos << " only has ";
oss << digit << " in group " << group;
return oss.str();
}
map<int, Cell> strategy(const State &state) override {
Filter filter{state};
if (state.has_value(line, digit)) {
skip = true;
return {};
}
Maybe m{digit, group};
int line_count = state.count_maybe(line, m);
int count = line.accumulate(0, [&](const auto &i) {
return state.pos(i).maybe.count([&](const auto& m) {
return m.value == digit;
});
});
if (line_count != count) {
return {};
}
set<int> skipline(begin(line), end(line));
for (int i : state.geom.forward) {
if (skipline.find(i) == end(skipline)) {
filter.put(i, state.pos(i).filter_maybe([&](const Maybe &m) {
return !(m.value == digit && m.group == group);
}));
}
}
skip = true;
return filter.flush();
}
};
struct ExactlyNSearch {
const State &state;
const PositionVector &valid;
const int nfound;
const set<Maybe> &first;
const int valid_inside;
map<int, set<Maybe>> allow;
vector<pair<int, Maybe>> candidate;
ExactlyNSearch(const State& state, const PositionVector &valid,
const int nfound, const set<Maybe> &first)
: state(state), valid(valid), nfound(nfound), first(first),
valid_inside(state.count_valid(valid)) {
for (auto i : valid) {
allow[i] = set<Maybe>{};
}
}
map<int, Cell> search() {
Filter filter{state};
for (auto i = begin(valid); i != end(valid); ++i) {
state.pos(*i).maybe.iterate([&](auto &m) {
if (first.find(m) != end(first)) {
search(i, end(valid), m, 3 - (nfound - valid_inside) - 1);
}
});
}
for (auto kv : allow) {
filter.put(kv.first,
state.pos(kv.first).with_maybe({kv.second}));
}
return filter.flush();
}
template<typename Iter>
void search(Iter start, Iter finish, Maybe m, int level) {
auto m2 = m.next();
candidate.push_back(make_pair(*start, m));
if (level > 0) {
for (auto i2 = next(start); i2 != finish; ++i2) {
if (state.pos(*i2).maybe.has_maybe(m2)) {
search(i2, finish, m2, level - 1);
}
}
} else {
if (has_valid_inside()) {
for (const auto &kv : candidate) {
allow[kv.first].insert(kv.second);
}
}
}
candidate.pop_back();
}
bool has_valid_inside() {
return valid_inside == count_if(begin(candidate), end(candidate),
[&](const auto &kv) {
return state.pos(kv.first).value.has_value();
});
}
};
struct ExactlyNValues final : public Strategy {
int n;
const Line &line;
const string line_name;
ExactlyNValues(int n_, const Line &line_, const string &line_name_, int pos_)
: n(n_), line(line_), line_name(build_name(line_name_, n_, pos_)) {
}
string name() override {
return line_name;
}
const string build_name(const string &line_name, int n, int line_pos) const {
ostringstream oss;
oss << line_name << " " << line_pos << " must have exactly ";
oss << n << " values";
return oss.str();
}
map<int, Cell> strategy(const State &state) override {
int nfound = state.count_valid(line);
if (nfound >= 3) {
skip = true;
return {};
}
auto seq = state.sequence(state.space_valid_trim(line));
if (!seq.has_value()) {
return {};
}
PositionVector valid = seq->filter(
[&](int i){ return !state.pos(i).empty(); });
set<Maybe> first = state.head_candidates(valid);
ExactlyNSearch search(state, valid, nfound, first);
return search.search();
}
};