Skip to content

Instantly share code, notes, and snippets.

@tantaroth
Created January 24, 2019 17:13
Show Gist options
  • Save tantaroth/09d867f278d4d9118d41a456267ac197 to your computer and use it in GitHub Desktop.
Save tantaroth/09d867f278d4d9118d41a456267ac197 to your computer and use it in GitHub Desktop.
AND Equation (TypeScript)
function toCombine(A: number[]): number[] {
A.push(A[0]);
A.splice(0, 1);
return A;
}
class ANDEquation {
private counter: number = 0;
restoreY(A?: number[]): number {
let result: number = null;
if (A && A.length > 0) {
const N: number[] = A.slice(0, (A.length-1));
const residue = A[A.length - 1];
if (this.counter < N.length) {
let i: number = 0;
while (i < N.length) {
if (result == null) {
result = N[i];
} else {
result &= N[i];
}
i++;
}
if (result !== residue) {
++this.counter;
return this.restoreY(toCombine(A));
}
} else {
this.counter = 0;
if (result !== residue) {
result = -1;
}
}
}
return result;
}
}
const AEq = new ANDEquation();
console.log(AEq.restoreY([1, 3, 5]));
console.log(AEq.restoreY([31, 7]));
console.log(AEq.restoreY([31, 7, 7]));
console.log(AEq.restoreY([1,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,1]));
console.log(AEq.restoreY([191411,256951,191411,191411,191411,256951,195507,191411,192435,191411,191411,195511,191419,191411,256947,191415,191475,195579,191415,191411,191483,191411,191419,191475,256947,191411,191411,191411,191419,256947, 191411,191411,191411]));
console.log(AEq.restoreY([1362,1066,1659,2010,1912,1720,1851,1593,1799,1805,1139,1493,1141,1163,1211]));
console.log(AEq.restoreY([2, 3, 7, 19]));
console.log(AEq.restoreY());
console.log(AEq.restoreY([]));
console.log(AEq.restoreY([1372]));
console.log(AEq.restoreY([-31, 7, 7]));
console.log(AEq.restoreY([0, 0, 0]));
console.log(AEq.restoreY([1, 2, 3]));
console.log(AEq.restoreY([7, 31, 7]));
console.log(AEq.restoreY([100, 31, 7]));
console.log(AEq.restoreY([9, 1, 21]));
console.log(AEq.restoreY([-9, -1, -21]));
const appDiv: HTMLElement = document.getElementById('app');
let HTML: string = '';
HTML += '<p> >> ' + AEq.restoreY([1, 3, 5]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([31, 7]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([31, 7, 7]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([1,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,1]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([191411,256951,191411,191411,191411,256951,195507,191411,192435,191411,191411,195511,191419,191411,256947,191415,191475,195579,191415,191411,191483,191411,191419,191475,256947,191411,191411,191411,191419,256947, 191411,191411,191411]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([1362,1066,1659,2010,1912,1720,1851,1593,1799,1805,1139,1493,1141,1163,1211]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([2, 3, 7, 19]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY() + '</p>';
HTML += '<p> >> ' + AEq.restoreY([]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([1372]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([-31, 7, 7]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([0, 0, 0]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([1, 2, 3]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([7, 31, 7]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([100, 31, 7]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([9, 1, 21]) + '</p>';
HTML += '<p> >> ' + AEq.restoreY([-9, -1, -21]) + '</p>';
appDiv.innerHTML = HTML;
@tantaroth
Copy link
Author

TO TEST THE CODE, CLICK TO THE NEXT StackBlitz LINK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment