Created
August 24, 2018 17:08
-
-
Save randrews/ba6c2c7cde6fed91e6760f6ae7ff78e0 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 next = (n) => [ | |
(n * 10) + 1, | |
n + 12, | |
n * 13, | |
Math.floor(n / 10), | |
parseInt(n.toString().split('').reverse().join(''), 10) | |
]; | |
const open = [0]; | |
const goal = 196; | |
const paths = {}; | |
while (open.length > 0) { | |
const curr = open.shift(); | |
const adj = next(curr); | |
adj.forEach((p) => { | |
if (paths[p] === undefined) { | |
open.push(p); | |
paths[p] = curr; | |
} | |
}); | |
if (paths[goal]) break; | |
} | |
const path = []; | |
let curr = goal; | |
do { | |
path.push(curr); | |
curr = paths[curr]; | |
} while (curr !== 0) | |
console.log(path.reverse()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment