-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZh1b2b.cpp
1847 lines (1733 loc) · 60.5 KB
/
Zh1b2b.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
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
/*
Based on code posted to <http://forum.enjoysudoku.com/3-77us-solver-2-8g-cpu-testcase-17sodoku-t30470.html>
by user zhouyundong_2012.
The copyright is not specified.
*/
#include "main.h"
#include "Zh1b2b.h"
// row unsolved 6 bits per digit lookup table
uint64_t zh2b_t_runsolved[9] = { 077 , 077 << 6 , 077 << 12 , 077 << 18 , 077 << 24 ,
(uint64_t)077 << 32 ,(uint64_t)077 << 38 ,(uint64_t)077 << 44 ,(uint64_t)077 << 50 };
uint32_t zh2b_t_runsolvedshift[9] = { 0,6,12,18,24,32,38,44,50 };
//========================= global variable and working areas
extern ZH_GLOBAL zh_g;
ZH2B zh2b[40]; //
//ZH2B zh2b_i, zh2b_i1;
ZH2B_GLOBAL zh2b_g; // 2 bands 9 digits
ZH2B5_GLOBAL zh2b5_g; // 2_5 digits 2 bands
ZH2B_1D_GLOBAL zh2b1d_g; // one digit 2 bands
ZH2B_1D zh2b1d[6]; // maxi 6 guesses, one per row
ZH2B5 zh2b5[10]; // solved digit per digit
ZHONE_GLOBAL zh1b_g;
ZHONE zhone[20]; // one band 9 digits
ZHONE zhone_i;
//#include "go_17sol_zx_UaCollector_cpp.h"
//GENUAS_1Bx genuas1b;
//============================= ZH_GLOBAL code
ZH2B_GLOBAL::ZH2B_GLOBAL(){
zsol = 0; // no solution unless required buy the user
nctlg = 0;
}
void ZH2B_GLOBAL::GetBands(int * g1, int * g2) {
// build sol per digit and pm per digit at start
memset(fd_sols, 0, sizeof fd_sols);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 3; j++) {
int cell = 9 * j + i, dig = puz0[cell];
fd_sols[1][dig].bf.u32[0] |= Zhoucol << i;
fd_sols[1][dig].bf.u32[1] |= Zhoucol << i;
fd_sols[0][dig].bf.u32[0] |= 1 << cell;
dig = puz0[cell+27];
fd_sols[1][dig].bf.u32[0] |= Zhoucol << i;
fd_sols[1][dig].bf.u32[1] |= Zhoucol << i;
fd_sols[0][dig].bf.u32[1] |= 1 << cell;
}
}
}
void ZH2B_GLOBAL::InitGangster(int * g0, int * g1) {
uint64_t col64 = (uint64_t)Zhoucol | ((uint64_t)Zhoucol << 32);
memcpy(fd_revised, fd_sols[1], sizeof fd_revised);
for (int i = 0; i < 9; i++, col64 <<= 1) {
if (g0[i] == g1[i])continue;
int changes = g0[i] ^ g1[i]; // one added one cleared
for (int d = 0, bit = 1; d < 9; d++, bit <<= 1) {// check digits
if (!(changes & bit)) continue;
if (g0[i] & bit)fd_revised[d] &= ~col64;
else fd_revised[d] |= col64;
}
}
}
uint64_t ZH2B_GLOBAL::BuildUaret(BF64 * wsol) {
ua_ret = 0;
for (int i = 0; i < 9; i++) {
BF64 w = wsol[i] - fd_sols[0][i];
ua_ret |= w.bf.u64;
}
return ua_ret;
}
//============= zh2B code for uas 2 bands and validity 2 bands puzzle
/*
void ZH2b::Start_nFloors(int floors, BF64 * mypm) {
cells_unsolved;bf.u64=BIT_SET_2X^ solved_cells;
for (uint32_t i = 0; i < nd; i++) {
myfd[i] &= cells_unsolved;
if (myfd[i].count == 6)return ;
}
}*/
//============================================ ZH2B code
void ZH2B::Init_std_bands() {//init after zh1b_g getband
zh2b_g.ndigits = 9;
memcpy(this, zh2b_start, sizeof zh2b_start);
memcpy(FD, zh2b_g.fd_sols[1], sizeof FD);
memset(CompFD, 0, sizeof CompFD);
}
void ZH2B::Init_gang() {//init after zh1b_g InitGangster
zh2b_g.ndigits = 9;
memcpy(this, zh2b_start, sizeof zh2b_start);
memcpy(FD, zh2b_g.fd_revised, sizeof FD);
memset(CompFD, 0, sizeof CompFD);
}
void ZH2B::DebugSol() {//init after zh1b_g getband
zh2b_g.ndigits = 9;
memset(this, 0, sizeof zh2b[0]);
memcpy(FD, zh2b_g.fd_sols[0], sizeof FD);
ImageCandidats();
}
#define UPDN(I,J)A=FD[I].bf.u32[J];\
Shrink = (TblShrinkMask[A & 0x1FF] | \
TblShrinkMask[ (A>>9) & 0x1FF]<<3 | \
TblShrinkMask[ (A>>18) & 0x1FF]<<6);\
if ((A &=TblComplexMask[Shrink]) ==0) return 0; \
S = ((A | (A >> 9) | (A >> 18)) & 0x1FF); \
FD[I].bf.u32[1 - J] &= TblMaskSingle[S]; \
S = TblRowUniq[TblShrinkSingle[Shrink] & TblColumnSingle[S]]; \
CompFD[I].bf.u32[J] = FD[I].bf.u32[J] = A;
#define UPWCL(I,P,Q,R,T,U,V,W,X)cl = ~(A & TblRowMask[S]); \
cells_unsolved.bf.u32[I] &= cl; \
wcl[P]&= cl;wcl[Q]&= cl;wcl[R]&= cl;wcl[T]&= cl;\
wcl[U]&= cl;wcl[V]&= cl;wcl[W]&= cl;wcl[X]&= cl;
#define UPWCL5(I,P,Q,R,T)cl = ~(A & TblRowMask[S]); \
cells_unsolved.bf.u32[I] &= cl; \
wcl[P]&= cl;wcl[Q]&= cl;wcl[R]&= cl;wcl[T]&= cl;
#define UPDN1(I,P,Q,R,T,U,V,W,X)Shrink = (TblShrinkMask[A & 0x1FF] | \
TblShrinkMask[ (A>>9) & 0x1FF]<<3 | \
TblShrinkMask[ (A>>18) & 0x1FF]<<6);\
if ((A &=TblComplexMask[Shrink]) ==0) return 0; \
S = ((A | (A >> 9) | (A >> 18)) & 0x1FF); \
S = TblRowUniq[TblShrinkSingle[Shrink] & TblColumnSingle[S]]; \
if ((A>>27) != S){\
cl = ~(A & TblRowMask[S]); \
A=(A&BIT_SET_27) | (S<<27);\
cells_unsolved &= cl; \
wcl[P]&= cl;wcl[Q]&= cl;wcl[R]&= cl;wcl[T]&= cl;\
wcl[U]&= cl;wcl[V]&= cl;wcl[W]&= cl;wcl[X]&= cl;}\
CompFD[I] = FD[I] = A
//================================ZH2B code
void ZH2B::InitTclues(uint32_t * tclues, int n) {
memset(zh2b_g.Digit_cell_Assigned_init, 0, sizeof zh2b_g.Digit_cell_Assigned_init);
for (int icell = 0; icell < n; icell++) {
int cell = tclues[icell], digit = zh2b_g.puz0[cell];
int xcell = C_To128[cell]; // the cell value in 3x32 of a 128 bits map
Assign(digit, cell, xcell);
zh2b_g.Digit_cell_Assigned_init[digit].Set(xcell);
}
for (int i = 0; i < 9; i++) FD[i] &= cells_unsolved |
zh2b_g.Digit_cell_Assigned_init[i];
}
int ZH2B::ApplySingleOrEmptyCells() {// only singles
zh2b_g.single_applied = 0;
uint64_t * map = &FD[0].bf.u64;
uint64_t unsolved = cells_unsolved.bf.u64;
register uint64_t R2 = map[0] & map[1],
R1 = (map[0] | map[1]), Map;// digits 12
Map = map[2]; R2 |= R1 & Map; R1 |= Map;
Map = map[3]; R2 |= R1 & Map; R1 |= Map;
Map = map[4]; R2 |= R1 & Map; R1 |= Map;
Map = map[5]; R2 |= R1 & Map; R1 |= Map;
Map = map[6]; R2 |= R1 & Map; R1 |= Map;
Map = map[7]; R2 |= R1 & Map; R1 |= Map;
Map = map[8]; R2 |= R1 & Map; R1 |= Map;
if (unsolved & (~R1)) return 1; // locked
R1 &= ~R2;
R1 &= unsolved; // forget solved seen as singles
if (R1) zh2b_g.single_applied = 1;
else return 0;
while (R1) {// usually a very small number of cells to assign
uint32_t res;
if (!bitscanforward64(res, R1)) break;
uint64_t bit = (uint64_t)1 << res; // switch to the bit value
R1 &= ~bit; // clear the bit
// call Seta(int digit, int xcell) so find digit
for (int idig = 0; idig < 9; idig++) {
if (map[idig] & bit) {// this is the digit
// if (FD[idig].Off(res)) return 1; // invalid, gane locked
// Seta(idig, res);
int cell = From_128_To_81[res];
Assign(idig, cell, res);
goto nextr1;// finished for that cell
}
}
return 1; //conflict with a previous cell assugn
nextr1: {}
}
return 0;// not locked
}
int ZH2B::Seta(int digit, int xcell) { // single in cell
int cell = From_128_To_81[xcell],
block = TblBoard_Block[cell];
if (FD[digit].Off(xcell)) return 1; // not valid
Assign(digit, cell, xcell);
BF64 *Fd = &FD[digit];
*Fd &= AssignMask_Digit[cell].u64[0];
int ddig = 6 * digit;
if (digit > 4) ddig += 2;// second bloc of 32 bits
rows_unsolved.Clear(ddig + C_row[cell]);//6*digit + row
cells_unsolved.Clear(xcell);
BF64 * RF = &FD[8];
for (; RF >= FD; RF--)RF->Clear(xcell);
Fd->Set(xcell); // restore bit for digit assigned
return 0;
}
int ZH2B::Update(){
int Shrink = 1;
register int S, A;
register unsigned int cl, *wcl = FD[0].bf.u32;
while (Shrink ){
Shrink = 0;
if (!rows_unsolved.bf.u32[0])goto digit5;
{register unsigned int AR = rows_unsolved.bf.u32[0];// valid for digits 0,1,2,3,4
if (!(AR & 077))goto digit1;
//=digit 0
if (FD[0].bf.u32[0] == CompFD[0].bf.u32[0])goto digit0b;
UPDN(0,0) if ((AR & 7) != S){
AR &= 07777777770 | S; UPWCL(0, 2,4,6,8,10,12,14,16) }
digit0b:if (FD[0].bf.u32[1] ==CompFD[0].bf.u32[1])goto digit1;
UPDN(0,1) if (((AR >> 3) & 7) != S){
AR &= 07777777707 | (S << 3); UPWCL(1, 3,5,7,9,11,13,15,17) }
digit1: if (!(AR & 07700))goto digit2;
if (FD[1].bf.u32[0] == CompFD[1].bf.u32[0])goto digit1b;
UPDN(1,0) if (((AR >> 6) & 7) != S){
AR &= 07777777077 | (S << 6);UPWCL(0, 0, 4, 6, 8, 10, 12, 14, 16) }
digit1b:if (FD[1].bf.u32[1] == CompFD[1].bf.u32[1])goto digit2;
UPDN(1,1) if (((AR >> 9) & 7) != S){
AR &= 07777770777 | (S << 9);UPWCL(1, 1, 5, 7, 9, 11, 13, 15, 17) }
digit2: if (!(AR & 0770000))goto digit3;
if (FD[2].bf.u32[0] == CompFD[2].bf.u32[0])goto digit2b;
UPDN(2,0) if (((AR >> 12) & 7) != S){
AR &= 07777707777 | (S << 12); UPWCL(0, 0, 2, 6, 8, 10, 12, 14, 16)}
digit2b:if (FD[2].bf.u32[1] == CompFD[2].bf.u32[1])goto digit3;
UPDN(2,1) if (((AR >> 15) & 7) != S){
AR &= 07777077777 | (S << 15); UPWCL(1, 1, 3, 7, 9, 11, 13, 15, 17) }
digit3: if (!(AR & 077000000))goto digit4;
if (FD[3].bf.u32[0] == CompFD[3].bf.u32[0])goto digit3b;
UPDN(3,0) if (((AR >> 18) & 7) != S){
AR &= 07770777777 | (S << 18); UPWCL(0, 0, 2, 4, 8, 10, 12, 14, 16) }
digit3b: if (FD[3].bf.u32[1] == CompFD[3].bf.u32[1])goto digit4;
UPDN(3, 1)if (((AR >> 21) & 7) != S){
AR &= 07707777777 | (S << 21); UPWCL(1, 1, 3, 5, 9, 11, 13, 15, 17) }
digit4:if (!(AR & 07700000000))goto end01234;
if (FD[4].bf.u32[0] == CompFD[4].bf.u32[0])goto digit4b;
UPDN(4, 0)if (((AR >> 24) & 7) != S){
AR &= 07077777777 | (S << 24); UPWCL(0, 0, 2, 4, 6, 10, 12, 14, 16) }
digit4b:if (FD[4].bf.u32[1] == CompFD[4].bf.u32[1])goto end01234;
UPDN(4, 1)if (((AR >> 27) & 7) != S){
AR &= 0777777777 | (S << 27); UPWCL(1, 1, 3, 5, 7, 11, 13, 15, 17) }
end01234: rows_unsolved.bf.u32[0] = AR;
}// end of validity for AR 01234
digit5:
if (!rows_unsolved.bf.u32[1])continue; // second lot 4 digits
{register unsigned int AR = rows_unsolved.bf.u32[1];// valid for digits 5,6,7,8
if (!(AR & 077))goto digit6;
if (FD[5].bf.u32[0] == CompFD[5].bf.u32[0])goto digit5b;
UPDN(5, 0) if ((AR & 7) != S){
AR &= 07777777770 | S; UPWCL(0, 0, 2, 4, 6, 8, 12, 14, 16) }
digit5b:if (FD[5].bf.u32[1] == CompFD[5].bf.u32[1])goto digit6;
UPDN(5, 1) if (((AR >> 3) & 7) != S){
AR &= 07777777707 | (S << 3); UPWCL(1, 1, 3, 5, 7, 9, 13, 15, 17) }
digit6: if (!(AR & 07700))goto digit7;
if (FD[6].bf.u32[0] == CompFD[6].bf.u32[0])goto digit6b;
UPDN(6, 0) if (((AR >> 6) & 7) != S){
AR &= 07777777077 | (S << 6); UPWCL(0, 0, 2, 4, 6, 8, 10, 14, 16) }
digit6b: if (FD[6].bf.u32[1] == CompFD[6].bf.u32[1])goto digit7;
UPDN(6, 1) if (((AR >> 9) & 7) != S){
AR &= 07777770777 | (S << 9); UPWCL(1, 1, 3, 5, 7, 9, 11, 15, 17) }
digit7: if (!(AR & 0770000))goto digit8;
if (FD[7].bf.u32[0] == CompFD[7].bf.u32[0])goto digit7b;
UPDN(7, 0)if (((AR >> 12) & 7) != S){
AR &= 07777707777 | (S << 12); UPWCL(0, 0, 2, 4, 6, 8, 10, 12, 16) }
digit7b: if (FD[7].bf.u32[1] == CompFD[7].bf.u32[1])goto digit8;
UPDN(7, 1) if (((AR >> 15) & 7) != S){
AR &= 07777077777 | (S << 15); UPWCL(1, 1, 3, 5, 7, 9, 11, 13, 17) }
digit8: if (!(AR & 077000000))goto end5678;
if (FD[8].bf.u32[0] == CompFD[8].bf.u32[0])goto digit8b;
UPDN(8,0) if (((AR >> 18) & 7) != S){
AR &= 07770777777 | (S << 18); UPWCL(0, 0, 2, 4, 6, 8, 10, 12, 14) }
digit8b: if (FD[8].bf.u32[1] == CompFD[8].bf.u32[1])goto end5678;
UPDN(8,1) if (((AR >> 21) & 7) != S){
AR &= 07707777777 | (S << 21); UPWCL(1, 1, 3, 5, 7, 9, 11, 13, 15) }
end5678:rows_unsolved.bf.u32[1] = AR;
}// end of validity for AR
}// end while
#ifdef DIAG
cout << "end update cycle" << endl;
Debug(1);
ImageCandidats();
#endif
return 1;
}
int ZH2B::FullUpdate() {
if (zh2b_g.go_back) return 0;
while (1) {
if (!Update()) return 0; // game locked in update
if (!Unsolved_Count()) return 2;
if (ApplySingleOrEmptyCells()) return 0; // locked empty cell or conflict singles in cells
if (zh2b_g.single_applied) {
continue;
}
break;
}
return 1;
}
char * ZH2B::SetKnown(char * zs) {
strcpy(zs, &empty_puzzle[27]);
int tdig[9];// build the table of digits
register int A = rows_unsolved.bf.u32[0];
tdig[0] = A & 077; A >>= 6; tdig[1] = A & 077; A >>= 6; tdig[2] = A & 077; A >>= 6;
tdig[3] = A & 077; A >>= 6; tdig[4] = A & 077;
A = rows_unsolved.bf.u32[1];
tdig[5] = A & 077; A >>= 6; tdig[6] = A & 077; A >>= 6;
tdig[7] = A & 077; A >>= 6; tdig[8] = A & 077;
for (int idig = 0; idig < 9; idig++) {// one digit
int arowsj = tdig[idig];// 6 rows
if (arowsj == 077) continue;
for (int ib = 0; ib < 2; ib++) {// 3 blocs per digit
int arows = (arowsj >> (3 * ib)) & 7;
if (arows == 7) continue; // not assigned
unsigned int band = FD[idig].bf.u32[ib];
for (int j = 0; j < 3; j++) if (!(arows & (1 << j))) {
int row = (band >> TblMult9[j]) & 0x1ff;
uint32_t irow;
bitscanforward(irow, row);
int cell = Tblstartblock[ib] + TblMult9[j] + irow;
zs[cell] = idig + '1';
}
}
}
return zs;
}
uint64_t ZH2B::ValidXY(uint32_t * tclues, int n, int test) {
if (test)cout << "entry validxy" << endl;
zh2b_g.ua_ret = 0;
Init_std_bands();
InitTclues(tclues, n);
if (FullUpdate()) {
if (test) ImageCandidats();
if (rows_unsolved.isEmpty()) return 0;// solved
if(n>15){ //try to find a UA minirow in band b
register uint32_t b2 = rows_unsolved.bf.u32[1];
if (!b2)return 0; // if b2 solved, all is solved
for (uint32_t i = 0, mask = 7; i < 9; i++, mask <<= 3) {
uint32_t mini = b2 & mask;
if (_popcnt32(mini) < 2) continue;
ZH2B * mynext = (this + 1);
*mynext = *this;
for (uint32_t j = 0, bit = 1; j < 27; j++, bit <<= 1) {
if (bit&mini)continue;
if (b2&bit) {
int cell = j + 27, digit = zh2b_g.puz0[cell];
mynext->SetaC(digit, cell);
}
}
mynext->ComputeNext();
if (zh2b_g.ua_ret) {
if (test) {
cout << Char2Xout(zh2b_g.ua_ret) << " ua mini " << i << endl;
}
return zh2b_g.ua_ret;// return if ua found
}
}
}
// if does not work, suspect valid try best case first
if (test) cout << "no mini row ua , try best" << endl;
GuessValidB12();//std solve
}
return zh2b_g.ua_ret;
}
void ZH2B::DebugValidXY(uint32_t * tclues, int n, int test) {
if (test)cout << "entry validxy" << endl;
zh2b_g.ua_ret = 0;
Init_std_bands();
InitTclues(tclues, n);
ImageCandidats();
FullUpdate();
ImageCandidats();
for (int i = 0; i < 9; i++) FD[i] = zh2b_g.fd_sols[0][i];
cout << "registered solution" << endl;
ImageCandidats();
}
void ZH2B::GuessGo(int dig, BF64 & wsol) {// done in a new ocurrence
*this = *(this - 1); // apply wsol and make next step
FD[dig] = wsol;// update will do the job
ComputeNext();
}
void ZH2B::ComputeNext() {
int ir = FullUpdate();
if (ir == 1)GuessValidB12();
else if (ir == 2) {// solved
zh2b_g.ua_ret = 0;
for (int i = 0; i < 9; i++) {
BF64 w = FD[i] - zh2b_g.fd_sols[0][i];
zh2b_g.ua_ret |= w.bf.u64;
}
}
}
void ZH2B::GuessValidB12() {// digit with lowest count of unsolved
int mincount =100, digmin = 10;
for (uint32_t idig = 0; idig < 9; idig++) {
int cc = FD[idig].Count();
if (cc < 7) continue;// digit solved
if (cc < mincount) {mincount = cc; digmin = idig; }
}
if (digmin > 8) return;// should never be unsolved status
BF64 mysol = zh2b_g.fd_sols[0][digmin];
if ((FD[digmin] & mysol) == mysol) { // if all true possible try it
(this + 1)->GuessGo(digmin, mysol);
if (zh2b_g.ua_ret) return;// return if ua found
}
// try false
int r_unsolved = (rows_unsolved.bf.u64 >> zh2b_t_runsolvedshift[digmin]) & 077;
BF64 tuaw[500], tsolw[500];
int nuaw = zh2b1d_g.Go(mysol, FD[digmin], tsolw, tuaw, r_unsolved);
//cout << " nfalse perms=" << nuaw << endl;
for (int i = 0; i < nuaw ; i++) {
(this + 1)->GuessGo(digmin, tsolw[i]);
if (zh2b_g.ua_ret) return;// return if ua found
}
}
void ZH2B::Debug(int all) {
cout << "DEBUG nbsol=" << zh2b_g.nsol << " unsolved=" << Unsolved_Count() << endl;
// cout << zh1b_g.out27 << " band1 "<<endl;
char zi[82]; SetKnown(zi);
cout << zi << " known rows 1_6 digits " << endl;
if (!all) return;
cout << "map per digit bands 2 3" << endl;
for (int ib = 0; ib < 2; ib++) {
for (int ir = 0; ir < 3; ir++) {
for (int idig = 0; idig < 9; idig++) {
unsigned vf = FD[idig].bf.u32[ib];
unsigned wr = (vf >> (9 * ir)) & 0x1ff;
for (int k = 0; k < 9; k++) {
if (wr & (1 << k)) cout << idig + 1;
else cout << ".";
if (k == 2 || k == 5) cout << " ";
}
cout << " ";
}
cout << endl; //end of row
}
cout << endl; // end of block
}
cout << endl; // end of map per digit
}
int ZH2B::GetAllDigits(int cell) {
int ir = 0, xcell = C_To128[cell];;
for (int i = 0; i < 9; i++) if (FD[i].On(xcell))ir |= (1 << i);
return ir;
}
void ZH2B::ImageCandidats() {
int dig_cells[81]; for (int i = 0; i < 54; i++) dig_cells[i] = GetAllDigits(i);
int i, j, l, lcol[9], tcol = 0, ncand = 0;
cout << "PM map " << endl << endl;
for (i = 0; i < 9; i++) { // attention ici i indice colonne
lcol[i] = 2; // 2 mini tous chiffres imposés
for (j = 0; j < 6; j++) {
l = _popcnt32(dig_cells[9 * j + i]);
if (l > lcol[i]) lcol[i] = l;
}
tcol += lcol[i];
}
for (i = 0; i < 9; i++) {
if ((i == 3) || (i == 6))cout << "|";
cout << (char)('A' + i) << Blancs(lcol[i], 1);
}
cout << endl;
for (i = 0; i < 6; i++) { // maintenant indice ligne
if ((i == 3) || (i == 6)) {
for (int ix = 0; ix < (tcol + 10); ix++) cout << (char)'-';
cout << endl;
}
for (j = 0; j < 9; j++) {
if ((j == 3) || (j == 6))cout << "|";
int cell = 9 * i + j, digs = dig_cells[cell],
ndigs = _popcnt32(digs);
ncand += ndigs;
for (int id = 0; id < 9; id++)if (digs & (1 << id))
cout << id + 1;
cout << Blancs(lcol[j] + 1 - ndigs, 1);
} // end for j
cout << endl;
} // end for i
cout << endl;
}
//============================= ZH2B5 uas guas creation
/*
process using as source
fd_sols[1] is pm if source=0 fd_revised if source=1
the solution is known and is fd_sols[0]
all digits not in bitfield fl are known (usualy fl 2-4 digits)
internal myfd as initial value cleaned in zb2b_1d step by step
*/
uint64_t ZH2B5_GLOBAL::FindUAsInit(int fl, int source) {
BF64 * mypm = zh2b_g.fd_sols[1];
if (source) mypm = zh2b_g.fd_revised;
uint64_t solved_cells = 0;
uint32_t nd = 0;
for (int idig = 0, bit = 1; idig < 9; idig++, bit <<= 1) {
if (fl & bit) {
fdsw[0][nd] = zh2b_g.fd_sols[0][idig];
fdsw[2][nd].bf.u64 = (~fdsw[0][nd].bf.u64) & BIT_SET_2X;
myfd[nd++] = mypm[idig];
}
else solved_cells |= zh2b_g.fd_sols[0][idig].bf.u64;
}
ndigits = nd;
cells_unsolved.bf.u64 = BIT_SET_2X ^ solved_cells;
for (uint32_t i = 0; i < nd; i++) {
myfd[i] &= cells_unsolved;
if (myfd[i].Count() == 6)return 0;
}
return solved_cells;
// first cleaning return unsolved cells in bits
}
void ZH2B5_GLOBAL::CollectUas5() {
if (diag)cout << "entry CollectUas5()" << endl;
nuaf5 = 0;
memset(&zh2b5[0], 0, sizeof zh2b5[0]);
memcpy(zh2b5[0].FD, myfd, sizeof myfd);
memset(zh2b5[0].CompFD, 0, sizeof myfd);
zh2b5[0].cells_unsolved = cells_unsolved;
uint32_t t[5] = { 077,07777,0777777,077777777,07777777777 };
zh2b5[0].rows_unsolved.bf.u32[0] = t[ndigits-1];
if(diag)zh2b5[0].ImageCandidats();
//if (1) return;
zh2b5[0].ComputeNext5();
}
void ZH2B5_GLOBAL::ValidSol5(uint64_t * sol) {//Ua to catch
if (zh2b5_g.diag) {
cout << "valid sol nuaf5="<<nuaf5<< endl;
}
uint64_t ua = 0, *s0 = &fdsw[0][0].bf.u64;
for (uint32_t i = 0; i < ndigits; i++) {
uint64_t w = sol[i] & ~s0[i];// digit ua
if (!w) {
if (zh2b5_g.diag) cout << "subset assumed rdigit" << i << endl;
return; //a subset exists
}
ua |= w;
}
/* not true can be small gua not a band ua
if (!modevalid) {// not gua mode must be 1n both bands
register uint64_t R = BIT_SET_27;
if (!(ua&R))return;; // ua in band2
R <<= 32;
if (!(ua&R))return; // ua in band1
}
*/
uint64_t cc = (int)_popcnt64(ua);
if (zh2b5_g.diag)cout << "cc=" << cc << endl;
if (cc > sizef5) return;
if (nuaf5 >= 40 && modevalid) {// reasonnable limit for a given step
int limit = tuaf5[39].bf.u64 >> 59;
if (cc >= limit)return; // too many uas found here, skip it
nuaf5 = 39;
}
ua |= cc << 59;
//int ir = genuasb12.AddUA64(&tuaf5[0].bf.u64, nuaf5);
//uint64_t cc = _popcnt64(genuasb12.ua);
//genuasb12.ua.bf.u64 |= cc << 59;
AddUA64(&tuaf5[0].bf.u64, nuaf5,ua);
if (zh2b5_g.diag)cout << Char2Xout(ua) << " ua found end nuaf5=" << nuaf5 << "cc=" << cc << endl;
}
//======================== ZH2B5 2-5 digits
int ZH2B5::Update5() {
if(zh2b5_g.ndigits>5)return 0; // force false if bad use
int Shrink = 1;
register int S, A;
register unsigned int cl, *wcl = FD[0].bf.u32;
while (Shrink) {
Shrink = 0;
if (!rows_unsolved.bf.u32[0])return 1;// solved
{register unsigned int AR = rows_unsolved.bf.u32[0];// valid for digits 0,1,2,3,4
if (!(AR & 077))goto digit1;
//=digit 0
if (FD[0].bf.u32[0] == CompFD[0].bf.u32[0])goto digit0b;
UPDN(0, 0) if ((AR & 7) != S) {
AR &= 07777777770 | S; UPWCL5(0, 2, 4, 6, 8)
}
digit0b:if (FD[0].bf.u32[1] == CompFD[0].bf.u32[1])goto digit1;
UPDN(0, 1) if (((AR >> 3) & 7) != S) {
AR &= 07777777707 | (S << 3); UPWCL5(1, 3, 5, 7, 9)
}
digit1: if (!(AR & 07700))goto digit2;
if (FD[1].bf.u32[0] == CompFD[1].bf.u32[0])goto digit1b;
UPDN(1, 0) if (((AR >> 6) & 7) != S) {
AR &= 07777777077 | (S << 6); UPWCL5(0, 0, 4, 6, 8)
}
digit1b:if (FD[1].bf.u32[1] == CompFD[1].bf.u32[1])goto digit2;
UPDN(1, 1) if (((AR >> 9) & 7) != S) {
AR &= 07777770777 | (S << 9); UPWCL5(1, 1, 5, 7, 9)
}
digit2:
if (zh2b5_g.ndigits < 3) goto end01234;
if (!(AR & 0770000))goto digit3;
if (FD[2].bf.u32[0] == CompFD[2].bf.u32[0])goto digit2b;
UPDN(2, 0) if (((AR >> 12) & 7) != S) {
AR &= 07777707777 | (S << 12); UPWCL5(0, 0, 2, 6, 8)
}
digit2b:if (FD[2].bf.u32[1] == CompFD[2].bf.u32[1])goto digit3;
UPDN(2, 1) if (((AR >> 15) & 7) != S) {
AR &= 07777077777 | (S << 15); UPWCL5(1, 1, 3, 7, 9)
}
digit3:
if (zh2b5_g.ndigits < 4) goto end01234;
if (!(AR & 077000000))goto digit4;
if (FD[3].bf.u32[0] == CompFD[3].bf.u32[0])goto digit3b;
UPDN(3, 0) if (((AR >> 18) & 7) != S) {
AR &= 07770777777 | (S << 18); UPWCL5(0, 0, 2, 4, 8)
}
digit3b: if (FD[3].bf.u32[1] == CompFD[3].bf.u32[1])goto digit4;
UPDN(3, 1)if (((AR >> 21) & 7) != S) {
AR &= 07707777777 | (S << 21); UPWCL5(1, 1, 3, 5, 9)
}
digit4:if (zh2b5_g.ndigits < 5) goto end01234;
if (!(AR & 07700000000))goto end01234;
if (FD[4].bf.u32[0] == CompFD[4].bf.u32[0])goto digit4b;
UPDN(4, 0)if (((AR >> 24) & 7) != S) {
AR &= 07077777777 | (S << 24); UPWCL5(0, 0, 2, 4, 6)
}
digit4b:if (FD[4].bf.u32[1] == CompFD[4].bf.u32[1])goto end01234;
UPDN(4, 1)if (((AR >> 27) & 7) != S) {
AR &= 0777777777 | (S << 27); UPWCL5(1, 1, 3, 5, 7)
}
end01234: rows_unsolved.bf.u32[0] = AR;
}// end of validity for AR 01234
}// end while
return 1;
}
int ZH2B5::FullUpdate5() {
while (1) {
if (!Update5()) return 0; // game locked in update
if (!Unsolved_Count()) return 2;
if (zh2b5_g.ndigits > 3) {
if (ApplySingleOrEmptyCells5()) return 0; // locked
if (zh2b5_g.single_applied) continue;
}
break;
}
return 1;
}
int ZH2B5::ApplySingleOrEmptyCells5() {
#define NAKED5(X) Map=map[X];R2|=R1⤅R1|=Map
zh2b5_g.single_applied = 0;
uint64_t * map = &FD[0].bf.u64;
uint64_t unsolved = cells_unsolved.bf.u64;
register uint64_t R2 = map[0] & map[1],
R1 = (map[0] | map[1]), Map;// digits 12
if (zh2b5_g.ndigits > 2)NAKED5(2);
if (zh2b5_g.ndigits > 3)NAKED5(3);
if (zh2b5_g.ndigits > 4)NAKED5(4);
if (unsolved & (~R1)) return 1; // locked
R1 &= ~R2;
R1 &= unsolved; // forget solved seen as singles
if (!R1) return 0;
zh2b5_g.single_applied = 1;
while (R1) {// usually a very small number of cells to assign
uint32_t res;
if (!bitscanforward64(res, R1)) break;
uint64_t bit = (uint64_t)1 << res; // switch to the bit value
R1 &= ~bit; // clear the bit
// call Seta(int digit, int xcell) so find digit
for (uint32_t idig = 0; idig < zh2b5_g.ndigits; idig++) {
if (map[idig] & bit) {// this is the digit
if (FD[idig].Off(res)) return 1; // invalid, gane locked
Seta5(idig, res);
goto nextr1;// finished for that cell
}
}
return 1; //conflict with a previous cell assugn
nextr1: {}
}
return 0;// not locked
}
int ZH2B5::Seta5(int digit, int xcell) { // single in cell
int cell = From_128_To_81[xcell],
block = TblBoard_Block[cell];
if (FD[digit].Off(xcell)) return 1; // not valid
//Assign(digit, cell, xcell);
BF64 *Fd = &FD[digit];
*Fd &= AssignMask_Digit[cell].u64[0];
int ddig = 6 * digit;
rows_unsolved.Clear(ddig + C_row[cell]);//6*digit + row
cells_unsolved.Clear(xcell);
BF64 * RF = &FD[zh2b5_g.ndigits - 1];//last used digit
for (; RF >= FD; RF--)RF->Clear(xcell);
Fd->Set(xcell); // restore bit for digit assigned
return 0;
}
void ZH2B5::Guess5() {// solve in table digit with lowest count
if (zh2b5_g.diag) {
cout << oct << rows_unsolved.bf.u64 << dec << " unsolved guess5" << endl;
ImageCandidats();
}
if (!rows_unsolved.bf.u64) {// this is a solution
//cout << "valid sol" << endl;
zh2b5_g.ValidSol5(&FD[0].bf.u64);
return;
}
int mincount = 100, digmin,ncount=0;
for (uint32_t idig = 0; idig < zh2b5_g.ndigits; idig++) {
int cc = FD[idig].Count();
if (cc < 7) continue;
ncount++;
if (cc < mincount) {
mincount = cc;
digmin = idig;
}
}
if (zh2b5_g.diag) {
cout << oct << rows_unsolved.bf.u64 << dec << " unsolved guess5 for dig"
<<digmin+1<< endl;
//ImageCandidats();
}
// put in table all digmin valid solutions
BF64 tuaw[100], tsolw[100];
//cout << "call solve 1 digit for digitw=" << digmin+1 << endl;
int nuaw=zh2b1d_g.Go(zh2b5_g.fdsw[0][digmin], FD[digmin], tsolw, tuaw,
(rows_unsolved.bf.u32[0]) >> (6 * digmin) & 077);
rows_unsolved.bf.u32[0] &= ~(077 << (6 * digmin));// clear unsolved
if (zh2b5_g.diag)cout << "return nuaw=" << nuaw << " digmin="<<digmin+1<< endl;
//try each digit full solution if not fully true (subset would exist)
for (int i = 0; i < nuaw; i++) {
ZH2B5 * nextz = this + 1;// open a new zh2b
*nextz = *this;
{// clear the solution for other digits
register uint64_t R = tsolw[i].bf.u64, nR = ~R;
if (zh2b5_g.diag)cout<<Char2Xout(R) << "apply for i=" << i << " digmin=" << digmin+1 << endl;
uint64_t * RF = &nextz->FD[zh2b5_g.ndigits - 1].bf.u64;
for (; RF >= &nextz->FD->bf.u64; RF--)*RF &= nR;
nextz->FD[digmin].bf.u64 = R;// restore the solution for digit
}
// if ncount=2 it is a solution
if (ncount == 2) {
if (zh2b5_g.diag) nextz->ImageCandidats();
zh2b5_g.ValidSol5(&nextz->FD[0].bf.u64);
}
else nextz->ComputeNext5();// continue solving
}
}
int ZH2B5::GetAllDigits(int cell) {
int ir = 0, xcell = C_To128[cell];;
for (uint32_t i = 0; i < zh2b5_g.ndigits; i++) if (FD[i].On(xcell))ir |= (1 << i);
return ir;
}
void ZH2B5::ImageCandidats() {
int dig_cells[81]; for (int i = 0; i < 54; i++) dig_cells[i] = GetAllDigits(i);
int i, j, l, lcol[9], tcol = 0, ncand = 0;
cout << "PM map " << endl << endl;
for (i = 0; i < 9; i++) { // attention ici i indice colonne
lcol[i] = 2; // 2 mini tous chiffres imposés
for (j = 0; j < 6; j++) {
l = _popcnt32(dig_cells[9 * j + i]);
if (l > lcol[i]) lcol[i] = l;
}
tcol += lcol[i];
}
for (i = 0; i < 9; i++) {
if ((i == 3) || (i == 6))cout << "|";
cout << (char)('A' + i) << Blancs(lcol[i], 1);
}
cout << endl;
for (i = 0; i < 6; i++) { // maintenant indice ligne
if ((i == 3) || (i == 6)) {
for (int ix = 0; ix < (tcol + 10); ix++) cout << (char)'-';
cout << endl;
}
for (j = 0; j < 9; j++) {
if ((j == 3) || (j == 6))cout << "|";
int cell = 9 * i + j, digs = dig_cells[cell],
ndigs = _popcnt32(digs);
ncand += ndigs;
for (int id = 0; id < 9; id++)if (digs & (1 << id))
cout << id + 1;
cout << Blancs(lcol[j] + 1 - ndigs, 1);
} // end for j
cout << endl;
} // end for i
cout << endl;
}
//======================== ZH2B1B one digit 2 bands table of solutions
int ZH2B_1D_GLOBAL::Go(BF64 & sol, BF64 & fde, BF64 *tsol, BF64 *tua,int ru) {
tsolw = tsol;
tuaw = tua;
mysol = sol;
nsolw = 0;
myandsol.bf.u64 = BIT_SET_2X;
zh2b1d[0].FD = fde;
zh2b1d[0].CompFD.bf.u64 = 0;
zh2b1d[0].ComputeNext(ru);
return nsolw;
}
int ZH2B_1D::GetSols( int ru) {
CompFD.bf.u64 = 0;
FD = zh2b_g.mystart &zh2b_g.myandsol;
zh2b_g.nsolw = 0;
ComputeNext(ru);
return zh2b_g.nsolw;
}
int ZH2B_1D::GetAllSols(BF64 & fde, int ru, BF64 & fdsol) {
zh2b_g.mystart = FD = fde;
zh2b_g.mysol = fdsol;
zh2b_g.myandsol.bf.u64 = BIT_SET_2X;
zh2b_g.nsolw = 0;
ComputeNext(ru);
return zh2b_g.nsolw;
}
void ZH2B_1D::ComputeNext(int ru) {
if (Update(ru)) {
if (!ru) {// new sol put it in table
if (FD != zh2b1d_g.mysol) {
zh2b1d_g.tuaw[zh2b1d_g.nsolw] = FD- zh2b1d_g.mysol;// partial ua
zh2b1d_g.tsolw[zh2b1d_g.nsolw++] = FD;// partial solution
}
}
else Guess(ru);
}
}
int ZH2B_1D::Update(int &ru) {// solve as much as possible
int Shrink = 1;
register int S, A;
while (Shrink) {
Shrink = 0;
if (!(ru & 7)) goto band2;
A = FD.bf.u32[0];
if (A == CompFD.bf.u32[0]) goto band2;
Shrink = (TblShrinkMask[A & 0x1FF] |
TblShrinkMask[(A >> 9) & 0x1FF] << 3 |
TblShrinkMask[(A >> 18) & 0x1FF] << 6);
if ((A &= TblComplexMask[Shrink]) == 0) return 0;
S = ((A | (A >> 9) | (A >> 18)) & 0x1FF);
FD.bf.u32[1] &= TblMaskSingle[S];
S = TblRowUniq[TblShrinkSingle[Shrink] & TblColumnSingle[S]];
CompFD.bf.u32[0] =
FD.bf.u32[0] = A;
ru = (ru & 070) | S;
band2:;
if (!(ru & 070)) goto exit;
A = FD.bf.u32[1];
if (A == CompFD.bf.u32[1]) goto exit;
Shrink = (TblShrinkMask[A & 0x1FF] |
TblShrinkMask[(A >> 9) & 0x1FF] << 3 |
TblShrinkMask[(A >> 18) & 0x1FF] << 6);
if ((A &= TblComplexMask[Shrink]) == 0) return 0;
S = ((A | (A >> 9) | (A >> 18)) & 0x1FF);
FD.bf.u32[0] &= TblMaskSingle[S];
S = TblRowUniq[TblShrinkSingle[Shrink] & TblColumnSingle[S]];
CompFD.bf.u32[1] =
FD.bf.u32[1] = A;
ru = (ru & 7) | (S<<3);
exit:;
}
//cout << Char2Xout(FD.bf.u64) << " sortie update ru="
// << oct << ru << dec << endl;
return 1;
}
void ZH2B_1D::Guess(int ru) {//
int dcell = 0,v,ruw=ru;
{
register uint32_t a = FD.bf.u32[0], b = FD.bf.u32[1];
if (!(ru & 7)) goto guess_b2;
if ((ru & 070) && (_popcnt32(b) < _popcnt32(a))) goto guess_b2;
// guess in band1
ruw = (-ru) &ru;
v = FD.bf.u32[0] & TblRowUnsolved[ruw];// unknown in last unknown row
goto gogo;
guess_b2: // guess in band2
dcell = 27;
ruw=ru >> 3;
ruw = (-ruw) &ruw;
v = FD.bf.u32[1] & TblRowUnsolved[ruw];// unknown in last unknown row
ruw <<= 3; //relocate bit to the right place
}
gogo:;
uint32_t cell;
ru ^= ruw;// clear the row for next steps
while (bitscanforward(cell, v)) {
v ^= 1 << cell;
cell += dcell;
ZH2B_1D * mynext = this + 1; // start next guess
*mynext = *this;
mynext->Assign(cell);
mynext->ComputeNext(ru);
}
}
int ZH2B_1D::IsValid(uint64_t v) {
CompFD.bf.u64 = 0;
FD.bf.u64 = v;
int ru = 077;
return Update(ru);
}
//=================================== ZHONE
//===================== now ZHone code
ZHONE_GLOBAL::ZHONE_GLOBAL() {
zsol = 0; // no solution unless required buy the user
}
void ZHONE_GLOBAL::GetBand(int * b, uint32_t * t) {
band0 = b; tua = t; nua = 0;
// build sol per digit and pm per digit at start
memset(fd_sols, 0, sizeof fd_sols);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 3; j++) {
int cell = 9 * j + i, dig = b[cell];
fd_sols[1][dig] |= Zhoucol << i; // add candidates in the column
fd_sols[0][dig] |= 1 << cell;
}
}
}
void ZHONE_GLOBAL::GetBand(uint32_t fd_solsb[2][9], int * b, uint32_t * t) {
band0 = b; tua = t; nua = 0;
memcpy(fd_sols, fd_solsb, sizeof fd_sols);
}
void ZHONE_GLOBAL::AddUA(uint32_t ua) {// add if and clear supersets
// ua 27 bit + 5 bit length
if (nua >= 100) return; // size given in STD_B416 go_17_bands