Str8ts String Definitions
As of 6th July 2024 I'm employing a new Str8ts string definition to transport information about the board and candidates. This is currently employed on the
[Email this Board] feature - best place to copy/paste the new link
The old method was to pass the clues as an 81 character string + the black/white cells as an 81 character string, for example:
The new string also transports whether a big number is a clue or solution AND all the candidates of empty cells. And is just one third longer (243 characters instead of 162).
An example is
Users of the solver may want to know more about this string so that they can duplicate it for their own uses and understand how it works. So I'm presenting the information here.
The candidates are the largest payload. We need to store all of 1 to 9 and any combination. This can be done in nine bits if we let each bit represent a number in this way: 1=1, 2=2, 3=4, 4=8, 5=16, 6=32, 7=64, 8=128 and 9=256. A solved cell has one 'candidate' number.
Both the clues/solution distinction and the black/white cells need only one bit between them, so 11 in all. I've chosen to store the candidates bits first, then the cell colour, then the clue flag. All nine candidates add up to 511. Since candidates must exist on an empty white cell the two flag bits will be 0 and 0. This gives us a maximum number of 51100 or 1vs in base 32. A 9 on a black cell will be 25611. So unlike the
Sudoku and
Killer transport strings, these are 3 bytes per cell and not two.
A bit of javascript code shows how this is done
for (y = 0; y < 9; y++)
for (x = 0; x < 9; x++) {
n = get the cell value or set of candidates as bits
n = (n << 2); // shift to make space for 2 more bits
n += ((isclue) ? 2 : 0) + ((isblack) ? 1 : 0);
h = n.toString(32); // convert to base 32
if (h.length < 2) h = '0' + h; // pad the number if not two digits
if (h.length < 3) h = '0' + h; // pad the number if not three digits
s += h; // append to string being made
}
To unpack a string we do the following
// This splits a string into an array of elements each 3 characters long
var narr = theString.match(/.{1,3}/g);
if (narr.length != 81) return false; // Sanity length check
for (y = 0; y < 9; y++)
for (x = 0; x < 9; x++) {
n = parseInt(narr[y * 9 + x], 32); // convert base 32 to decimal
clue(y, x) = (n & 2) ? 1 : 0; // extract the clue flag
colour(y, x) = (n & 1) ? 1 : 0; // extract the black/white flag
n = n >> 2; Shift the bits to remove the flags
if (bit_count(n) == 1) {
// save the number as a clue or solution
} else {
// save the number as a set of candidates
}
}
Any problems or questions, let me know in the comments below
Comments