Skip to content

Commit

Permalink
Ensure that SumLine sum is <= 30 so that the state fits in 32 bits.
Browse files Browse the repository at this point in the history
  • Loading branch information
sigh committed May 23, 2024
1 parent 48c446d commit a4eac72
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion js/solver/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2621,6 +2621,11 @@ SudokuConstraintHandler.SumLine = class SumLine extends SudokuConstraintHandler
super(cells);
this._sum = +sum;
this._states = null;

if (this._sum > 30) {
// A sum of 30 fits within a 32-bit state.
throw ('SumLine sum must at most 30');
}
}

initialize(initialGrid, cellExclusions, shape) {
Expand All @@ -2629,7 +2634,7 @@ SudokuConstraintHandler.SumLine = class SumLine extends SudokuConstraintHandler
// Each state is a mask that represents the possible partial sums of a
// segment at a particular point. The i-th state corresponds to the
// cell boundary before the i-th cell.
const states = new Uint16Array(this.cells.length + 1);
const states = new Uint32Array(this.cells.length + 1);
states[0] = 1;
states[this.cells.length] = 1 << this._sum;
this._states = states;
Expand Down

0 comments on commit a4eac72

Please sign in to comment.