Skip to content

Instantly share code, notes, and snippets.

@jougene
Last active November 16, 2017 15:05
Show Gist options
  • Save jougene/d13f46225e8effd86e5504bbc5cc8610 to your computer and use it in GitHub Desktop.
Save jougene/d13f46225e8effd86e5504bbc5cc8610 to your computer and use it in GitHub Desktop.
const splitByPairs = (x) => {
const pairsCount = x.length - 1;
return new Array(pairsCount).fill().map((index, val) => x.substr(val, 2));
};
const digitsCount = (digit, number) => number.split('').filter(num => num == digit).length;
const dec2bin = (dec) => { return (dec >>> 0).toString(2) };
const range = (l, r) => new Array(r - 1).fill().map((_,k) => k + l);
const fillByZeroes = (x, to) => {
const numLength = (x).toString().length;
if (numLength >= to) {
return x;
}
const zeroLacks = to - numLength;
return '0'.repeat(zeroLacks).concat((x).toString());
};
const withoutTwoZeros = (x, y) => {
const pow = x + y;
if(x == 1 && y == 1) {
return 2;
}
const zeroOnesColl = range(1, Math.pow(2, pow))
.map(x => dec2bin(x))
.map(x => fillByZeroes(x, pow));
//return zeroOnesColl;
const filtered = zeroOnesColl.filter(el => {
return digitsCount(0, el) == x && digitsCount(1, el) == y;
});
return filtered
.map(x => splitByPairs((x).toString()))
.filter(pairs => pairs.indexOf('00') == -1)
.length
};
console.log(withoutTwoZeros(2, 2)); // 3
console.log(withoutTwoZeros(1, 1)); // 2
console.log(withoutTwoZeros(1, 3)); // 4
console.log(withoutTwoZeros(2, 4)); // 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment