Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created January 31, 2017 17:14
Show Gist options
  • Save railsstudent/3c40de3a53569fe397631e93fafbac3f to your computer and use it in GitHub Desktop.
Save railsstudent/3c40de3a53569fe397631e93fafbac3f to your computer and use it in GitHub Desktop.
function getPINs(observed) {
// return [observed].concat(getPINsHelper(observed));
// return getPINsHelper(observed);
let adjacentPins = {
'1': ['1','2','4'],
'2': ['1','2','3','5'],
'3': ['2','3','6'],
'4': ['1','4','5','7'],
'5': ['2','4','5','6','8'],
'6': ['3','5','6','9'],
'7': ['4','7','8'],
'8': ['5','7','8','9','0'],
'9': ['6','8','9'],
'0': ['0', '8']
};
if (observed.length === 1) {
return adjacentPins[observed];
}
let allPins = [];
let subPins = getPINs(observed.substring(1));
adjacentPins[observed[0]].forEach((p) => {
allPins = allPins.concat(subPins.reduce((acc, sp) => {
acc.push(p + sp);
return acc;
}, []));
});
return allPins;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment