Bit representations
GNPX ver5.0 uses a lot of Bit representations in analysis algorithms/codes.
Bit expression can express logic intuitively and easily.
The occurrence of bugs has also been suppressed (probably).
(1) Operations
81 bits are required to represent the Sudoku board, so “UInt128” is used. The following functions are used for shorthand expressions. Instructions for use can be found in GNPX.
static public partial class GNPZExtender{
static private readonly UInt128 _filter0081 = (UInt128.One<<81)-1;
static private readonly UInt128 qZero = UInt128.Zero;
static private readonly UInt128 qOne = UInt128.One;
static private readonly UInt128 _b9 = 0x1FF;
// [att] UInt128: instance variable! Original variable remains unchanged.
static public UInt128 DifSet( this UInt128 A, UInt128 B ) => (UInt128)(A&(B^UInt128.MaxValue));
static public UInt128 Set( this UInt128 A, int rc ) => A | qOne
static public UInt128 Set( this UInt128 A, UInt128 B ) => A | B;
static public UInt128 Reset( this UInt128 A, int rc ) => A & ((qOne<<rc)^UInt128.MaxValue);
static public UInt128 Reset( this UInt128 A, UInt128 B ) => A & B^UInt128.MaxValue;
static public bool IsHit( this UInt128 A, int rc ) => (A & (qOne<<rc)) != qZero;
static public bool IsHit( this UInt128 A, UInt128 B ) => (A & B) != qZero;
static public bool IsZero( this UInt128 A ) => (A == qZero);
static public bool IsNotZero( this UInt128 A ) => (A != qZero);
}
(2) Constant
ConnectedCells81 and HouseCells81 are examples of the most basic constants used in the analysis algorithm.
- ConnectedCells81
ConnectedCells81 is a constant that represents cells connected to board 81 cells in bits. The cells to be connected are found using the AND operation with the bit representation of the Sudoku puzzle.ConnectedCells81 ("."=0 Separated by 9,27 bits).11111111 111...... 111...... /1........ 1........ 1........ /1........ 1........ 1........ 1.1111111 111...... 111...... /.1....... .1....... .1....... /.1....... .1....... .1....... 11.111111 111...... 111...... /..1...... ..1...... ..1...... /..1...... ..1...... ..1...... 111.11111 ...111... ...111... /...1..... ...1..... ...1..... /...1..... ...1..... ...1..... 1111.1111 ...111... ...111... /....1.... ....1.... ....1.... /....1.... ....1.... ....1.... 11111.111 ...111... ...111... /.....1... .....1... .....1... /.....1... .....1... .....1... 111111.11 ......111 ......111 /......1.. ......1.. ......1.. /......1.. ......1.. ......1.. 1111111.1 ......111 ......111 /.......1. .......1. .......1. /.......1. .......1. .......1. 11111111. ......111 ......111 /........1 ........1 ........1 /........1 ........1 ........1 111...... .11111111 111...... /1........ 1........ 1........ /1........ 1........ 1........ 111...... 1.1111111 111...... /.1....... .1....... .1....... /.1....... .1....... .1....... 111...... 11.111111 111...... /..1...... ..1...... ..1...... /..1...... ..1...... ..1...... ...111... 111.11111 ...111... /...1..... ...1..... ...1..... /...1..... ...1..... ...1..... ...111... 1111.1111 ...111... /....1.... ....1.... ....1.... /....1.... ....1.... ....1.... ...111... 11111.111 ...111... /.....1... .....1... .....1... /.....1... .....1... .....1... ......111 111111.11 ......111 /......1.. ......1.. ......1.. /......1.. ......1.. ......1.. ......111 1111111.1 ......111 /.......1. .......1. .......1. /.......1. .......1. .......1. ......111 11111111. ......111 /........1 ........1 ........1 /........1 ........1 ........1 111...... 111...... .11111111 /1........ 1........ 1........ /1........ 1........ 1........ 111...... 111...... 1.1111111 /.1....... .1....... .1....... /.1....... .1....... .1....... 111...... 111...... 11.111111 /..1...... ..1...... ..1...... /..1...... ..1...... ..1...... ...111... ...111... 111.11111 /...1..... ...1..... ...1..... /...1..... ...1..... ...1..... ...111... ...111... 1111.1111 /....1.... ....1.... ....1.... /....1.... ....1.... ....1.... ...111... ...111... 11111.111 /.....1... .....1... .....1... /.....1... .....1... .....1... ......111 ......111 111111.11 /......1.. ......1.. ......1.. /......1.. ......1.. ......1.. ......111 ......111 1111111.1 /.......1. .......1. .......1. /.......1. .......1. .......1. ......111 ......111 11111111. /........1 ........1 ........1 /........1 ........1 ........1 1........ 1........ 1........ /.11111111 111...... 111...... /1........ 1........ 1........ .1....... .1....... .1....... /1.1111111 111...... 111...... /.1....... .1....... .1....... ..1...... ..1...... ..1...... /11.111111 111...... 111...... /..1...... ..1...... ..1...... ...1..... ...1..... ...1..... /111.11111 ...111... ...111... /...1..... ...1..... ...1..... ....1.... ....1.... ....1.... /1111.1111 ...111... ...111... /....1.... ....1.... ....1.... .....1... .....1... .....1... /11111.111 ...111... ...111... /.....1... .....1... .....1... ......1.. ......1.. ......1.. /111111.11 ......111 ......111 /......1.. ......1.. ......1.. .......1. .......1. .......1. /1111111.1 ......111 ......111 /.......1. .......1. .......1. ........1 ........1 ........1 /11111111. ......111 ......111 /........1 ........1 ........1 1........ 1........ 1........ /111...... .11111111 111...... /1........ 1........ 1........ .1....... .1....... .1....... /111...... 1.1111111 111...... /.1....... .1....... .1....... ..1...... ..1...... ..1...... /111...... 11.111111 111...... /..1...... ..1...... ..1...... ...1..... ...1..... ...1..... /...111... 111.11111 ...111... /...1..... ...1..... ...1..... ....1.... ....1.... ....1.... /...111... 1111.1111 ...111... /....1.... ....1.... ....1.... .....1... .....1... .....1... /...111... 11111.111 ...111... /.....1... .....1... .....1... ......1.. ......1.. ......1.. /......111 111111.11 ......111 /......1.. ......1.. ......1.. .......1. .......1. .......1. /......111 1111111.1 ......111 /.......1. .......1. .......1. ........1 ........1 ........1 /......111 11111111. ......111 /........1 ........1 ........1 1........ 1........ 1........ /111...... 111...... .11111111 /1........ 1........ 1........ .1....... .1....... .1....... /111...... 111...... 1.1111111 /.1....... .1....... .1....... ..1...... ..1...... ..1...... /111...... 111...... 11.111111 /..1...... ..1...... ..1...... ...1..... ...1..... ...1..... /...111... ...111... 111.11111 /...1..... ...1..... ...1..... ....1.... ....1.... ....1.... /...111... ...111... 1111.1111 /....1.... ....1.... ....1.... .....1... .....1... .....1... /...111... ...111... 11111.111 /.....1... .....1... .....1... ......1.. ......1.. ......1.. /......111 ......111 111111.11 /......1.. ......1.. ......1.. .......1. .......1. .......1. /......111 ......111 1111111.1 /.......1. .......1. .......1. ........1 ........1 ........1 /......111 ......111 11111111. /........1 ........1 ........1 1........ 1........ 1........ /1........ 1........ 1........ /.11111111 111...... 111...... .1....... .1....... .1....... /.1....... .1....... .1....... /1.1111111 111...... 111...... ..1...... ..1...... ..1...... /..1...... ..1...... ..1...... /11.111111 111...... 111...... ...1..... ...1..... ...1..... /...1..... ...1..... ...1..... /111.11111 ...111... ...111... ....1.... ....1.... ....1.... /....1.... ....1.... ....1.... /1111.1111 ...111... ...111... .....1... .....1... .....1... /.....1... .....1... .....1... /11111.111 ...111... ...111... ......1.. ......1.. ......1.. /......1.. ......1.. ......1.. /111111.11 ......111 ......111 .......1. .......1. .......1. /.......1. .......1. .......1. /1111111.1 ......111 ......111 ........1 ........1 ........1 /........1 ........1 ........1 /11111111. ......111 ......111 1........ 1........ 1........ /1........ 1........ 1........ /111...... .11111111 111...... .1....... .1....... .1....... /.1....... .1....... .1....... /111...... 1.1111111 111...... ..1...... ..1...... ..1...... /..1...... ..1...... ..1...... /111...... 11.111111 111...... ...1..... ...1..... ...1..... /...1..... ...1..... ...1..... /...111... 111.11111 ...111... ....1.... ....1.... ....1.... /....1.... ....1.... ....1.... /...111... 1111.1111 ...111... .....1... .....1... .....1... /.....1... .....1... .....1... /...111... 11111.111 ...111... ......1.. ......1.. ......1.. /......1.. ......1.. ......1.. /......111 111111.11 ......111 .......1. .......1. .......1. /.......1. .......1. .......1. /......111 1111111.1 ......111 ........1 ........1 ........1 /........1 ........1 ........1 /......111 11111111. ......111 1........ 1........ 1........ /1........ 1........ 1........ /111...... 111...... .11111111 .1....... .1....... .1....... /.1....... .1....... .1....... /111...... 111...... 1.1111111 ..1...... ..1...... ..1...... /..1...... ..1...... ..1...... /111...... 111...... 11.111111 ...1..... ...1..... ...1..... /...1..... ...1..... ...1..... /...111... ...111... 111.11111 ....1.... ....1.... ....1.... /....1.... ....1.... ....1.... /...111... ...111... 1111.1111 .....1... .....1... .....1... /.....1... .....1... .....1... /...111... ...111... 11111.111 ......1.. ......1.. ......1.. /......1.. ......1.. ......1.. /......111 ......111 111111.11 .......1. .......1. .......1. /.......1. .......1. .......1. /......111 ......111 1111111.1 ........1 ........1 ........1 /........1 ........1 ........1 /......111 ......111 11111111. - HouseCells81
A constant representing "cell belonging to house". The order is row house, column house, block house.HouseCells81 ("."=0 Separated by 9,27 bits)111111111 ......... ......... /......... ......... ......... /......... ......... ......... ......... 111111111 ......... /......... ......... ......... /......... ......... ......... ......... ......... 111111111 /......... ......... ......... /......... ......... ......... ......... ......... ......... /111111111 ......... ......... /......... ......... ......... ......... ......... ......... /......... 111111111 ......... /......... ......... ......... ......... ......... ......... /......... ......... 111111111 /......... ......... ......... ......... ......... ......... /......... ......... ......... /111111111 ......... ......... ......... ......... ......... /......... ......... ......... /......... 111111111 ......... ......... ......... ......... /......... ......... ......... /......... ......... 111111111 1........ 1........ 1........ /1........ 1........ 1........ /1........ 1........ 1........ .1....... .1....... .1....... /.1....... .1....... .1....... /.1....... .1....... .1....... ..1...... ..1...... ..1...... /..1...... ..1...... ..1...... /..1...... ..1...... ..1...... ...1..... ...1..... ...1..... /...1..... ...1..... ...1..... /...1..... ...1..... ...1..... ....1.... ....1.... ....1.... /....1.... ....1.... ....1.... /....1.... ....1.... ....1.... .....1... .....1... .....1... /.....1... .....1... .....1... /.....1... .....1... .....1... ......1.. ......1.. ......1.. /......1.. ......1.. ......1.. /......1.. ......1.. ......1.. .......1. .......1. .......1. /.......1. .......1. .......1. /.......1. .......1. .......1. ........1 ........1 ........1 /........1 ........1 ........1 /........1 ........1 ........1 111...... 111...... 111...... /......... ......... ......... /......... ......... ......... ...111... ...111... ...111... /......... ......... ......... /......... ......... ......... ......111 ......111 ......111 /......... ......... ......... /......... ......... ......... ......... ......... ......... /111...... 111...... 111...... /......... ......... ......... ......... ......... ......... /...111... ...111... ...111... /......... ......... ......... ......... ......... ......... /......111 ......111 ......111 /......... ......... ......... ......... ......... ......... /......... ......... ......... /111...... 111...... 111...... ......... ......... ......... /......... ......... ......... /...111... ...111... ...111... ......... ......... ......... /......... ......... ......... /......111 ......111 ......111
(3) Bit representation functions
- Bit representation generation function
In the analysis algorithm, the state of the entire board and the state of a group of cells such as ALS are expressed as a single variable expressed in bits.List<UCell> UCellLst; UInt128 bitExp = UCellLst.Aggregate(UInt128.Zero, (p,q) => p | qOne<<q.rc ); UInt128 Connected = UCellLst.Aggregate(UInt128.Zero, (p,q) => p | pConnectedCells81[q.rc] ); - Bit representation enumeration functions
Enumerates the position of the value "1" = original value from bit representation variables.- "IEGet_BtoNo()" is a 9-bit variable enumeration function
- "IEGet_BtoHouse27()" is a 27-bit variable enumeration function
- "IEGet_rc()" is an enumeration function for 128 (81) bit variables
static public IEnumerable<int>> IEGet_BtoNo( this int B, int sz=9 ){ for(int no=0; no<sz; no++ ){ if((B & (1<<no) )>0) yield return no; } yield break; } static public IEnumerableIEGet_BtoHouse27( this int P ){ for(int m=0; m<27; m++ ){ if( (P & (1<m)) > 0 ) yield return m; } yield break; } static public IEnumerable IEGet_rc( this UInt128 A, int mx=81){ for(int rc=0; rc<mx; rc++){ if( (A & (UInt128.One<<rc)) > 0 ) yield return rc; } yield break; }
(4) rcbFrame
These are functions that represent rows, columns, and blocks of Sudoku cells, and aggregate functions.
Used in algorithms that handle multiple cells (such as ALS) to examine the common parts and coverage status of a group of cells.
static public class ULogical_Node_Utility (only some){
static public int func_rcbFrame_Value( this int rc) => (1<<(rc.ToBlock()+18)) | (1<<(rc%9+9)) | (1<<rc/9);
static public int rcbFrame_Value( this int rc) => _rcbFrame_Value[rc];
static public int Ceate_rcbFrameAnd( this UInt128 b081 ) => b081.IEGet_rc().Aggregate( 0x7FFFFFF, (p,q) => p & rcbFrame_Value(q) );
static public int Ceate_rcbFrameOr( this UInt128 b081 ) => b081.IEGet_rc().Aggregate( 0, (p,q) => p | rcbFrame_Value(q) );
static public UInt128 AggregateCommonAxis( this UInt128 B) => B.IEGet_rc().Aggregate( (UInt128.One<<81)-1, (b,rc)=>b & pConnectedCells81[rc] );
}