-
-
Save getify/83d7aaaff50912636744bc101a921d00 to your computer and use it in GitHub Desktop.
the "knights-dialer" problem
This file contains hidden or 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
// 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 |
This file contains hidden or 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
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