-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreSumGreaterLessThanForcing.java
More file actions
297 lines (249 loc) · 11.1 KB
/
PreSumGreaterLessThanForcing.java
File metadata and controls
297 lines (249 loc) · 11.1 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
import java.util.*;
/**
* Prunes values from Sum, GreaterThan, and LessThan regions by analyzing
* global pip supply and regional constraints.
*/
public final class PreSumGreaterLessThanForcing {
public static boolean DEBUG = false;
private static final int MASK_0_6 = 0x7F; // Binary 1111111 (bits 0 through 6)
public static int apply(PipsGame game) {
// --- PASS 1: INITIAL HEURISTIC PASS ---
int totalPruned = runHeuristicPass(game);
// --- PREPARE FOR PASS 2: TIGHTEN SUPPLY ---
int[] restrictedInventory = game.pipSupply.clone();
updateInventoryForForcedSingles(game, restrictedInventory);
// --- PASS 2: COMBINATORIAL PASS ---
totalPruned += runCombinatorialPass(game, restrictedInventory);
return totalPruned;
}
/**
* Pass 1: Uses fast min/max boundary logic to prune impossible values.
*/
private static int runHeuristicPass(PipsGame game) {
int prunedCount = 0;
int[] localInventory = game.pipSupply.clone();
for (Region region : game.regions) {
for (Cell cell : region.cells) {
if (cell.value < 0 && isNakedSingle(cell)) {
int singleVal = getSingleValue(cell);
if (localInventory[singleVal] > 0) {
localInventory[singleVal]--;
}
}
}
}
for (Region region : game.regions) {
List<Cell> targetCells = new ArrayList<>();
int currentRegionSum = 0;
for (Cell cell : region.cells) {
if (cell.value >= 0) currentRegionSum += cell.value;
else targetCells.add(cell);
}
if (targetCells.isEmpty()) continue;
int remainingTarget = region.target - currentRegionSum;
int emptySlots = targetCells.size();
for (Cell cell : targetCells) {
boolean cellIsForced = isNakedSingle(cell);
int forcedVal = cellIsForced ? getSingleValue(cell) : -1;
for (int v = PipsConstants.MIN_PIP; v <= PipsConstants.MAX_PIP; v++) {
// Bitwise check: Is bit v set?
if ((cell.forbiddenMask & (1 << v)) != 0) continue;
boolean wasRestored = false;
if (cellIsForced && v == forcedVal) {
localInventory[v]++;
wasRestored = true;
}
boolean isFeasible = false;
if (localInventory[v] > 0) {
localInventory[v]--;
isFeasible = checkValueFeasibility(v, emptySlots, remainingTarget, region.constraintTypeId, localInventory);
localInventory[v]++;
}
if (!isFeasible) {
// Bitwise set: Set bit v to 1
cell.forbiddenMask |= (1 << v);
prunedCount++;
if (DEBUG) {
System.out.printf("[Heuristic] Pruned %d at (%d,%d) | Type: %s%n", v, cell.r, cell.c, region.constraintTypeId);
}
}
if (wasRestored) {
localInventory[v]--;
}
}
}
}
return prunedCount;
}
/**
* Identifies Naked and Hidden singles to reserve their pips in the inventory.
*/
private static void updateInventoryForForcedSingles(PipsGame game, int[] inventory) {
if (DEBUG) System.out.println("--- Updating Supply for Singles ---");
Set<Cell> accountedCells = new HashSet<>();
for (Region region : game.regions) {
for (Cell cell : region.cells) {
if (cell.value < 0 && isNakedSingle(cell)) {
if (accountedCells.contains(cell)) continue;
int v = getSingleValue(cell);
if (inventory[v] > 0) {
inventory[v]--;
accountedCells.add(cell);
}
}
}
if (region.constraintTypeId ==PipsConstants.TYPE_SUM) {
for (int v = PipsConstants.MIN_PIP; v <= PipsConstants.MAX_PIP; v++) {
Cell hiddenCandidate = null;
int possibleSlotCount = 0;
for (Cell cell : region.cells) {
// Bitwise check: Is bit v clear?
if (cell.value < 0 && (cell.forbiddenMask & (1 << v)) == 0) {
possibleSlotCount++;
hiddenCandidate = cell;
}
}
if (possibleSlotCount == 1 && !accountedCells.contains(hiddenCandidate)) {
if (inventory[v] > 0) {
inventory[v]--;
accountedCells.add(hiddenCandidate);
}
}
}
}
}
}
/**
* Pass 2: Uses recursion to verify if a valid combination actually exists.
*/
private static int runCombinatorialPass(PipsGame game, int[] inventory) {
int prunedCount = 0;
for (Region region : game.regions) {
if (region.constraintTypeId !=PipsConstants.TYPE_SUM) continue;
List<Cell> emptyCells = new ArrayList<>();
int currentRegionSum = 0;
for (Cell cell : region.cells) {
if (cell.value >= 0) currentRegionSum += cell.value;
else emptyCells.add(cell);
}
if (emptyCells.isEmpty() || emptyCells.size() >= 6) continue;
int remainingTarget = region.target - currentRegionSum;
for (Cell targetCell : emptyCells) {
for (int v = PipsConstants.MIN_PIP; v <= PipsConstants.MAX_PIP; v++) {
// Bitwise check: Is bit v set?
if ((targetCell.forbiddenMask & (1 << v)) != 0) continue;
if (!checkByPermutation(targetCell, v, emptyCells, remainingTarget, region.constraintTypeId, inventory)) {
// Bitwise set: Set bit v to 1
targetCell.forbiddenMask |= (1 << v);
prunedCount++;
}
}
}
}
return prunedCount;
}
private static boolean checkByPermutation(Cell targetCell, int val, List<Cell> regionCells, int target, int type, int[] inventory) {
int[] workingInventory = inventory.clone();
for (Cell cell : regionCells) {
if (isNakedSingle(cell)) {
workingInventory[getSingleValue(cell)]++;
}
}
if (workingInventory[val] <= 0) return false;
workingInventory[val]--;
List<Cell> otherCells = new ArrayList<>(regionCells);
otherCells.remove(targetCell);
return canSatisfyRecursively(0, val, otherCells, target, type, workingInventory);
}
private static boolean canSatisfyRecursively(int index, int runningSum, List<Cell> others, int target, int type, int[] inventory) {
if (index == others.size()) {
switch (type) {
case PipsConstants.TYPE_SUM:
return runningSum == target;
case PipsConstants.TYPE_GREATER_THAN:
return runningSum > target;
case PipsConstants.TYPE_LESS_THAN:
return runningSum < target;
default:
return true;
}
}
Cell current = others.get(index);
if (type == PipsConstants.TYPE_SUM && runningSum > target) return false;
for (int v = PipsConstants.MIN_PIP; v <= PipsConstants.MAX_PIP; v++) {
// Bitwise check: Is bit v clear?
if ((current.forbiddenMask & (1 << v)) == 0 && inventory[v] > 0) {
inventory[v]--;
if (canSatisfyRecursively(index + 1, runningSum + v, others, target, type, inventory)) {
inventory[v]++;
return true;
}
inventory[v]++;
}
}
return false;
}
// --- UTILITIES ---
/**
* A "Naked Single" exists if exactly one bit in the 0-6 range is clear (0).
*/
private static boolean isNakedSingle(Cell c) {
// Invert mask and isolate bits 0-6. One set bit means one value is allowed.
int allowedBits = (~c.forbiddenMask) & MASK_0_6;
return Integer.bitCount(allowedBits) == 1;
}
/**
* Returns the pip value of the only allowed bit.
*/
private static int getSingleValue(Cell c) {
int allowedBits = (~c.forbiddenMask) & MASK_0_6;
if (allowedBits == 0) return -1;
// numberOfTrailingZeros returns the index of the first '1' bit
return Integer.numberOfTrailingZeros(allowedBits);
}
private static boolean checkValueFeasibility(int v, int totalSlots, int target, int type, int[] inventory) {
int remainingSlots = totalSlots - 1;
if (remainingSlots == 0) {
switch (type) {
case PipsConstants.TYPE_SUM:
return (v == target);
case PipsConstants.TYPE_GREATER_THAN:
return (v > target);
case PipsConstants.TYPE_LESS_THAN:
return (v < target);
default:
return true;
}
}
int neededFromOthers = target - v;
int minOthers = getFiniteBound(remainingSlots, inventory, true);
int maxOthers = getFiniteBound(remainingSlots, inventory, false);
switch (type) {
case PipsConstants.TYPE_SUM:
return (neededFromOthers >= minOthers && neededFromOthers <= maxOthers);
case PipsConstants.TYPE_GREATER_THAN:
return (v + maxOthers > target);
case PipsConstants.TYPE_LESS_THAN:
return (v + minOthers < target);
default:
return true;
}
}
private static int getFiniteBound(int count, int[] inventory, boolean findMin) {
int sum = 0, remainingToFill = count;
if (findMin) {
for (int val = 0; val <= 6 && remainingToFill > 0; val++) {
int take = Math.min(remainingToFill, inventory[val]);
sum += (take * val);
remainingToFill -= take;
}
} else {
for (int val = 6; val >= 0 && remainingToFill > 0; val--) {
int take = Math.min(remainingToFill, inventory[val]);
sum += (take * val);
remainingToFill -= take;
}
}
return remainingToFill > 0 ? (findMin ? 1000 : -1000) : sum;
}
}