-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
61 lines (52 loc) · 1.75 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function findBox(row, col) {
let rGroup = Math.floor(row / 3);
let cGroup = Math.floor(col / 3);
return (3 * cGroup) + rGroup;
}
function getId(row, col) {
return (9 * col) + row;
}
// [9, 8, 7, 6, 5, 4, 3, 2, 1]
// [1 1 1 1 1 1 1 1 1]
// 256 128 64 32 16 8 4 2 1
function convertToBinary(value) {
let bits = 10**(value - 1);
return parseInt(bits, 2);
}
function convertFromBinary(value) {
let returnValue = null;
for (let i = 1; i < 10; i++) {
let v = convertToBinary(i);
if (value !== v) continue;
returnValue = i;
break;
}
return returnValue;
}
function binaryValueContains(binaryFull, value) {
let bValue = convertToBinary(value);
return binaryValueContainsOtherBinary(binaryFull, bValue);
}
function binaryValueContainsOtherBinary(binaryFull, binaryCompare) {
return binaryCompare - (binaryCompare & binaryFull) === 0;
}
function possibleValuesFromBinary(binaryValue) {
let values = [];
for (let i = 1; i < 10; i++) {
if (!binaryValueContains(binaryValue, i)) continue;
values.push(i);
}
return values;
}
function cellInRowColBox(cellSelected, cellCompared) {
if (cellSelected.row === cellCompared.row) return true;
if (cellSelected.col === cellCompared.col) return true;
if (cellSelected.box === cellCompared.box) return true;
return false;
}
function filterCells(cells, filterBy, value) {
if (filterBy === "row") return cells.filter(c => c.row === value);
if (filterBy === "col") return cells.filter(c => c.col === value);
if (filterBy === "box") return cells.filter(c => c.box === value);
return cells;
}