Skip to content

Instantly share code, notes, and snippets.

@getify
Last active November 12, 2018 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/83d7aaaff50912636744bc101a921d00 to your computer and use it in GitHub Desktop.
Save getify/83d7aaaff50912636744bc101a921d00 to your computer and use it in GitHub Desktop.
the "knights-dialer" problem
// https://medium.com/@alexgolec/google-interview-questions-deconstructed-the-knights-dialer-f780d516f029
function nearby(number) {
return (
number === 0 ? [4,6] :
number == 1 ? [6,8] :
number == 2 ? [7,9] :
number == 3 ? [4,8] :
number == 4 ? [3,9,0] :
number == 6 ? [1,7,0] :
number == 7 ? [2,6] :
number == 8 ? [1,3] :
number == 9 ? [2,4] :
[]
);
}
var countPaths = memoize(countPaths);
function memoize(fn) {
var cache = {};
return function memoized(start,length){
if (!cache[`${start}:${length}`]) {
cache[`${start}:${length}`] = fn(start,length);
}
return cache[`${start}:${length}`];
};
}
function countPaths(startDigit,hopCount = 0) {
if (hopCount == 0) return 1;
var pathCount = 0;
for (let digit of nearby(startDigit)) {
pathCount += countPaths(digit,hopCount-1);
}
return pathCount;
}
function countPaths2(startDigit,hopCount = 0) {
if (hopCount == 0) return 1;
var priorPathCounts = Array(10).fill(1);
var pathCounts;
var hops = 1;
while (hops <= hopCount) {
pathCounts = Array(10).fill(0);
hops++;
for (let digit = 0; digit <= 9; digit++) {
for (let n of nearby(digit)) {
pathCounts[digit] += priorPathCounts[n];
}
}
priorPathCounts = pathCounts;
}
return pathCounts[startDigit];
}
countPaths(1,50); // 894498933328314400
countPaths2(1,50); // 894498933328314400
function followPath(path) {
let nextHops = nearby(path[path.length-1]);
var pathForwardFound = false;
for (let nextHop of nextHops) {
if (!path.includes(nextHop)) {
pathForwardFound = true;
let nextPath = [...path,nextHop];
followPath(nextPath);
}
}
if (!pathForwardFound) {
console.log(path);
return;
}
}
function uniqueDigitPaths() {
for (let i = 0; i <= 9; i++) {
let nextHops = nearby(i);
for (let nextHop of nextHops) {
let path = [i,nextHop];
followPath(path);
}
console.log("");
}
}
uniqueDigitPaths();
// [0, 4, 3, 8, 1, 6, 7, 2, 9]
// [0, 4, 9, 2, 7, 6, 1, 8, 3]
// [0, 6, 1, 8, 3, 4, 9, 2, 7]
// [0, 6, 7, 2, 9, 4, 3, 8, 1]
// [1, 6, 7, 2, 9, 4, 3, 8]
// [1, 6, 7, 2, 9, 4, 0]
// [1, 6, 0, 4, 3, 8]
// [1, 6, 0, 4, 9, 2, 7]
// [1, 8, 3, 4, 9, 2, 7, 6, 0]
// [1, 8, 3, 4, 0, 6, 7, 2, 9]
// [2, 7, 6, 1, 8, 3, 4, 9]
// [2, 7, 6, 1, 8, 3, 4, 0]
// [2, 7, 6, 0, 4, 3, 8, 1]
// [2, 7, 6, 0, 4, 9]
// [2, 9, 4, 3, 8, 1, 6, 7]
// [2, 9, 4, 3, 8, 1, 6, 0]
// [2, 9, 4, 0, 6, 1, 8, 3]
// [2, 9, 4, 0, 6, 7]
// [3, 4, 9, 2, 7, 6, 1, 8]
// [3, 4, 9, 2, 7, 6, 0]
// [3, 4, 0, 6, 1, 8]
// [3, 4, 0, 6, 7, 2, 9]
// [3, 8, 1, 6, 7, 2, 9, 4, 0]
// [3, 8, 1, 6, 0, 4, 9, 2, 7]
// [4, 3, 8, 1, 6, 7, 2, 9]
// [4, 3, 8, 1, 6, 0]
// [4, 9, 2, 7, 6, 1, 8, 3]
// [4, 9, 2, 7, 6, 0]
// [4, 0, 6, 1, 8, 3]
// [4, 0, 6, 7, 2, 9]
// [6, 1, 8, 3, 4, 9, 2, 7]
// [6, 1, 8, 3, 4, 0]
// [6, 7, 2, 9, 4, 3, 8, 1]
// [6, 7, 2, 9, 4, 0]
// [6, 0, 4, 3, 8, 1]
// [6, 0, 4, 9, 2, 7]
// [7, 2, 9, 4, 3, 8, 1, 6, 0]
// [7, 2, 9, 4, 0, 6, 1, 8, 3]
// [7, 6, 1, 8, 3, 4, 9, 2]
// [7, 6, 1, 8, 3, 4, 0]
// [7, 6, 0, 4, 3, 8, 1]
// [7, 6, 0, 4, 9, 2]
// [8, 1, 6, 7, 2, 9, 4, 3]
// [8, 1, 6, 7, 2, 9, 4, 0]
// [8, 1, 6, 0, 4, 3]
// [8, 1, 6, 0, 4, 9, 2, 7]
// [8, 3, 4, 9, 2, 7, 6, 1]
// [8, 3, 4, 9, 2, 7, 6, 0]
// [8, 3, 4, 0, 6, 1]
// [8, 3, 4, 0, 6, 7, 2, 9]
// [9, 2, 7, 6, 1, 8, 3, 4, 0]
// [9, 2, 7, 6, 0, 4, 3, 8, 1]
// [9, 4, 3, 8, 1, 6, 7, 2]
// [9, 4, 3, 8, 1, 6, 0]
// [9, 4, 0, 6, 1, 8, 3]
// [9, 4, 0, 6, 7, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment