-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuman.jl
More file actions
2272 lines (1894 loc) · 69 KB
/
Copy pathhuman.jl
File metadata and controls
2272 lines (1894 loc) · 69 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
"""
Human-like Str8ts Solver
Solves Str8ts puzzles using human reasoning strategies, without guessing.
Based on the strategies described in str8ts-strategies.txt.
(c) Mia Muessig
"""
using Combinatorics
using MatrixNetworks
using SparseArrays
@isdefined(Str8ts) || include("structs.jl")
# ============================================================================
# HARDNESS SCALE (0-100)
# All strategy functions return 0 (no progress) or a hardness value > 0.
# Strategies with size-dependent complexity return higher values for larger sizes.
# ============================================================================
const H_SINGLE = 3 # Naked / hidden single
const H_SURE_CANDIDATES = 5 # Sure candidates cross-compartment elimination
stranded_hardness(k::Int) = 10 + (k - 1) * 2 # stranded digits
split_hardness(k::Int) = 15 + (k - 1) * 2 # split compartments (k≥3)
mindgap_hardness(k::Int) = 15 + (k - 1) * 2 # mind the gap
range_check_hardness(k::Int) = 20 + (k - 1) * 3 # bipartite range check
# Naked sets: setSize 2→35, 3→40, 4→45, 5→50
naked_set_hardness(size::Int) = 20 + (size - 1) * 5
# Hidden sets: setSize 2→37, 3→42, 4→47, 5→52
hidden_set_hardness(size::Int) = 25 + (size - 1) * 5
const H_LOCKED = 30 # Locked compartments
# Sea creatures: n=2→60 (X-Wing), 3→65, 4→70, 5→75
sea_creature_hardness(n::Int) = 50 + n * 5
const H_UNIQUE = 60 # Unique solution constraint
const H_SETTI = 70 # Setti's rule
const H_SETTI_CONSIDER = 75 # Setti considerations
const H_SETTI_SET = 75 # Combined Settis
const H_YWING = 80 # Y-Wing
const H_BINARY_GUESS = 90 # Binary contradiction guess (single level)
const H_UNSOLVABLE = 100 # Could not solve with available strategies
"""
Show candidates for a specific cell
"""
function showCandidates(s::Str8ts, r::Int, c::Int)
println("Candidates at ($r, $c): ", collect(s.candidates[r, c]))
end
"""
add!(s, r, c, num)
Set the tile at position (r, c) to num and update candidates.
"""
function add!(s::Str8ts, r::Int, c::Int, num::Int)
if s.solved[r, c]
return
end
s.solved[r, c] = true
s.numbers[r, c] = num
s.candidates[r, c] = BitSet(num)
propagateAdd!(s, r, c, num)
end
"""
propagateAdd!(s, r, c, num)
Propagate constraints after adding num at (r, c).
"""
function propagateAdd!(s::Str8ts, r::Int, c::Int, num::Int)
# Remove num from all other cells in row
for cc in 1:9
if cc != c && !s.isBlack[r, cc]
remCandidate!(s, r, cc, num)
end
end
# Remove num from all other cells in column
for rr in 1:9
if rr != r && !s.isBlack[rr, c]
remCandidate!(s, rr, c, num)
end
end
# Remove all other candidates from (r, c)'s occurrence sets
for n in 1:9
if n != num
delete!(s.occRow[r, n], (r, c))
delete!(s.occCol[c, n], (r, c))
end
end
end
"""
remCandidate!(s, r, c, num)
Remove num as a candidate from cell (r, c).
"""
function remCandidate!(s::Str8ts, r::Int, c::Int, num::Int)
if num in s.candidates[r, c]
delete!(s.candidates[r, c], num)
delete!(s.occRow[r, num], (r, c))
delete!(s.occCol[c, num], (r, c))
end
end
"""
getCompartmentCandidates(s, comp)
Get all candidates that appear in any cell of the compartment.
"""
function getCompartmentCandidates(s::Str8ts, comp::Compartment)
cands = BitSet()
for (r, c) in comp
union!(cands, s.candidates[r, c])
end
return cands
end
"""
getCompartmentSolvedValues(s, comp)
Get all values that are already solved/placed in the compartment.
"""
function getCompartmentSolvedValues(s::Str8ts, comp::Compartment)
solved = BitSet()
for (r, c) in comp
if s.solved[r, c]
push!(solved, s.numbers[r, c])
end
end
return solved
end
"""
getCompartmentRanges(s, comp)
Get all possible ranges for a compartment, accounting for both candidates and solved values.
"""
function getCompartmentRanges(s::Str8ts, comp::Compartment)
cands = getCompartmentCandidates(s, comp)
solved = getCompartmentSolvedValues(s, comp)
return getPossibleRanges(cands, length(comp), required=solved)
end
"""
getPossibleRanges(candidates, size; required=BitSet())
Get all possible ranges (consecutive sequences) of given size that can be formed
from the candidates. If `required` is provided, ranges must contain all required values.
The `required` parameter should contain values that are already placed in the compartment.
"""
function getPossibleRanges(candidates::BitSet, size::Int; required::BitSet=BitSet())
ranges = Vector{UnitRange{Int}}()
if size == 0
return ranges
end
for start in 1:(10-size)
rng = start:(start+size-1)
# Check if range is possible (all numbers exist in candidates)
if all(n in candidates for n in rng)
# Also check that all required values are within the range
if all(n in rng for n in required)
push!(ranges, rng)
end
end
end
return ranges
end
"""
getSureCandidates(ranges, size)
Get sure candidates: numbers that appear in ALL possible ranges.
"""
function getSureCandidates(ranges::Vector{UnitRange{Int}}, size::Int)
if isempty(ranges)
return BitSet()
end
# Intersection of all ranges
sure = BitSet(first(ranges))
for rng in ranges[2:end]
intersect!(sure, BitSet(rng))
end
return sure
end
"""
isValid(s)
Check if the Str8ts is still solvable (no cell has empty candidates).
"""
function isValid(s::Str8ts)
for r in 1:9
for c in 1:9
if !s.isBlack[r, c] && isempty(s.candidates[r, c])
return false
end
end
end
return true
end
"""
isDone(s)
Check if the Str8ts is completely solved.
"""
function isDone(s::Str8ts)
for r in 1:9
for c in 1:9
if !s.isBlack[r, c] && !s.solved[r, c]
return false
end
end
end
return true
end
# ============================================================================
# STRATEGIES
# ============================================================================
"""
useSingle(s)
Find and apply naked or hidden singles.
- Naked single: cell has only one candidate
- Hidden single: a sure candidate appears in only one cell of a compartment
"""
function useSingle(s::Str8ts)
# Naked singles
for r in 1:9
for c in 1:9
if !s.isBlack[r, c] && !s.solved[r, c] && length(s.candidates[r, c]) == 1
add!(s, r, c, first(s.candidates[r, c]))
return H_SINGLE
end
end
end
# Hidden singles in row compartments
for comp in s.rowCompartments
ranges = getCompartmentRanges(s, comp)
sure = getSureCandidates(ranges, length(comp))
for n in sure
cells_with_n = [(r, c) for (r, c) in comp if n in s.candidates[r, c]]
if length(cells_with_n) == 1
(r, c) = cells_with_n[1]
if !s.solved[r, c]
add!(s, r, c, n)
return H_SINGLE
end
end
end
end
# Hidden singles in column compartments
for comp in s.colCompartments
ranges = getCompartmentRanges(s, comp)
sure = getSureCandidates(ranges, length(comp))
for n in sure
cells_with_n = [(r, c) for (r, c) in comp if n in s.candidates[r, c]]
if length(cells_with_n) == 1
(r, c) = cells_with_n[1]
if !s.solved[r, c]
add!(s, r, c, n)
return H_SINGLE
end
end
end
end
return 0
end
"""
hasPerfectMatching(cellCandidates, rangeValues)
Check if there exists a perfect matching between cells and values.
cellCandidates: Vector of BitSets representing candidate values for each cell
rangeValues: Vector of values that must be assigned
Returns true if a perfect matching exists.
"""
function hasPerfectMatching(cellCandidates::Vector{BitSet}, rangeValues::Vector{Int})
n = length(cellCandidates)
if n != length(rangeValues)
return false
end
if n == 0
return true
end
# Build bipartite graph adjacency matrix
# Rows: cells, Cols: values
# A[i,j] = 1 if cell i can have value rangeValues[j]
rows = Int[]
cols = Int[]
for i in 1:n
for j in 1:n
if rangeValues[j] in cellCandidates[i]
push!(rows, i)
push!(cols, j)
end
end
end
if isempty(rows)
return false
end
vals = ones(Int, length(rows))
A = sparse(rows, cols, vals, n, n)
# Find maximum matching
result = bipartite_matching(A)
# Check if it's a perfect matching
return result.cardinality == n
end
"""
useCompartmentRangeCheck(s, compSize)
Remove candidates that cannot be part of any valid assignment using bipartite matching.
Only processes compartments of exactly `compSize` cells.
Returns `range_check_hardness(compSize)` if progress was made, else 0.
"""
function useCompartmentRangeCheck(s::Str8ts, compSize::Int)
effective = false
for comp in vcat(s.rowCompartments, s.colCompartments)
size = length(comp)
if size != compSize
continue
end
ranges = getCompartmentRanges(s, comp)
if isempty(ranges)
continue
end
# Get unsolved cells
unsolved = [(r, c) for (r, c) in comp if !s.solved[r, c]]
n_unsolved = length(unsolved)
if n_unsolved == 0
continue
end
# For each unsolved cell, collect valid values across all ranges
validAssignments = [BitSet() for _ in 1:n_unsolved]
for rng in ranges
rangeValues = collect(rng)
# Check if solved cells are compatible with this range
compatibleWithSolved = true
solvedValues = Int[]
for (r, c) in comp
if s.solved[r, c]
if !(s.numbers[r, c] in rng)
compatibleWithSolved = false
break
end
push!(solvedValues, s.numbers[r, c])
end
end
if !compatibleWithSolved
continue
end
# Remove solved values from available values
availableValues = setdiff(rangeValues, solvedValues)
if length(availableValues) != n_unsolved
continue
end
# Get current candidates for unsolved cells
cellCandidates = [s.candidates[r, c] for (r, c) in unsolved]
# First check if this range allows any perfect matching at all
if !hasPerfectMatching(cellCandidates, collect(availableValues))
continue
end
# For each cell, test which values can be part of a valid matching
for i in 1:n_unsolved
for val in availableValues
if !(val in cellCandidates[i])
continue
end
# Test if assigning val to cell i allows a perfect matching
# Create restricted candidates where cell i can only have val
testCandidates = [i == j ? BitSet([val]) : cellCandidates[j] for j in 1:n_unsolved]
if hasPerfectMatching(testCandidates, collect(availableValues))
push!(validAssignments[i], val)
end
end
end
end
# Remove candidates that are not in validAssignments
for i in 1:n_unsolved
(r, c) = unsolved[i]
for n in collect(s.candidates[r, c])
if !(n in validAssignments[i])
remCandidate!(s, r, c, n)
effective = true
end
end
end
end
return effective ? range_check_hardness(compSize) : 0
end
"""
useStrandedDigits(s, compSize)
Remove stranded digits - candidates that cannot be part of any valid sequence.
Only processes compartments of exactly `compSize` cells.
Returns `stranded_hardness(compSize)` if progress was made, else 0.
"""
function useStrandedDigits(s::Str8ts, compSize::Int)
effective = false
for comp in vcat(s.rowCompartments, s.colCompartments)
size = length(comp)
if size != compSize
continue
end
cands = getCompartmentCandidates(s, comp)
solved = getCompartmentSolvedValues(s, comp)
# For each candidate, check if it can be part of a valid range
for n in collect(cands)
# Find all ranges containing n
canBePartOfRange = false
for start in max(1, n - size + 1):min(n, 10 - size)
rng = start:(start + size - 1)
# Check if range is possible: all range numbers are candidates AND
# all solved values are within the range
if all(m in cands for m in rng) && all(m in rng for m in solved)
canBePartOfRange = true
break
end
end
if !canBePartOfRange
# Remove n from all cells in compartment
for (r, c) in comp
if n in s.candidates[r, c]
remCandidate!(s, r, c, n)
effective = true
end
end
end
end
# Special case: bridging digits for size-2 compartments
# If a cell has candidate n, and the only adjacent candidate (n-1 or n+1)
# is also only in this cell, then n is stranded for this cell
if size == 2
cands = getCompartmentCandidates(s, comp) # Refresh
for i in 1:2
(r, c) = comp[i]
(r2, c2) = comp[3 - i] # The other cell
if s.solved[r, c]
continue
end
otherCands = s.candidates[r2, c2]
for n in collect(s.candidates[r, c])
# For n to be valid, either n-1 or n+1 must be in the other cell
hasAdjacent = (n - 1) in otherCands || (n + 1) in otherCands
if !hasAdjacent
remCandidate!(s, r, c, n)
effective = true
end
end
end
end
end
return effective ? stranded_hardness(compSize) : 0
end
"""
useSplitCompartment(s, compSize)
Handle split compartments - when possible ranges don't overlap.
Only processes compartments of exactly `compSize` cells (compSize ≥ 3).
Returns `split_hardness(compSize)` if progress was made, else 0.
"""
function useSplitCompartment(s::Str8ts, compSize::Int)
effective = false
for comp in vcat(s.rowCompartments, s.colCompartments)
size = length(comp)
if size != compSize
continue
end
ranges = getCompartmentRanges(s, comp)
if length(ranges) <= 1
continue
end
# Group ranges by overlap
groups = Vector{Vector{UnitRange{Int}}}()
for rng in ranges
found = false
for group in groups
if any(overlaps(rng, r) for r in group)
push!(group, rng)
found = true
break
end
end
if !found
push!(groups, [rng])
end
end
# Merge overlapping groups
merged = true
while merged
merged = false
for i in 1:length(groups)
for j in i+1:length(groups)
if any(overlaps(r1, r2) for r1 in groups[i] for r2 in groups[j])
append!(groups[i], groups[j])
deleteat!(groups, j)
merged = true
break
end
end
if merged
break
end
end
end
if length(groups) > 1
# We have a split compartment!
# For each group, analyze independently
for group in groups
groupSure = getSureCandidates(group, size)
# Sure candidates in this group must appear only in cells that could be in this range
groupMin = minimum(first(r) for r in group)
groupMax = maximum(last(r) for r in group)
groupCands = BitSet(groupMin:groupMax)
for n in groupSure
# n is sure in this group - remove from cells that can't be part of this group
for (r, c) in comp
if !s.solved[r, c] && n in s.candidates[r, c]
# Check if this cell could be in this group's range
cellCands = intersect(s.candidates[r, c], groupCands)
if isempty(cellCands)
remCandidate!(s, r, c, n)
effective = true
end
end
end
end
end
end
end
return effective ? split_hardness(compSize) : 0
end
@inline function overlaps(r1::UnitRange{Int}, r2::UnitRange{Int})
return first(r1) <= last(r2) && first(r2) <= last(r1)
end
"""
useMindTheGap(s, compSize)
If a cell has a large gap (distance >= compartment size) between its candidates,
those extreme candidates cannot appear in other cells.
Only processes compartments of exactly `compSize` cells.
Returns `mindgap_hardness(compSize)` if progress was made, else 0.
"""
function useMindTheGap(s::Str8ts, compSize::Int)
effective = false
for comp in vcat(s.rowCompartments, s.colCompartments)
size = length(comp)
if size != compSize
continue
end
for (r, c) in comp
if s.solved[r, c]
continue
end
cands = collect(s.candidates[r, c])
if length(cands) < 2
continue
end
sort!(cands)
minCand = first(cands)
maxCand = last(cands)
gap = maxCand - minCand
if gap >= size
# Large-gap eliminations (conservative):
# 1) If exactly two candidates (min,max), remove both from other cells.
# 2) If exactly three candidates and only one side is singleton,
# remove only that singleton side candidate from other cells.
lowSideCands = filter(n -> n <= minCand + (gap - size), cands)
highSideCands = filter(n -> n >= maxCand - (gap - size), cands)
if length(cands) == 2 && length(lowSideCands) == 1 && length(highSideCands) == 1
lowForced = lowSideCands[1]
highForced = highSideCands[1]
for (r2, c2) in comp
if (r2, c2) != (r, c) && !s.solved[r2, c2]
if lowForced in s.candidates[r2, c2]
remCandidate!(s, r2, c2, lowForced)
effective = true
end
if highForced in s.candidates[r2, c2]
remCandidate!(s, r2, c2, highForced)
effective = true
end
end
end
elseif length(cands) == 3
if length(lowSideCands) == 1 && length(highSideCands) > 1 && lowSideCands[1] == minCand
lowForced = lowSideCands[1]
for (r2, c2) in comp
if (r2, c2) != (r, c) && !s.solved[r2, c2] && lowForced in s.candidates[r2, c2]
remCandidate!(s, r2, c2, lowForced)
effective = true
end
end
elseif length(highSideCands) == 1 && length(lowSideCands) > 1 && highSideCands[1] == maxCand
highForced = highSideCands[1]
for (r2, c2) in comp
if (r2, c2) != (r, c) && !s.solved[r2, c2] && highForced in s.candidates[r2, c2]
remCandidate!(s, r2, c2, highForced)
effective = true
end
end
end
end
end
end
# Check for gaps spanning two cells - but only when both cells have exactly 2 candidates
# with a shared bridge
for i in 1:length(comp)
for j in i+1:length(comp)
(r1, c1) = comp[i]
(r2, c2) = comp[j]
if s.solved[r1, c1] || s.solved[r2, c2]
continue
end
cands1 = s.candidates[r1, c1]
cands2 = s.candidates[r2, c2]
# Only apply when both cells have exactly 2 candidates
if length(cands1) != 2 || length(cands2) != 2
continue
end
# Find common candidate (bridge)
common = intersect(cands1, cands2)
if length(common) != 1
continue
end
bridge = first(common)
low = first(setdiff(cands1, common))
high = first(setdiff(cands2, common))
# Make sure low < bridge < high
if low > bridge
low, high = high, low
r1, c1, r2, c2 = r2, c2, r1, c1
end
if !(low < bridge < high)
continue
end
gap = high - low
if gap >= size
# Remove bridge from other cells
for (r, c) in comp
if (r, c) != (r1, c1) && (r, c) != (r2, c2) && !s.solved[r, c]
if bridge in s.candidates[r, c]
remCandidate!(s, r, c, bridge)
effective = true
end
end
end
end
end
end
end
return effective ? mindgap_hardness(compSize) : 0
end
"""
useNakedSet(s, setSize)
Find naked sets of exactly `setSize` cells in compartments (and rows/columns).
If `setSize` cells contain only `setSize` candidates total, remove those from other cells.
Returns `naked_set_hardness(setSize)` if progress was made, else 0.
"""
function useNakedSet(s::Str8ts, setSize::Int)
# In-compartment naked sets
for comp in vcat(s.rowCompartments, s.colCompartments)
if length(comp) <= setSize
continue
end
unsolved = [(r, c) for (r, c) in comp if !s.solved[r, c]]
if length(unsolved) <= setSize
continue
end
smallCells = [(r, c) for (r, c) in unsolved if length(s.candidates[r, c]) <= setSize]
for subset in combinations(smallCells, setSize)
unionCands = BitSet()
for (r, c) in subset
union!(unionCands, s.candidates[r, c])
end
if length(unionCands) == setSize
effective = false
for (r, c) in comp
if !((r, c) in subset) && !s.solved[r, c]
for n in collect(unionCands)
if n in s.candidates[r, c]
remCandidate!(s, r, c, n)
effective = true
end
end
end
end
if effective
return naked_set_hardness(setSize)
end
end
end
end
# Cross-compartment naked sets within rows
for r in 1:9
unsolved = [(r, c) for c in 1:9 if !s.isBlack[r, c] && !s.solved[r, c]]
if length(unsolved) <= setSize
continue
end
smallCells = [(rr, c) for (rr, c) in unsolved if length(s.candidates[rr, c]) <= setSize]
for subset in combinations(smallCells, setSize)
unionCands = BitSet()
for (rr, c) in subset
union!(unionCands, s.candidates[rr, c])
end
if length(unionCands) == setSize
effective = false
for c in 1:9
if !s.isBlack[r, c] && !s.solved[r, c] && !((r, c) in subset)
for n in collect(unionCands)
if n in s.candidates[r, c]
remCandidate!(s, r, c, n)
effective = true
end
end
end
end
if effective
return naked_set_hardness(setSize)
end
end
end
end
# Cross-compartment naked sets within columns
for c in 1:9
unsolved = [(r, c) for r in 1:9 if !s.isBlack[r, c] && !s.solved[r, c]]
if length(unsolved) <= setSize
continue
end
smallCells = [(r, cc) for (r, cc) in unsolved if length(s.candidates[r, cc]) <= setSize]
for subset in combinations(smallCells, setSize)
unionCands = BitSet()
for (r, cc) in subset
union!(unionCands, s.candidates[r, cc])
end
if length(unionCands) == setSize
effective = false
for r in 1:9
if !s.isBlack[r, c] && !s.solved[r, c] && !((r, c) in subset)
for n in collect(unionCands)
if n in s.candidates[r, c]
remCandidate!(s, r, c, n)
effective = true
end
end
end
end
if effective
return naked_set_hardness(setSize)
end
end
end
end
return 0
end
"""
useHiddenSet(s, setSize)
Find hidden sets of exactly `setSize` sure candidates appearing in exactly `setSize` cells.
Remove other candidates from those cells.
Returns `hidden_set_hardness(setSize)` if progress was made, else 0.
"""
function useHiddenSet(s::Str8ts, setSize::Int)
# In-compartment hidden sets
for comp in vcat(s.rowCompartments, s.colCompartments)
if length(comp) <= setSize
continue
end
ranges = getCompartmentRanges(s, comp)
sure = getSureCandidates(ranges, length(comp))
if length(sure) < setSize
continue
end
candidateCells = Dict{Int, Vector{Tuple{Int,Int}}}()
for n in sure
candidateCells[n] = [(r, c) for (r, c) in comp if n in s.candidates[r, c]]
end
for subset in combinations(collect(sure), setSize)
cells = Set{Tuple{Int,Int}}()
for n in subset
for cell in candidateCells[n]
push!(cells, cell)
end
end
if length(cells) == setSize
effective = false
for (r, c) in cells
if !s.solved[r, c]
for n in collect(s.candidates[r, c])
if !(n in subset)
remCandidate!(s, r, c, n)
effective = true
end
end
end
end
if effective
return hidden_set_hardness(setSize)
end
end
end
end
# Cross-compartment hidden sets within rows
for r in 1:9
whiteCells = [(r, c) for c in 1:9 if !s.isBlack[r, c]]
if length(whiteCells) <= setSize
continue
end
allCands = BitSet()
for (rr, c) in whiteCells
union!(allCands, s.candidates[rr, c])
end
for subset in combinations(collect(allCands), setSize)
cells = Set{Tuple{Int,Int}}()
for n in subset
for (rr, c) in whiteCells
if n in s.candidates[rr, c]
push!(cells, (rr, c))
end
end
end
if length(cells) == setSize
allSure = all(n -> begin
compIdx = s.cellToRowCompartment[r, first(c for (_, c) in cells if n in s.candidates[r, c])]
comp = s.rowCompartments[compIdx]
compRanges = getCompartmentRanges(s, comp)
compSure = getSureCandidates(compRanges, length(comp))
n in compSure
end, subset)
if allSure
effective = false
for (rr, c) in cells
if !s.solved[rr, c]
for n in collect(s.candidates[rr, c])
if !(n in subset)
remCandidate!(s, rr, c, n)
effective = true
end
end
end
end
if effective
return hidden_set_hardness(setSize)
end
end
end
end
end
return 0
end
"""
useLockedCompartments(s)
Handle locked compartments - when compartments share ranges and constrain each other.
"""
function useLockedCompartments(s::Str8ts)
effective = false
# Check row compartments that could lock each other
for r in 1:9
comps = [comp for comp in s.rowCompartments if !isempty(comp) && comp[1][1] == r]
if length(comps) >= 2
for i in 1:length(comps)
for j in i+1:length(comps)
eff = checkLockedCompartments!(s, comps[i], comps[j])
effective = effective || eff
end
end
end
end
# Check column compartments
for c in 1:9
comps = [comp for comp in s.colCompartments if !isempty(comp) && comp[1][2] == c]