Skip to content

Commit

Permalink
Check the case where maxTotal == minTotal for SumLine.
Browse files Browse the repository at this point in the history
  • Loading branch information
sigh committed Jun 3, 2024
1 parent d493f39 commit b24e454
Showing 1 changed file with 0 additions and 28 deletions.
28 changes: 0 additions & 28 deletions js/solver/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2774,40 +2774,12 @@ SudokuConstraintHandler.SumLine = class SumLine extends SudokuConstraintHandler
minTotal += minMax >> 8;
}

// Check if it possible to reach the sum.
if (maxTotal < sum) return false;
// If the maximum sum is a multiple of the sum, then we know this is valid.
const maxRemainder = maxTotal % sum;

// Otherwise for the total to be a multiple of sum, the min and max must be
// different integers when divided by sum.
return minTotal < maxTotal - maxRemainder;
}

__checkTotalSum(grid) {
const cells = this.cells;
const numCells = cells.length;
const sum = this._sum;

// If there are multiple possible partial sums, then initial and final
// states may be inconsistent.
// In this case check that the totalSum == 0 (mod sum), which is equivalent
// to checking that the partialSum is consistent.
let maxTotal = 0;
for (let i = 0; i < numCells; i++) {
maxTotal += LookupTables.maxValue(grid[cells[i]]);
}
// Check if it possible to reach the sum.
if (maxTotal < sum) return false;
// If the maximum sum is a multiple of the sum, then we know this is valid.
const maxRemainder = maxTotal % sum;
if (maxRemainder == 0) return true;

let minTotal = 0;
for (let i = 0; i < numCells; i++) {
minTotal += LookupTables.minValue(grid[cells[i]]);
}

// Otherwise for the total to be a multiple of sum, the min and max must be
// different integers when divided by sum.
return minTotal < maxTotal - maxRemainder;
Expand Down

0 comments on commit b24e454

Please sign in to comment.