Skip to content

Instantly share code, notes, and snippets.

@ivansky
Created September 27, 2020 18:00
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 ivansky/9c11c31bbb54fb5b0a3ff7fa7ca55ccf to your computer and use it in GitHub Desktop.
Save ivansky/9c11c31bbb54fb5b0a3ff7fa7ca55ccf to your computer and use it in GitHub Desktop.
Pathfinder Calculation
const original = [1, 0, 0, 0, 1, 1, 0];
const relations = {
1: [1, 2, 6],
2: [1, 2],
3: [3, 5],
4: [4, 5, 6],
5: [3, 4, 5],
6: [1, 4, 6],
};
const variants = [];
for (let i = 0; i < 100; i++) {
let steps = [];
let prev = -1;
let result = [...original];
while (result.includes(0)) {
let next = getRandomInt(6) + 1;
while (next === prev) {
prev = next;
next = getRandomInt(6) + 1;
}
relations[next].forEach((swapIndex) => {
result[swapIndex] = +Boolean(!result[swapIndex])
});
steps.push(next);
}
variants.push(steps);
}
variants.sort((a, b) => a.length - b.length);
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
console.log(variants[0].length, variants[99].length, JSON.stringify(variants[0]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment