This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
forked from hkociemba/sudokuNxM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pas
4621 lines (4242 loc) · 116 KB
/
main.pas
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
unit main;
{$Define BIG}
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin,
Vcl.Menus, Vcl.ExtCtrls;
const
{$IFDEF BIG}
N_MAXBOXLENGTH = 21; // Values up to 31 possible
N_BIG = (N_MAXBOXLENGTH * N_MAXBOXLENGTH) div 64 + 1;
{$ELSE}
N_MAXBOXLENGTH = 15;
N_BIG = 4;
{$ENDIF}
STRICT_POINTCLAIM = false;
// for pointing/claiming are two elements needed if true;
type
TForm1 = class(TForm)
Memo1: TMemo;
OpenDialog1: TOpenDialog;
PopupMenu1: TPopupMenu;
Paste1: TMenuItem;
GroupBox1: TGroupBox;
Label1: TLabel;
SpinEditRow: TSpinEdit;
Label2: TLabel;
SpinEditCol: TSpinEdit;
BLoad: TButton;
GroupBox2: TGroupBox;
CheckOutlineBoxes: TCheckBox;
BPrintPuzzle: TButton;
GroupBox3: TGroupBox;
CheckVerbose: TCheckBox;
CheckSudokuX: TCheckBox;
CheckSudokuP: TCheckBox;
CheckNC: TCheckBox;
CheckSudokuW: TCheckBox;
BSATSolver: TButton;
BAddSolution: TButton;
BCheckSolution: TButton;
GroupBox4: TGroupBox;
CheckHiddenSingles: TCheckBox;
CheckNakedSingles: TCheckBox;
CheckBlockLineInteraction: TCheckBox;
Splitter1: TSplitter;
CheckHiddenTuple: TCheckBox;
SpinEditMaxHiddenTuple: TSpinEdit;
Label3: TLabel;
CheckNakedTuple: TCheckBox;
SpinEditMaxNakedTuple: TSpinEdit;
Label4: TLabel;
CheckBasicFish: TCheckBox;
SpinEditMaxFish: TSpinEdit;
Label5: TLabel;
Splitter2: TSplitter;
CheckSATSolver: TCheckBox;
GroupBox5: TGroupBox;
GroupBox6: TGroupBox;
RadioOneFold: TRadioButton;
RadioTwoFold: TRadioButton;
RadioFourFold: TRadioButton;
CheckCenterLineHor: TCheckBox;
CheckCenterLineVer: TCheckBox;
CheckDiagonal: TCheckBox;
GroupBox7: TGroupBox;
BReduceBasic: TButton;
BStop: TButton;
BReduceSAT: TButton;
CheckSingleStep: TCheckBox;
GroupBox8: TGroupBox;
BDefault: TButton;
BCreate: TButton;
BLowClueGrid: TButton;
BPermute: TButton;
procedure BAddSolutionClick(Sender: TObject);
procedure BCheckSolutionClick(Sender: TObject);
procedure BCreateClick(Sender: TObject);
procedure BDefaultClick(Sender: TObject);
procedure BLoadClick(Sender: TObject);
procedure BLowClueGridClick(Sender: TObject);
procedure BPermuteClick(Sender: TObject);
procedure BPrintPuzzleClick(Sender: TObject);
procedure BReduceBasicClick(Sender: TObject);
procedure BReduceSATClick(Sender: TObject);
procedure BSATSolverClick(Sender: TObject);
procedure BStopClick(Sender: TObject);
procedure CheckCenterLineHorClick(Sender: TObject);
procedure CheckCenterLineVerClick(Sender: TObject);
procedure CheckDiagonalClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Paste1Click(Sender: TObject);
procedure RbSymClick(Sender: TObject);
procedure SEMaxHiddenTupleChange(Sender: TObject);
procedure SpinEditColChange(Sender: TObject);
procedure SpinEditRowChange(Sender: TObject);
private
nochange: Boolean;
sym: Integer; // demanded symmetry for puzzle enries
public
function PrintCurrentPuzzle: Integer;
end;
TSATThread = class(TThread)
procedure Execute; override;
end;
BigInteger = record
b: array [0 .. N_BIG - 1] of UINT64;
end;
{$IFDEF BIG}
ByteArr = array of Word;
{$ELSE}
ByteArr = array of Byte;
{$ENDIF}
BigIntArr = array of BigInteger;
var
Form1: TForm1;
B_ROW, B_COL, DIM, DIM2, DIM3, DIMWIN: Integer;
// Block size may be different in both directions
rc_n, rn_c, cn_r, bn_k, pn_k: BigIntArr;
empty: BigInteger; // Bitmask for empty entry
// Bitmasks for block-candidates-in-row detection
rowmask, rowmask2, colmask, colmask2: array of BigInteger;
n_set, n_cand_del: Integer; // n_cand_del number of deleted candidates
verbose: Boolean;
simpleCheck, firstLoad, solFound: Boolean;
// simpleCheck: determines if after succesfully applying ame
rc_set: ByteArr; // the numbers set
info: array [0 .. 50] of Integer;
solution: TStringList;
stopComputation, SATKilled, satFailed: Boolean;
curDefString: String;
output, errors: TStringList;
satError: String;
satThread: TSATThread;
function rcQ(r, c, n: Integer): Boolean;
function bk_to_r(box, index: Integer): Integer;
function bk_to_c(box, index: Integer): Integer;
function wk_to_r(win, index: Integer): Integer;
function wk_to_c(win, index: Integer): Integer;
function custom_split(input: string): TArray<string>;
implementation
uses ShellApi, Math, Clipbrd, console, cnf; // TlHelp32
{$R *.dfm}
procedure printFail;
begin
Form1.Memo1.Lines.Add('ERROR: could not start SAT-solver!');
end;
procedure TSATThread.Execute;
var
b: Boolean;
begin
b := GetConsoleOutput('java.exe -server -jar org.sat4j.core.jar cnf.txt',
output, errors);
// GetConsoleOutput
// ('C:\"Program Files (x86)\Java\jre1.8.0_171\bin\java.exe" -server -jar org.sat4j.core.jar cnf.txt',
// output, errors);
if not b then
begin
satFailed := true;
satError := ('ERROR: SAT-solver failed. Result is invald!');
end
else if errors.Count > 0 then
begin
satFailed := true;
satError := errors[0] + '. Result is invalid!';
end
else
satFailed := false;
Terminate;
end;
function KillProcess(PID: DWord): Bool;
var
hProcess: THandle;
begin
hProcess := OpenProcess(PROCESS_TERMINATE, false, PID);
Result := TerminateProcess(hProcess, 0);
end;
procedure permuteArray(var a: array of Integer);
// Fisher-Yates shuffle
var
n, i, j, tmp: Integer;
begin
n := Length(a);
for i := n - 1 downto 1 do
begin
j := Random(i + 1);
tmp := a[i];
a[i] := a[j];
a[j] := tmp;
end;
end;
function shrOneBig(a: BigInteger): BigInteger;
var
m: Integer;
temp: BigInteger;
begin
for m := 0 to N_BIG - 1 do
temp.b[m] := a.b[m];
Result.b[N_BIG - 1] := temp.b[N_BIG - 1] shr 1;
for m := 0 to N_BIG - 2 do
Result.b[m] := temp.b[m] shr 1 or temp.b[m + 1] shl 63;
end;
function NotBig(a: BigInteger): BigInteger;
var
m: Integer;
begin
for m := 0 to N_BIG - 1 do
Result.b[m] := not a.b[m]
end;
function EqualBig(a, c: BigInteger): Boolean;
var
m: Integer;
begin
for m := 0 to N_BIG - 1 do
if a.b[m] <> c.b[m] then
Exit(false);
Result := true;
end;
function AndBig(a, c: BigInteger): BigInteger;
var
m: Integer;
begin
for m := 0 to N_BIG - 1 do
Result.b[m] := a.b[m] and c.b[m]
end;
function OrBig(a, c: BigInteger): BigInteger;
var
m: Integer;
begin
for m := 0 to N_BIG - 1 do
Result.b[m] := a.b[m] or c.b[m]
end;
function notZero(n: BigInteger): Boolean;
var
i: Integer;
begin
for i := 0 to N_BIG - 1 do
if n.b[i] <> 0 then
Exit(true);
Result := false;
end;
function BigZero: BigInteger;
var
i: Integer;
begin
for i := 0 to N_BIG - 1 do
Result.b[i] := UINT64(0);
end;
function bitCount64(num: UINT64): Integer;
asm
POPCNT rax, num
end;
function bitCount(num: BigInteger): Integer;
var
i, s: Integer;
begin
s := 0;
for i := 0 to N_BIG - 1 do
Inc(s, bitCount64(num.b[i]));
Result := s
end;
function bitPos_low64(num: UINT64): Integer;
asm
bsf rax, num
end;
function bitPos_low(num: BigInteger): Integer;
var
i: Integer;
begin
i := 0;
while num.b[i] = 0 do
Inc(i);
Result := bitPos_low64(num.b[i]) + 64 * i;
end;
function bitPos_high64(num: UINT64): Integer;
asm
bsr rax, num
end;
function bitPos_high(num: BigInteger): Integer;
var
i: Integer;
begin
i := N_BIG - 1;
while num.b[i] = 0 do
Dec(i);
Result := bitPos_high64(num.b[i]) + 64 * i;
end;
procedure init_bitmasks;
var
i, j, base, offset: Integer;
begin
empty := BigZero;
// for i := 0 to N_BIG - 1 do
// empty.b[i] := 0;
for i := 0 to DIM - 1 do
begin
base := i div 64;
offset := i mod 64;
empty.b[base] := empty.b[base] or UINT64(1) shl offset;
end;
for i := 0 to N_BIG - 1 do
rowmask[0].b[i] := 0;
for i := 0 to B_COL - 1 do
begin
base := i div 64;
offset := i mod 64;
rowmask[0].b[base] := rowmask[0].b[base] or UINT64(1) shl offset;
end;
rowmask2[0] := BigZero;
for i := 0 to B_COL - 1 do
begin
base := i * B_ROW div 64;
offset := i * B_ROW mod 64;
rowmask2[0].b[base] := rowmask2[0].b[base] or UINT64(1) shl offset;
end;
colmask[0] := BigZero;
for i := 0 to B_ROW - 1 do
begin
base := i div 64;
offset := i mod 64;
colmask[0].b[base] := colmask[0].b[base] or UINT64(1) shl offset;
end;
colmask2[0] := BigZero;
for i := 0 to B_ROW - 1 do
begin
base := i * B_COL div 64;
offset := i * B_COL mod 64;
colmask2[0].b[base] := colmask2[0].b[base] or UINT64(1) shl offset;
end;
for i := 1 to B_ROW - 1 do
begin
rowmask[i].b[0] := rowmask[i - 1].b[0] shl B_COL;
for j := 1 to N_BIG - 1 do
begin
rowmask[i].b[j] := rowmask[i - 1].b[j] shl B_COL or
rowmask[i - 1].b[j - 1] shr (64 - B_COL);
end;
end;
for i := 1 to B_ROW - 1 do
begin
rowmask2[i].b[0] := rowmask2[i - 1].b[0] shl 1;
for j := 1 to N_BIG - 1 do
begin
rowmask2[i].b[j] := rowmask2[i - 1].b[j] shl 1 or
rowmask2[i - 1].b[j - 1] shr 63;
end;
end;
for i := 1 to B_COL - 1 do
begin
colmask[i].b[0] := colmask[i - 1].b[0] shl B_ROW;
for j := 1 to N_BIG - 1 do
begin
colmask[i].b[j] := colmask[i - 1].b[j] shl B_ROW or
colmask[i - 1].b[j - 1] shr (64 - B_ROW);
end;
end;
for i := 1 to B_COL - 1 do
begin
colmask2[i].b[0] := colmask2[i - 1].b[0] shl 1;
for j := 1 to N_BIG - 1 do
begin
colmask2[i].b[j] := colmask2[i - 1].b[j] shl 1 or
colmask2[i - 1].b[j - 1] shr 63;
end;
end;
end;
// clear number n in numbervector of row r and column c
procedure clear_rc(r, c, n: Integer);
var
b, base, offset: Integer;
begin
b := DIM * r + c;
base := n div 64;
offset := n mod 64;
rc_n[b].b[base] := rc_n[b].b[base] and not(UINT64(1) shl offset);
end;
procedure set_rc(r, c, n: Integer);
var
b, base, offset: Integer;
begin
b := DIM * r + c;
base := n div 64;
offset := n mod 64;
rc_n[b].b[base] := rc_n[b].b[base] or (UINT64(1) shl offset);
end;
function rcQ(r, c, n: Integer): Boolean;
var
base, offset: Integer;
begin
base := n div 64;
offset := n mod 64;
if rc_n[DIM * r + c].b[base] and (UINT64(1) shl offset) <> 0 then
Result := true
else
Result := false;
end;
// returns row for cell with index in box
function bk_to_r(box, index: Integer): Integer;
begin
Result := B_ROW * (box div B_ROW) + index div B_COL
end;
// returns column for cell with index in box
function bk_to_c(box, index: Integer): Integer;
begin
Result := B_COL * (box mod B_ROW) + index mod B_COL
end;
// returns row for cell with index in pset
function pk_to_r(pset, index: Integer): Integer;
begin
Result := B_ROW * (index div B_ROW) + pset div B_COL
end;
// returns column for cell with index in pset
function pk_to_c(pset, index: Integer): Integer;
begin
Result := B_COL * (index mod B_ROW) + pset mod B_COL
end;
// returns row for cell with index in window
function wk_to_r(win, index: Integer): Integer;
begin
Result := (B_ROW + 1) * (win div (B_ROW - 1)) + 1 + index div B_ROW
end;
// returns column for cell with index in window
function wk_to_c(win, index: Integer): Integer;
begin
Result := (B_ROW + 1) * (win mod (B_ROW - 1)) + 1 + index mod B_ROW
end;
// clear column c in columnvector of row r and number n
procedure clear_rn(r, c, n: Integer);
var
bse, base, offset: Integer;
begin
bse := DIM * r + n;
base := c div 64;
offset := c mod 64;
rn_c[bse].b[base] := rn_c[bse].b[base] and not(UINT64(1) shl offset);
end;
procedure set_rn(r, c, n: Integer);
var
bse, base, offset: Integer;
begin
bse := DIM * r + n;
base := c div 64;
offset := c mod 64;
rn_c[bse].b[base] := rn_c[bse].b[base] or (UINT64(1) shl offset);
end;
// clear row r in rowvector of column c and number n
procedure clear_cn(r, c, n: Integer);
var
bse, base, offset: Integer;
begin
bse := DIM * c + n;
base := r div 64;
offset := r mod 64;
cn_r[bse].b[base] := cn_r[bse].b[base] and not(UINT64(1) shl offset);
end;
procedure set_cn(r, c, n: Integer);
var
bse, base, offset: Integer;
begin
bse := DIM * c + n;
base := r div 64;
offset := r mod 64;
cn_r[bse].b[base] := cn_r[bse].b[base] or (UINT64(1) shl offset);
end;
// clear blockposition k in blockvector of block b and number n
// b and k are computed from r and c
procedure clear_bn(r, c, n: Integer);
var
bse, b, k, base, offset: Integer;
begin
b := B_ROW * (r div B_ROW) + c div B_COL; // block index
bse := DIM * b + n;
k := B_COL * (r mod B_ROW) + c mod B_COL; // cell index in block
base := k div 64;
offset := k mod 64;
bn_k[bse].b[base] := bn_k[bse].b[base] and not(UINT64(1) shl offset);
end;
procedure set_bn(r, c, n: Integer);
var
bse, b, k, base, offset: Integer;
begin
b := B_ROW * (r div B_ROW) + c div B_COL; // block index
bse := DIM * b + n;
k := B_COL * (r mod B_ROW) + c mod B_COL; // cell index in block
base := k div 64;
offset := k mod 64;
bn_k[bse].b[base] := bn_k[bse].b[base] or (UINT64(1) shl offset);
end;
// clear position k in psetvector of pset p and number n
// b and k are computed from r and c
procedure clear_pn(r, c, n: Integer);
var
bse, p, k, base, offset: Integer;
begin
p := B_COL * (r mod B_ROW) + c mod B_COL; // pset index
bse := DIM * p + n;
k := B_ROW * (r div B_ROW) + c div B_COL; // cell index in pset
base := k div 64;
offset := k mod 64;
pn_k[bse].b[base] := pn_k[bse].b[base] and not(UINT64(1) shl offset);
end;
procedure set_pn(r, c, n: Integer);
var
bse, p, k, base, offset: Integer;
begin
p := B_COL * (r mod B_ROW) + c mod B_COL; // pset index
bse := DIM * p + n;
k := B_ROW * (r div B_ROW) + c div B_COL; // cell index in pset
base := k div 64;
offset := k mod 64;
pn_k[bse].b[base] := pn_k[bse].b[base] or (UINT64(1) shl offset);
end;
procedure deleteCandidate(r, c, n: Integer);
var
bse, base, offset: Integer;
begin
bse := DIM * r + c;
base := n div 64;
offset := n mod 64;
if rc_n[bse].b[base] <> rc_n[bse].b[base] and not(UINT64(1) shl offset) then
// not cleared yet
begin
Inc(n_cand_del);
clear_rc(r, c, n);
clear_rn(r, c, n);
clear_cn(r, c, n);
clear_bn(r, c, n);
if Form1.CheckSudokuP.Checked then
clear_pn(r, c, n);
end;
end;
// this procedure is only usefull if you have used isCandidate before
procedure addCandidate(r, c, n: Integer);
var
bse, base, offset: Integer;
begin
bse := DIM * r + c;
base := n div 64;
offset := n mod 64;
if rc_n[bse].b[base] <> rc_n[bse].b[base] or (UINT64(1) shl offset) then
// yet clear
begin
Dec(n_cand_del);
set_rc(r, c, n);
set_rn(r, c, n);
set_cn(r, c, n);
set_bn(r, c, n);
if Form1.CheckSudokuP.Checked then
set_pn(r, c, n);
end;
end;
function isCandidate(r, c, n: Integer): Boolean;
var
i, b, p: Integer;
begin
if rc_set[DIM * r + c] <> 0 then // cell ocupied-> no candidates any more
Exit(false);
// check if number n is anywhere in column
for i := 0 to DIM - 1 do
if rc_set[DIM * i + c] = n + 1 then
Exit(false);
// check if number n is anywhere in row
for i := 0 to DIM - 1 do
if rc_set[DIM * r + i] = n + 1 then
Exit(false);
// check if number n is anywhere in block
b := B_ROW * (r div B_ROW) + c div B_COL; // block index
for i := 0 to DIM - 1 do
if rc_set[DIM * bk_to_r(b, i) + bk_to_c(b, i)] = n + 1 then
Exit(false);
if Form1.CheckSudokuP.Checked then
begin
p := B_COL * (r mod B_ROW) + c mod B_COL; // pset index;
for i := 0 to DIM - 1 do
if rc_set[DIM * pk_to_r(p, i) + pk_to_c(p, i)] = n + 1 then
Exit(false);
end;
Result := true;
end;
procedure setNumber(r, c, n: Integer);
var
j, b, p, w: Integer;
begin
Inc(n_set);
b := B_ROW * (r div B_ROW) + c div B_COL;
for j := 0 to DIM - 1 do
begin
deleteCandidate(r, c, j);
deleteCandidate(r, j, n);
deleteCandidate(j, c, n);
deleteCandidate(bk_to_r(b, j), bk_to_c(b, j), n);
end;
if Form1.CheckSudokuP.Checked then
begin
p := B_COL * (r mod B_ROW) + c mod B_COL; // pset index;
for j := 0 to DIM - 1 do
deleteCandidate(pk_to_r(p, j), pk_to_c(p, j), n);
end;
if Form1.CheckSudokuX.Checked then
begin
if r = c then // set on diagonal
for j := 0 to DIM - 1 do
deleteCandidate(j, j, n);
if r = DIM - 1 - c then // set on antidiagonal
for j := 0 to DIM - 1 do
deleteCandidate(j, DIM - 1 - j, n);
end;
if Form1.CheckSudokuW.Checked then
begin
if (r mod (B_ROW + 1) <> 0) and (c mod (B_ROW + 1) <> 0) then
begin // number is inside a window
w := (B_ROW - 1) * (r div (B_ROW + 1)) + c div (B_ROW + 1);
// window index;
for j := 0 to DIM - 1 do
deleteCandidate(wk_to_r(w, j), wk_to_c(w, j), n);
end;
end;
if Form1.CheckNC.Checked then
begin
if r <> 0 then
begin
deleteCandidate(r - 1, c, (n - 1 + DIM) mod DIM);
deleteCandidate(r - 1, c, (n + 1) mod DIM);
end;
if r <> DIM - 1 then
begin
deleteCandidate(r + 1, c, (n - 1 + DIM) mod DIM);
deleteCandidate(r + 1, c, (n + 1) mod DIM);
end;
if c <> 0 then
begin
deleteCandidate(r, c - 1, (n - 1 + DIM) mod DIM);
deleteCandidate(r, c - 1, (n + 1) mod DIM);
end;
if c <> DIM - 1 then
begin
deleteCandidate(r, c + 1, (n - 1 + DIM) mod DIM);
deleteCandidate(r, c + 1, (n + 1) mod DIM);
end;
end;
rc_set[DIM * r + c] := n + 1;
end;
procedure deleteNumber(r, c, n: Integer);
var
j, b, p: Integer;
begin
Dec(n_set);
b := B_ROW * (r div B_ROW) + c div B_COL;
for j := 0 to DIM - 1 do
begin
if isCandidate(r, c, j) then
addCandidate(r, c, j);
if isCandidate(r, j, n) then
addCandidate(r, j, n);
if isCandidate(j, c, n) then
addCandidate(j, c, n);
if isCandidate(bk_to_r(b, j), bk_to_c(b, j), n) then
addCandidate(bk_to_r(b, j), bk_to_c(b, j), n);
end;
if Form1.CheckSudokuP.Checked then
begin
p := B_COL * (r mod B_ROW) + c mod B_COL; // pset index;
for j := 0 to DIM - 1 do
if isCandidate(pk_to_r(p, j), pk_to_c(p, j), n) then
addCandidate(pk_to_r(p, j), pk_to_c(p, j), n);
end;
if Form1.CheckSudokuX.Checked then
begin
if r = c then // set on diagonal
for j := 0 to DIM - 1 do
if isCandidate(j, j, n) then
addCandidate(j, j, n);
if r = DIM - 1 - c then // set on antidiagonal
for j := 0 to DIM - 1 do
if isCandidate(j, DIM - 1 - j, n) then
addCandidate(j, DIM - 1 - j, n);
end;
end;
function custom_split(input: string): TArray<string>;
var
delimiterSet: array [0 .. 1] of char;
// split works with char array, not a single char
begin
delimiterSet[0] := ' '; // some character
delimiterSet[1] := '|';
Result := input.Split(delimiterSet);
end;
// The givens have to be specified in rc_set
procedure initBitArraysFromGivens;
var
i, r, c: Integer;
begin
n_set := 0;
n_cand_del := 0;
for i := 0 to DIM2 - 1 do
begin
rc_n[i] := empty;
rn_c[i] := empty;
cn_r[i] := empty;
bn_k[i] := empty;
pn_k[i] := empty;
end;
for r := 0 to DIM - 1 do
for c := 0 to DIM - 1 do
if rc_set[DIM * r + c] > 0 then
begin
setNumber(r, c, rc_set[DIM * r + c] - 1);
end;
end;
// String s contains the givens of the sudoku. Entries have to be separated by
// spaces, an empty entry is given by '0' or '.'
procedure initBitArraysFromString(s: String);
var
i, r, c, n, idx: Integer;
data: TArray<string>;
t: String;
begin
n_set := 0;
n_cand_del := 0;
for i := 0 to DIM2 - 1 do
begin
rc_n[i] := empty;
rn_c[i] := empty;
cn_r[i] := empty;
bn_k[i] := empty;
pn_k[i] := empty;
rc_set[i] := 0;
end;
s := s.ToUpper;
t := '';
for i := 1 to Length(s) do
begin
if s[i] <> '.' then
t := t + s[i]
else
t := t + ' . '
end;
s := t;
data := custom_split(s);
n := Length(data);
idx := 0;
for i := 0 to n - 1 do
if data[i] <> '' then
begin
data[idx] := data[i];
Inc(idx);
end;
// idx sollte jetzt SZ4 sein
for i := 0 to DIM2 - 1 do
begin
r := i div DIM;
c := i mod DIM;
if (data[i] = '.') or (data[i] = '0') then
continue;
n := StrToInt(data[i]) - 1;
setNumber(r, c, n);
end;
end;
function solutionValid: Boolean;
var
b, r, c, i: Integer;
t: array of Integer;
begin
// for i := 0 to DIM-1 do
// t[i]:=0;
for r := 0 to DIM - 1 do
begin
SetLength(t, DIM); // zero initialization!
for c := 0 to DIM - 1 do
begin
if rc_set[DIM * r + c] = 0 then
Exit(false); // not all cells filled
Inc(t[rc_set[DIM * r + c] - 1]);
end;
for i := 0 to DIM - 1 do
if t[i] <> 1 then
Exit(false);
SetLength(t, 0);
end;
for c := 0 to DIM - 1 do
begin
SetLength(t, DIM);
for r := 0 to DIM - 1 do
Inc(t[rc_set[DIM * r + c] - 1]);
for i := 0 to DIM - 1 do
if t[i] <> 1 then
Exit(false);
SetLength(t, 0);
end;
for b := 0 to DIM - 1 do
begin
SetLength(t, DIM);
for i := 0 to DIM - 1 do
begin
r := bk_to_r(b, i);
c := bk_to_c(b, i);
Inc(t[rc_set[DIM * r + c] - 1]);
end;
for i := 0 to DIM - 1 do
if t[i] <> 1 then
Exit(false);
SetLength(t, 0);
if Form1.CheckSudokuX.Checked then
begin
SetLength(t, DIM);
for r := 0 to DIM - 1 do
Inc(t[rc_set[DIM * r + r] - 1]);
for i := 0 to DIM - 1 do
if t[i] <> 1 then
Exit(false);
SetLength(t, 0);
SetLength(t, DIM);
for r := 0 to DIM - 1 do
Inc(t[rc_set[DIM * (DIM - r - 1) + r] - 1]);
for i := 0 to DIM - 1 do
if t[i] <> 1 then
Exit(false);
SetLength(t, 0);
end;
end;
Result := true;
end;
procedure hidden_single_pset;
var
i, p, k: Integer;
begin
for i := 0 to DIM2 - 1 do
if bitCount(pn_k[i]) = 1 then
begin
p := i div DIM; // pset number
k := bitPos_low(pn_k[i]); // index of cell in block
if verbose then
Form1.Memo1.Lines.Add('hidden single in pset ' + IntToStr(p + 1) + ': r'
+ IntToStr(pk_to_r(p, k) + 1) + 'c' + IntToStr(pk_to_c(p, k) + 1) +
' = ' + IntToStr(i mod DIM + 1));
setNumber(pk_to_r(p, k), pk_to_c(p, k), i mod DIM);
if simpleCheck then
Exit; // try simpler methods
end;
end;
procedure hidden_single_block;
var
i, b, k: Integer;
begin
for i := 0 to DIM2 - 1 do
if bitCount(bn_k[i]) = 1 then
begin
b := i div DIM; // block number
k := bitPos_low(bn_k[i]); // index of cell in block
if verbose then
Form1.Memo1.Lines.Add('hidden single in box ' + IntToStr(b + 1) + ': r'
+ IntToStr(bk_to_r(b, k) + 1) + 'c' + IntToStr(bk_to_c(b, k) + 1) +
' = ' + IntToStr(i mod DIM + 1));
setNumber(bk_to_r(b, k), bk_to_c(b, k), i mod DIM);
// if simpleCheck then //this is unnecessary
// Exit; // retry from start
end;
end;
procedure hidden_single_row;
var
i: Integer;
begin
for i := 0 to DIM2 - 1 do
if bitCount(rn_c[i]) = 1 then
begin
if verbose then
Form1.Memo1.Lines.Add('hidden single in row: r' +
IntToStr(i div DIM + 1) + 'c' + IntToStr(bitPos_low(rn_c[i]) + 1) +
' = ' + IntToStr(i mod DIM + 1));
setNumber(i div DIM, bitPos_low(rn_c[i]), i mod DIM);
if simpleCheck then
Exit; // try simpler methods
end;
end;
procedure hidden_single_col;
var
i: Integer;
begin
for i := 0 to DIM2 - 1 do
if bitCount(cn_r[i]) = 1 then
begin
if verbose then
Form1.Memo1.Lines.Add('hidden single in column ' +
IntToStr(i div DIM + 1) + ': r' + IntToStr(bitPos_low(cn_r[i]) + 1) +
'c' + IntToStr(i div DIM + 1) + ' = ' + IntToStr(i mod DIM + 1));
setNumber(bitPos_low(cn_r[i]), i div DIM, i mod DIM);
if simpleCheck then
Exit; // try simpler methods
end;
end;
procedure naked_single;
var
i: Integer;
begin
for i := 0 to DIM2 - 1 do
if bitCount(rc_n[i]) = 1 then
begin