Skip to content

Instantly share code, notes, and snippets.

@randrews
Created August 24, 2018 17:08
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 randrews/ba6c2c7cde6fed91e6760f6ae7ff78e0 to your computer and use it in GitHub Desktop.
Save randrews/ba6c2c7cde6fed91e6760f6ae7ff78e0 to your computer and use it in GitHub Desktop.
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