Created
September 27, 2020 18:00
-
-
Save ivansky/9c11c31bbb54fb5b0a3ff7fa7ca55ccf to your computer and use it in GitHub Desktop.
Pathfinder Calculation
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
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