-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrexplus.cpp
More file actions
1903 lines (1798 loc) · 65.4 KB
/
Copy pathrexplus.cpp
File metadata and controls
1903 lines (1798 loc) · 65.4 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
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 <algorithm>
#include <array>
#include <bit>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <initializer_list>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <string>
#include <string_view>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <vector>
namespace {
enum class Result : std::uint8_t { Loss = 0, Win = 1, Unknown = 2 };
const char* result_name(Result result)
{
if (result == Result::Win)
return "win";
if (result == Result::Loss)
return "loss";
return "unknown";
}
struct Outcome
{
Result black;
Result white;
};
struct WinningSet
{
Result result;
std::vector<int> cells;
};
std::uint64_t mix64(std::uint64_t value)
{
value ^= value >> 30;
value *= 0xbf58476d1ce4e5b9ULL;
value ^= value >> 27;
value *= 0x94d049bb133111ebULL;
return value ^ (value >> 31);
}
struct Key
{
std::uint64_t black;
std::uint64_t white;
bool operator==(const Key& other) const
{
return black == other.black && white == other.white;
}
};
std::uint64_t key_hash(const Key& key)
{
return mix64(key.black ^ std::rotl(key.white, 23));
}
class TranspositionTable
{
public:
TranspositionTable(int cells, std::size_t megabytes)
: compact(cells <= 36)
{
constexpr std::size_t Scale = 1024ULL * 1024ULL;
if (megabytes > std::numeric_limits<std::size_t>::max() / Scale)
throw std::invalid_argument("transposition table is too large");
std::size_t entry_size = compact ? sizeof(std::uint64_t)
: sizeof(WideEntry);
const std::size_t requested = megabytes * Scale / entry_size;
std::size_t count = 1;
while (count < requested && count < (std::size_t(1) << 34))
count <<= 1;
if (count > requested && count > 1)
count >>= 1;
count = std::max<std::size_t>(count, 1024);
if (compact)
compact_entries.resize(count);
else
wide_entries.resize(count);
mask = count - 1;
}
bool find(const Key& key, std::uint64_t code,
int color, Result& result, Result* other_result = nullptr,
bool* other_witness = nullptr) const
{
if (other_result)
*other_result = Result::Unknown;
if (other_witness)
*other_witness = false;
if (!compact)
return find_wide(key, color, result, other_result,
other_witness);
const std::size_t home = mix64(code) & mask;
const std::uint64_t wanted = code << MetaBits;
for (std::size_t offset = 0; offset < ProbeCount; ++offset)
{
const std::uint64_t entry =
compact_entries[(home + offset) & mask];
if ((entry & ~MetaMask) == wanted)
{
const std::uint8_t known = entry & KnownMask;
const std::uint8_t wins = (entry >> 2) & KnownMask;
if (other_result || other_witness)
{
const std::uint8_t other_bit =
std::uint8_t(1) << (color ^ 1);
if (other_result && (known & other_bit))
*other_result = wins & other_bit
? Result::Win : Result::Loss;
if (other_witness)
*other_witness = entry
& (std::uint64_t(other_bit) << 4);
}
const std::uint8_t bit = std::uint8_t(1) << color;
if (!(known & bit))
return false;
result = wins & bit ? Result::Win : Result::Loss;
return true;
}
}
return false;
}
void store(const Key& key, std::uint64_t code, int color, Result result,
bool witness = false)
{
if (!compact)
{
store_wide(key, color, result, witness);
return;
}
const std::uint64_t wanted = code << MetaBits;
const std::size_t home = mix64(code) & mask;
std::size_t target = home;
for (std::size_t offset = 0; offset < ProbeCount; ++offset)
{
const std::size_t index = (home + offset) & mask;
const std::uint64_t entry = compact_entries[index];
if (!entry || (entry & ~MetaMask) == wanted)
{
target = index;
break;
}
}
std::uint64_t entry = compact_entries[target];
if ((entry & ~MetaMask) != wanted)
entry = wanted;
const std::uint64_t known_bit = std::uint64_t(1) << color;
const std::uint64_t win_bit = std::uint64_t(1) << (color + 2);
entry |= known_bit;
if (result == Result::Win)
entry |= win_bit;
else
{
entry &= ~win_bit;
entry &= ~(std::uint64_t(1) << (color + 4));
}
if (witness)
entry |= std::uint64_t(1) << (color + 4);
compact_entries[target] = entry;
}
std::size_t bytes() const
{
return compact ? compact_entries.size() * sizeof(std::uint64_t)
: wide_entries.size() * sizeof(WideEntry);
}
private:
static constexpr std::size_t ProbeCount = 8;
static constexpr int MetaBits = 6;
static constexpr std::uint64_t MetaMask = 0x3f;
static constexpr std::uint8_t KnownMask = 0x3;
struct WideEntry
{
Key key{};
bool valid = false;
std::uint8_t known = 0;
std::uint8_t wins = 0;
std::uint8_t witnesses = 0;
};
bool find_wide(const Key& key, int color, Result& result,
Result* other_result, bool* other_witness) const
{
const std::size_t home = key_hash(key) & mask;
for (std::size_t offset = 0; offset < ProbeCount; ++offset)
{
const WideEntry& entry = wide_entries[(home + offset) & mask];
if (entry.valid && entry.key == key)
{
if (other_result || other_witness)
{
const std::uint8_t other_bit =
std::uint8_t(1) << (color ^ 1);
if (other_result && (entry.known & other_bit))
*other_result = entry.wins & other_bit
? Result::Win : Result::Loss;
if (other_witness)
*other_witness = entry.witnesses & other_bit;
}
const std::uint8_t bit = std::uint8_t(1) << color;
if (!(entry.known & bit))
return false;
result = entry.wins & bit ? Result::Win : Result::Loss;
return true;
}
}
return false;
}
void store_wide(const Key& key, int color, Result result, bool witness)
{
const std::size_t home = key_hash(key) & mask;
std::size_t target = home;
for (std::size_t offset = 0; offset < ProbeCount; ++offset)
{
const std::size_t index = (home + offset) & mask;
WideEntry& entry = wide_entries[index];
if (!entry.valid || entry.key == key)
{
target = index;
break;
}
}
WideEntry& entry = wide_entries[target];
if (!entry.valid || !(entry.key == key))
{
entry.key = key;
entry.known = 0;
entry.wins = 0;
entry.witnesses = 0;
entry.valid = true;
}
const std::uint8_t bit = std::uint8_t(1) << color;
entry.known |= bit;
if (result == Result::Win)
entry.wins |= bit;
else
{
entry.wins &= ~bit;
entry.witnesses &= ~bit;
}
if (witness)
entry.witnesses |= bit;
}
bool compact;
std::vector<std::uint64_t> compact_entries;
std::vector<WideEntry> wide_entries;
std::size_t mask = 0;
};
class WinningSetTable
{
public:
WinningSetTable(int cells, std::size_t megabytes)
{
if (cells > 30 || megabytes == 0)
return;
constexpr std::size_t Scale = 1024ULL * 1024ULL;
const std::size_t requested = megabytes * Scale / sizeof(Entry);
std::size_t count = 1;
while (count < requested && count < (std::size_t(1) << 34))
count <<= 1;
if (count > requested && count > 1)
count >>= 1;
entries.resize(std::max<std::size_t>(count, 1024));
mask = entries.size() - 1;
}
bool find(std::uint64_t code, int color, std::uint64_t& cells) const
{
if (entries.empty())
return false;
const std::uint64_t tag = ((code << 1) | color) + 1;
const std::size_t home = mix64(tag) & mask;
for (std::size_t offset = 0; offset < ProbeCount; ++offset)
{
const Entry& entry = entries[(home + offset) & mask];
if (entry.tag == tag)
{
cells = entry.cells;
return true;
}
if (!entry.tag)
return false;
}
return false;
}
void store(std::uint64_t code, int color, std::uint64_t cells)
{
if (entries.empty() || !cells)
return;
const std::uint64_t tag = ((code << 1) | color) + 1;
const std::size_t home = mix64(tag) & mask;
std::size_t target = home;
for (std::size_t offset = 0; offset < ProbeCount; ++offset)
{
const std::size_t index = (home + offset) & mask;
if (!entries[index].tag || entries[index].tag == tag)
{
target = index;
break;
}
}
entries[target] = {tag, static_cast<std::uint32_t>(cells)};
}
std::size_t bytes() const { return entries.size() * sizeof(Entry); }
private:
static constexpr std::size_t ProbeCount = 4;
struct Entry
{
std::uint64_t tag = 0;
std::uint32_t cells = 0;
};
std::vector<Entry> entries;
std::size_t mask = 0;
};
class MonotoneTable
{
public:
MonotoneTable(int cells, std::size_t megabytes)
: compact(cells <= 30), cell_count(cells)
{
constexpr std::size_t Scale = 1024ULL * 1024ULL;
if (megabytes > std::numeric_limits<std::size_t>::max() / Scale)
throw std::invalid_argument("monotone table is too large");
const std::size_t bytes = megabytes * Scale;
if (compact)
{
std::size_t bucket_count = 1024;
const std::size_t bucket_budget = bytes / 4;
while (bucket_count
<= bucket_budget / (2 * sizeof(std::uint32_t)))
bucket_count <<= 1;
heads.assign(bucket_count, None);
bucket_mask = bucket_count - 1;
const std::size_t record_bytes =
bytes > bucket_count * sizeof(std::uint32_t)
? bytes - bucket_count * sizeof(std::uint32_t) : 0;
record_capacity = std::min<std::size_t>(
std::max<std::size_t>(record_bytes / sizeof(Record), 1024),
std::numeric_limits<std::uint32_t>::max());
records.reserve(record_capacity);
board_mask = (std::uint64_t(1) << cell_count) - 1;
return;
}
const std::size_t requested = bytes / sizeof(WideEntry);
std::size_t count = 1;
while (count < requested && count < (std::size_t(1) << 34))
count <<= 1;
if (count > requested && count > 1)
count >>= 1;
wide_entries.resize(std::max<std::size_t>(count, 1024));
bucket_mask = wide_entries.size() - 1;
}
bool find(const Key& key, int color, Result& result) const
{
if (!compact)
return find_wide(key, color, result);
const std::uint64_t occupied = key.black | key.white;
const std::uint64_t query_own = color == 0 ? key.black : key.white;
std::uint32_t index = heads[bucket(occupied, color)];
while (index != None)
{
const Record& record = records[index];
const std::uint64_t known_occupied =
record.data & board_mask;
const int known_color = (record.data >> (2 * cell_count)) & 1;
if (known_occupied == occupied && known_color == color)
{
const std::uint64_t known_own =
(record.data >> cell_count) & board_mask;
const bool win =
(record.data >> (2 * cell_count + 1)) & 1;
if (win && (query_own & ~known_own) == 0)
{
result = Result::Win;
return true;
}
if (!win && (known_own & ~query_own) == 0)
{
result = Result::Loss;
return true;
}
}
index = record.next;
}
return false;
}
bool find_witness(const Key& key, int color,
std::uint64_t& witness) const
{
if (!compact)
return false;
const std::uint64_t occupied = key.black | key.white;
const std::uint64_t own = color == 0 ? key.black : key.white;
std::uint32_t index = heads[bucket(occupied, color)];
while (index != None)
{
const Record& record = records[index];
const std::uint64_t known_occupied = record.data & board_mask;
const std::uint64_t known_own =
(record.data >> cell_count) & board_mask;
const int known_color =
(record.data >> (2 * cell_count)) & 1;
const bool known_win =
(record.data >> (2 * cell_count + 1)) & 1;
if (known_occupied == occupied && known_color == color
&& known_win && record.witness
&& known_own == own)
{
witness = record.witness;
return true;
}
index = record.next;
}
return false;
}
void store(const Key& key, int color, Result result,
std::uint64_t witness = 0)
{
if (!compact)
{
store_wide(key, color, result);
return;
}
const std::uint64_t occupied = key.black | key.white;
const std::uint64_t own = color == 0 ? key.black : key.white;
const bool win = result == Result::Win;
const std::size_t home = bucket(occupied, color);
std::uint32_t current = heads[home];
std::uint32_t previous = None;
while (current != None)
{
Record& record = records[current];
const std::uint32_t next = record.next;
const std::uint64_t known_occupied = record.data & board_mask;
const int known_color = (record.data >> (2 * cell_count)) & 1;
const bool known_win =
(record.data >> (2 * cell_count + 1)) & 1;
if (known_occupied == occupied && known_color == color
&& known_win == win)
{
const std::uint64_t known_own =
(record.data >> cell_count) & board_mask;
const bool known_dominates = win
? (own & ~known_own) == 0
: (known_own & ~own) == 0;
if (known_dominates)
{
if (win && witness && known_own == own)
record.witness = static_cast<std::uint32_t>(witness);
return;
}
const bool new_dominates = win
? (known_own & ~own) == 0
: (own & ~known_own) == 0;
if (new_dominates)
{
if (previous == None)
heads[home] = next;
else
records[previous].next = next;
record.next = free_head;
free_head = current;
current = next;
continue;
}
}
previous = current;
current = next;
}
const std::uint64_t data = occupied | (own << cell_count)
| (std::uint64_t(color) << (2 * cell_count))
| (std::uint64_t(win) << (2 * cell_count + 1));
std::uint32_t index;
if (records.size() < record_capacity)
{
index = static_cast<std::uint32_t>(records.size());
records.push_back({data, heads[home],
static_cast<std::uint32_t>(witness)});
}
else if (free_head != None)
{
index = free_head;
free_head = records[index].next;
records[index] = {data, heads[home],
static_cast<std::uint32_t>(witness)};
}
else
return;
heads[home] = index;
}
std::size_t bytes() const
{
return compact
? heads.size() * sizeof(std::uint32_t)
+ records.capacity() * sizeof(Record)
: wide_entries.size() * sizeof(WideEntry);
}
private:
static constexpr std::size_t ProbeCount = 16;
static constexpr std::uint32_t None = 0xffffffffU;
struct Record
{
std::uint64_t data;
std::uint32_t next;
std::uint32_t witness;
};
struct WideEntry
{
Key key{};
bool valid = false;
std::uint8_t known = 0;
std::uint8_t wins = 0;
};
std::size_t bucket(std::uint64_t occupied, int color) const
{
return mix64(occupied
^ (std::uint64_t(color) * 0x9e3779b97f4a7c15ULL))
& bucket_mask;
}
bool find_wide(const Key& key, int color, Result& result) const
{
const std::uint64_t occupied = key.black | key.white;
const std::size_t home = bucket(occupied, color);
for (std::size_t offset = 0; offset < ProbeCount; ++offset)
{
const WideEntry& entry =
wide_entries[(home + offset) & bucket_mask];
if (!entry.valid
|| (entry.key.black | entry.key.white) != occupied)
continue;
const std::uint8_t bit = std::uint8_t(1) << color;
if (!(entry.known & bit))
continue;
const std::uint64_t query_own = color == 0 ? key.black : key.white;
const std::uint64_t known_own =
color == 0 ? entry.key.black : entry.key.white;
if ((entry.wins & bit) && (query_own & ~known_own) == 0)
{
result = Result::Win;
return true;
}
if (!(entry.wins & bit) && (known_own & ~query_own) == 0)
{
result = Result::Loss;
return true;
}
}
return false;
}
void store_wide(const Key& key, int color, Result result)
{
const std::uint64_t occupied = key.black | key.white;
const std::size_t home = bucket(occupied, color);
std::size_t target = home;
for (std::size_t offset = 0; offset < ProbeCount; ++offset)
{
const std::size_t index = (home + offset) & bucket_mask;
WideEntry& entry = wide_entries[index];
if (!entry.valid || entry.key == key)
{
target = index;
break;
}
}
WideEntry& entry = wide_entries[target];
if (!entry.valid || !(entry.key == key))
{
entry.key = key;
entry.known = 0;
entry.wins = 0;
entry.valid = true;
}
const std::uint8_t bit = std::uint8_t(1) << color;
entry.known |= bit;
if (result == Result::Win)
entry.wins |= bit;
else
entry.wins &= ~bit;
}
bool compact;
int cell_count;
std::uint64_t board_mask = 0;
std::vector<std::uint32_t> heads;
std::vector<Record> records;
std::uint32_t free_head = None;
std::size_t record_capacity = 0;
std::vector<WideEntry> wide_entries;
std::size_t bucket_mask = 0;
};
class Board
{
public:
explicit Board(int size)
: n(size), cells(size >= 1 && size <= 8 ? size * size : 0)
{
if (n < 1 || n > 8)
throw std::invalid_argument("board size must be in 1..8");
board_mask = cells == 64 ? ~std::uint64_t(0)
: (std::uint64_t(1) << cells) - 1;
for (int row = 0; row < n; ++row)
for (int col = 0; col < n; ++col)
{
const int point = index(col, row);
std::uint64_t adjacent = 0;
constexpr int dc[] = {1, -1, 0, 0, 1, -1};
constexpr int dr[] = {0, 0, 1, -1, -1, 1};
for (int direction = 0; direction < 6; ++direction)
{
const int next_col = col + dc[direction];
const int next_row = row + dr[direction];
if (inside(next_col, next_row))
adjacent |= bit(index(next_col, next_row));
}
neighbors[point] = adjacent;
transform[0][point] = point;
transform[1][point] = index(n - 1 - col, n - 1 - row);
transform[2][point] = index(row, col);
transform[3][point] = index(n - 1 - row, n - 1 - col);
}
std::array<std::uint64_t, 64> powers{};
powers[0] = 1;
for (int point = 1; point < cells; ++point)
powers[point] = powers[point - 1] * 3;
for (int symmetry = 0; symmetry < 4; ++symmetry)
for (int point = 0; point < cells; ++point)
transformed_power[symmetry][point] =
powers[transform[symmetry][point]];
for (int value = 0; value < n; ++value)
{
north |= bit(index(value, 0));
south |= bit(index(value, n - 1));
west |= bit(index(0, value));
east |= bit(index(n - 1, value));
}
}
int index(int col, int row) const { return row * n + col; }
bool inside(int col, int row) const
{
return col >= 0 && col < n && row >= 0 && row < n;
}
static std::uint64_t bit(int point) { return std::uint64_t(1) << point; }
bool connected(std::uint64_t stones, int color) const
{
const std::uint64_t first = color == 0 ? north : west;
const std::uint64_t second = color == 0 ? south : east;
std::uint64_t frontier = stones & first;
std::uint64_t seen = frontier;
while (frontier)
{
if (frontier & second)
return true;
frontier = adjacent(frontier) & stones & ~seen;
seen |= frontier;
}
return false;
}
std::uint64_t adjacent(std::uint64_t stones) const
{
return (((stones & ~east) << 1)
| ((stones & ~west) >> 1)
| (stones << n)
| (stones >> n)
| ((stones & ~(north | east)) >> (n - 1))
| ((stones & ~(south | west)) << (n - 1)))
& board_mask;
}
std::uint64_t connecting_moves(std::uint64_t stones, int color) const
{
const std::uint64_t first = color == 0 ? north : west;
const std::uint64_t second = color == 0 ? south : east;
auto closure = [&](std::uint64_t frontier) {
std::uint64_t seen = frontier;
while (frontier)
{
frontier = adjacent(frontier) & stones & ~seen;
seen |= frontier;
}
return seen;
};
const std::uint64_t from_first = closure(stones & first);
const std::uint64_t from_second = closure(stones & second);
const std::uint64_t touches_first = first | adjacent(from_first);
const std::uint64_t touches_second = second | adjacent(from_second);
return touches_first & touches_second & ~stones;
}
std::uint64_t terminal_trap(std::uint64_t own,
std::uint64_t other, int color) const
{
// Leave the opponent one empty cell that completes its connection.
// Taking every other empty cell is safe and leaves no legal reply.
const std::uint64_t empty = board_mask & ~(own | other);
if (std::popcount(empty) < 2)
return 0;
std::uint64_t pivots =
connecting_moves(other, color ^ 1) & empty;
while (pivots)
{
const std::uint64_t pivot = pivots & -pivots;
pivots &= pivots - 1;
const std::uint64_t response = empty & ~pivot;
if (!connected(own | response, color))
return response;
}
return 0;
}
std::uint64_t disjoint_pair_reserve(std::uint64_t stones,
std::uint64_t blocked,
int color) const
{
const std::uint64_t empty = board_mask & ~(stones | blocked);
if (std::popcount(empty) < 4)
return 0;
const std::uint64_t pivots = connecting_moves(stones, color) & empty;
std::array<std::uint64_t, 36> graph{};
std::uint64_t first_work = empty;
while (first_work)
{
const std::uint64_t first = first_work & -first_work;
first_work &= first_work - 1;
const int first_point = std::countr_zero(first);
std::uint64_t seconds = pivots & first
? empty & ~first
: connecting_moves(stones | first, color) & empty & ~first;
seconds &= ~((first << 1) - 1);
graph[first_point] |= seconds;
while (seconds)
{
const std::uint64_t second = seconds & -seconds;
seconds &= seconds - 1;
graph[std::countr_zero(second)] |= first;
}
}
first_work = empty;
while (first_work)
{
const std::uint64_t first = first_work & -first_work;
first_work &= first_work - 1;
std::uint64_t seconds =
graph[std::countr_zero(first)] & first_work;
while (seconds)
{
const std::uint64_t second = seconds & -seconds;
seconds &= seconds - 1;
const std::uint64_t remaining = empty & ~(first | second);
std::uint64_t third_work = remaining;
while (third_work)
{
const std::uint64_t third = third_work & -third_work;
third_work &= third_work - 1;
const std::uint64_t fourth =
graph[std::countr_zero(third)] & remaining;
if (fourth)
return first | second | third | (fourth & -fourth);
}
}
}
return 0;
}
std::uint64_t disjoint_pair_trap(std::uint64_t own,
std::uint64_t other, int color) const
{
const std::uint64_t empty = board_mask & ~(own | other);
if (std::popcount(empty) < 5)
return 0;
const std::uint64_t reserve =
disjoint_pair_reserve(other, own, color ^ 1);
if (!reserve)
return 0;
const std::uint64_t response = empty & ~reserve;
return response && !connected(own | response, color)
? response : 0;
}
std::uint64_t simplicial_cells(std::uint64_t own,
std::uint64_t other, int color) const
{
if (cells > 36)
return 0;
const std::array<std::uint64_t, 38> graph =
connection_graph(own, other, color);
const std::uint64_t empty = board_mask & ~(own | other);
std::uint64_t simplicial = 0;
std::uint64_t work = empty;
while (work)
{
const int point = std::countr_zero(work);
work &= work - 1;
bool clique = true;
const std::uint64_t boundary = graph[point];
std::uint64_t boundary_work = boundary;
while (boundary_work && clique)
{
const int vertex = std::countr_zero(boundary_work);
boundary_work &= boundary_work - 1;
clique = ((boundary & ~bit(vertex)) & ~graph[vertex]) == 0;
}
if (clique)
simplicial |= bit(point);
}
return simplicial;
}
std::uint64_t transformed(std::uint64_t stones, int symmetry) const
{
std::uint64_t output = 0;
while (stones)
{
const int point = std::countr_zero(stones);
stones &= stones - 1;
output |= bit(transform[symmetry][point]);
}
return output;
}
struct Position
{
std::uint64_t black = 0;
std::uint64_t white = 0;
std::array<std::uint64_t, 4> black_symmetry{};
std::array<std::uint64_t, 4> white_symmetry{};
std::array<std::uint64_t, 4> code{};
std::array<std::uint64_t, 4> occupied_code{};
};
Position position(std::uint64_t black, std::uint64_t white) const
{
Position result;
result.black = black;
result.white = white;
std::uint64_t work = black;
while (work)
{
const int point = std::countr_zero(work);
work &= work - 1;
for (int symmetry = 0; symmetry < 4; ++symmetry)
{
const std::uint64_t value =
transformed_power[symmetry][point];
result.black_symmetry[symmetry] |=
bit(transform[symmetry][point]);
result.code[symmetry] += value;
result.occupied_code[symmetry] += value;
}
}
work = white;
while (work)
{
const int point = std::countr_zero(work);
work &= work - 1;
for (int symmetry = 0; symmetry < 4; ++symmetry)
{
const std::uint64_t value =
transformed_power[symmetry][point];
result.white_symmetry[symmetry] |=
bit(transform[symmetry][point]);
result.code[symmetry] += 2 * value;
result.occupied_code[symmetry] += value;
}
}
return result;
}
void place(Position& position, int point, int color) const
{
(color == 0 ? position.black : position.white) |= bit(point);
for (int symmetry = 0; symmetry < 4; ++symmetry)
{
const std::uint64_t value = transformed_power[symmetry][point];
(color == 0 ? position.black_symmetry[symmetry]
: position.white_symmetry[symmetry])
|= bit(transform[symmetry][point]);
position.code[symmetry] += (color == 0 ? value : 2 * value);
position.occupied_code[symmetry] += value;
}
}
void unplace(Position& position, int point, int color) const
{
(color == 0 ? position.black : position.white) &= ~bit(point);
for (int symmetry = 0; symmetry < 4; ++symmetry)
{
const std::uint64_t value = transformed_power[symmetry][point];
(color == 0 ? position.black_symmetry[symmetry]
: position.white_symmetry[symmetry])
&= ~bit(transform[symmetry][point]);
position.code[symmetry] -= (color == 0 ? value : 2 * value);
position.occupied_code[symmetry] -= value;
}
}
struct Canonical
{
Key key;
int color;
std::uint64_t code;
int symmetry;
};
Canonical canonical(const Position& position, int color,
bool use_symmetry, bool need_key) const
{
Canonical best{{position.black, position.white}, color,
position.code[0], 0};
if (!use_symmetry)
return best;
if (cells <= 36)
{
int best_symmetry = 0;
for (int symmetry = 1; symmetry < 4; ++symmetry)
{
const bool swaps = symmetry >= 2;
const std::uint64_t next_code = swaps
? 3 * position.occupied_code[symmetry]
- position.code[symmetry]
: position.code[symmetry];
const int next_color = swaps ? color ^ 1 : color;
if (std::tie(next_code, next_color)
< std::tie(best.code, best.color))
{
best.code = next_code;
best.color = next_color;
best_symmetry = symmetry;
best.symmetry = symmetry;
}
}
if (need_key && best_symmetry != 0)
{
best.key.black = position.black_symmetry[best_symmetry];
best.key.white = position.white_symmetry[best_symmetry];
if (best_symmetry >= 2)
std::swap(best.key.black, best.key.white);
}
return best;
}
for (int symmetry = 1; symmetry < 4; ++symmetry)
{
std::uint64_t next_black = position.black_symmetry[symmetry];
std::uint64_t next_white = position.white_symmetry[symmetry];
const bool swaps = symmetry >= 2;
if (swaps)
std::swap(next_black, next_white);
const int next_color = swaps ? color ^ 1 : color;
const Key candidate{next_black, next_white};
if (std::tie(candidate.black, candidate.white, next_color)
< std::tie(best.key.black, best.key.white, best.color))
best = {candidate, next_color, 0, symmetry};
}
return best;
}
std::string cell_name(int point) const
{
return std::string(1, static_cast<char>('a' + point % n))
+ std::to_string(point / n + 1);
}