Skip to content

Instantly share code, notes, and snippets.

@197291
Created June 16, 2019 16:16
Show Gist options
  • Save 197291/ee378aa024b9de5a16c3b91c77e8098e to your computer and use it in GitHub Desktop.
Save 197291/ee378aa024b9de5a16c3b91c77e8098e to your computer and use it in GitHub Desktop.
function isCryptSolution(crypt, solution) {
const map = {};
for(let m of solution) {
map[m[0]] = m[1];
}
for(let i in crypt) {
const line = crypt[i];
if( line.length > 1 && map[line[0]] == "0" )
return false;
crypt[i] = line.split('').reverse().join('');
}
let carry = 0;
const lenCrypt = crypt[2].length;
for(let i=0; i < lenCrypt || carry; ++i) {
const a = ~~map[crypt[0][i]],
b = ~~map[crypt[1][i]],
c = ~~map[crypt[2][i]];
const sum = a + b + carry;
carry = sum/10|0;
if (sum%10 != c) return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment