Last active
January 2, 2018 12:27
-
-
Save MunifTanjim/e08441cb3dd5838e9e83ed8c595257ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getDigitsArray = numberString => numberString.split('') | |
const getProbableNumbers = (placeholder, numberString) => { | |
let probableNumbers = [] | |
let stuffedDigitsArray = getDigitsArray(numberString).map( | |
digit => | |
digit == placeholder ? [null, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] : [digit] | |
) | |
while (stuffedDigitsArray.some(digitgGroup => digitgGroup.length > 1)) { | |
let probableNumberDigits = [] | |
stuffedDigitsArray.forEach( | |
digitgGroup => | |
digitgGroup.length > 1 | |
? probableNumberDigits.push(digitgGroup.pop()) | |
: probableNumberDigits.push(digitgGroup[0]) | |
) | |
probableNumbers.push(probableNumberDigits.join('')) | |
} | |
return probableNumbers | |
} | |
const findMultiplicationWithPlaceholdersSolution = ( | |
placeholder, | |
firstNumber, | |
secondNumber, | |
multiplicationResult, | |
...partialMultiplicationLines | |
) => { | |
let probableSolutions = [] | |
let probableFirstNumbers = getProbableNumbers(placeholder, firstNumber) | |
let probableSecondNumbers = getProbableNumbers(placeholder, secondNumber) | |
let probablePartialMultiplicationLines = Array( | |
...partialMultiplicationLines.map(line => []) | |
) | |
probableFirstNumbers.forEach(fNum => { | |
probableSecondNumbers.forEach(sNum => { | |
probableSolutions.push({ | |
firstNumber: fNum, | |
secondNumber: sNum, | |
partialMultiplicationLines: [], | |
result: `${Number(fNum) * Number(sNum)}` | |
}) | |
sNum | |
.split('') | |
.reverse() | |
.forEach((sDigit, idx) => { | |
probableSolutions[ | |
probableSolutions.length - 1 | |
].partialMultiplicationLines[idx] = `${Number(fNum) * Number(sDigit)}` | |
}) | |
}) | |
}) | |
return probableSolutions | |
.filter(sol => | |
matchWithoutPlaceholders(sol.result, multiplicationResult, placeholder) | |
) | |
.filter(sol => | |
sol.partialMultiplicationLines.reduce( | |
(bool, line, idx) => | |
bool && | |
matchWithoutPlaceholders( | |
line, | |
partialMultiplicationLines[idx], | |
placeholder | |
), | |
true | |
) | |
) | |
} | |
const matchWithoutPlaceholders = ( | |
number, | |
numberWithPlaceholders, | |
placeholder | |
) => | |
number.length == numberWithPlaceholders.length | |
? numberWithPlaceholders | |
.split('') | |
.reduce( | |
(bool, digit, idx) => bool && (digit == 'x' || digit == number[idx]), | |
true | |
) | |
: false | |
console.log( | |
findMultiplicationWithPlaceholdersSolution( | |
'x', | |
'x62', | |
'x7', | |
'5xxx4', | |
'xxx4', | |
'5xxx' | |
) | |
) | |
/* | |
x62 | |
x7 | |
----- | |
xxx4 | |
5xxx0 | |
----- | |
5xxx4 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment