Folie 20:
var base; function createXWord(indexes){ word = "" for(let i = 0; i < indexes.length; i++){ word += base[indexes[i]-1][0] } return word } function createYWord(indexes){ word = "" for(let i = 0; i < indexes.length; i++){ word += base[indexes[i]-1][1] } return word } function validateSolution(solution){ solX = createXWord(solution) solY = createYWord(solution) checkLength = Math.min(solX.length,solY.length) if(solX.slice(0, checkLength) == solY.slice(0, checkLength)){ if(solX.length == solY.length){ return solution } else { return true } } else { return false } } function generateSolutions(starts){ solutions = [] if(starts.length == 0){ for(let j = 0; j < base.length; j++){ solutions.push([j+1]) } } for(let i = 0; i < starts.length; i++){ for(let j = 0; j < base.length; j++){ solutions.push(starts[i].concat([j+1])) } } return solutions } function solve(b){ base = b solution = [] solution = generateSolutions(solution) while(true){ faulty = [] for(let s = 0; s < solution.length; s++){ result = validateSolution(solution[s]) if(result == false){ faulty.push(s) }else if(result != true){ return result } } for(let f = faulty.length-1; f > -1; f--){ solution.splice(faulty[f],1) } if(solution.length == 0){ return false } solution = generateSolutions(solution) } }
k = [["001","0"],["01","011"],["01","101"],["10","001"]] console.log(solve(k))
ausführen