Sudoku: Difference between revisions
Content deleted Content added
No edit summary |
m →ES6 |
||
Line 4,422:
====ES6====
<lang JavaScript>//-------------------------------------------
* The doubly-doubly circularly linked data object.
* Data object X
Line 4,429 ⟶ 4,430:
/**
* @param {string} V
* @param {!DoX
*/
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
/**
* 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;
};
/**
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 {
*/
const gridMeta = s => {
const g = s.split('');
▲ /**
const numCells = g.length;
/**▼
const nCandid = Math.sqrt(numCells);
const N = Math.sqrt(nCandid);
const g2D = g.map(e => isNaN(e * 1) ?
new Array(nCandid).fill(1).map((_, i) => i + 1) :
[e * 1]);
return [numCells, N, nCandid, g2D];
};
*/
/**
Line 4,633 ⟶ 4,634:
const reduceGrid = puzString => {
printGrid(puzString);
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);
|