Sudoku: Difference between revisions

Content deleted Content added
No edit summary
Line 4,422:
====ES6====
 
<lang JavaScript>//----------------------------------------------------------------------------[ Dancing Links and Algorithm X ]--
<lang JavaScript>/**
/**
* The doubly-doubly circularly linked data object.
* Data object X
Line 4,429 ⟶ 4,430:
/**
* @param {string} V
* @param {!DoX?=} H
*/
constructor(V, H) {
Line 4,442 ⟶ 4,443:
}
}
 
 
/**
Line 4,456 ⟶ 4,458:
return n;
};
 
 
/**
Line 4,495 ⟶ 4,498:
}
};
 
 
/**
Line 4,512 ⟶ 4,516:
return c;
};
 
 
/**
Line 4,528 ⟶ 4,533:
}
};
 
 
/**
Line 4,545 ⟶ 4,551:
};
 
 
//-----------------------------------------------------------[ Print Helpers to print ]--
/**
* Given the standard string format of a grid, print a formatted view of it.
Line 4,556 ⟶ 4,563:
let N = Math.sqrt(U);
let div = new Array(N).fill('+').reduce((p, c) => {
p.push(... Array.from(new Array(1 + N*2).fill('-')));
p.push(c);
return p;
Line 4,571 ⟶ 4,578:
return p;
}, '') + '|' + div;
processconsole.stdout.writelog(g);
};
 
 
/**
Line 4,586 ⟶ 4,594:
};
 
 
//------------------------------------------------------------------------------
//----------------------------------------------[ Grid to Exact cover Matrix ]--
/**
* Helper to get some meta about the grid.
* @param {!string} s The standard string representation of a grid.
* @return {[!Number,!Number,!Number,!Array<!Array>]}
*/
const gridMeta = s => {
 
const g = s.split('');
 
/**
* The total number of cells in the grid.
* @type {Number}
*/
const numCells = g.length;
 
/**
* The number of unique values that each cell can contain.
* Also the length of a row, the height of a column, and the size of a block.
* This is always simply the square of the N-value of the grid.
* @type {number}
*/
const nCandid = Math.sqrt(numCells);
 
/**
* The grid's N-value. Standard sudoku's have an N-value of 3.
* @type {number}
*/
const N = Math.sqrt(nCandid);
 
/**
* An array of arrays with the grid candidates in each position.
* @type {!Array}
*/
const g2D = g.map(e => isNaN(e * 1) ?
new Array(nCandid).fill(1).map((_, i) => i + 1) :
[e * 1]);
 
return [numCells, N, nCandid, g2D];
};
 
/**
* Given a cell grid index, return the row, column and box indexes.
* @param {!number} n The n-value of the grid. 3 for a 9x9 sudoku.
* @return {!function(!number): !Array<!number>}
*/
const indexesN = n => i => {
const ri = i => Math.floor(i / (n * n));
const ci = i => i % (n * n);
const r = ri(i);
const c = ci(i);
const b = Math.floor(r / n) * n + Math.floor(c / n);
return [r, c, b];
};
 
 
/**
Line 4,633 ⟶ 4,634:
const reduceGrid = puzString => {
 
// Print the unsolved grid.
printGrid(puzString);
 
/**
* Some meta about the grid, derived from the puzzle string.
*/
const [
numCells, // The total number of cells in a grid (81 for a 9x9 grid)
Line 4,648 ⟶ 4,644:
] = gridMeta(puzString);
 
/**
* Given a cell grid index, return the row, column and box indexes.
* @param {!number} n The n-value of the grid. 3 for a 9x9 sudoku.
* @return {!function(!number): !Array<!number>}
*/
const indexesN = n => i => {
const ri = i => Math.floor(i / (n * n));
const ci = i => i % (n * n);
const r = ri(i);
const c = ci(i);
const b = Math.floor(r / n) * n + Math.floor(c / n);
return [r, c, b];
};
const getIndex = indexesN(N);