Created
August 12, 2021 05:39
-
-
Save jennyluciav/9d2052526dea7f0408b1e4fd58ea8221 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
function generateSolution(solution,position) { | |
if (solution[position] == 'V'){ | |
solution[position] = 'M'; | |
} else { | |
solution[position] = 'V'; | |
} | |
console.log("Setting in "+position+": "+solution[position]); | |
} | |
function isUnsatified (solution,customer) { | |
let satisfied = false | |
customer.forEach((value, key, map) => { | |
let x = false; | |
if(solution[key] == value || solution[key] == null) { | |
x = true | |
} | |
satisfied = satisfied || x ; | |
}); | |
return !satisfied; | |
} | |
function main(){ | |
/* | |
const quantityOfCurrys = 5; | |
let solution = [null,null,null,null,null]; | |
let customer1 = new Map(); | |
customer1.set(0,"M"); | |
customer1.set(2,"V"); | |
customer1.set(4,"V"); | |
let customer2 = new Map(); | |
customer2.set(1,"V"); | |
customer2.set(2,"M"); | |
customer2.set(3,"V"); | |
let customer3 = new Map(); | |
customer3.set(4,"M"); | |
let customers = [customer1,customer2,customer3]; | |
*/ | |
/* | |
const quantityOfCurrys = 1; | |
let solution = [null]; | |
let customer1 = new Map(); | |
customer1.set(0,"M"); | |
let customer2 = new Map(); | |
customer2.set(0,"V"); | |
let customers = [customer1,customer2]; | |
*/ | |
/* | |
const quantityOfCurrys = 5; | |
let solution = [null,null,null,null,null]; | |
let customer1 = new Map(); | |
customer1.set(1,"M"); | |
let customer2 = new Map(); | |
customer2.set(4,"V"); | |
let customer3 = new Map(); | |
customer3.set(0,"V"); | |
let customer4 = new Map(); | |
customer4.set(4,"V"); | |
customer4.set(0,"V"); | |
customer4.set(3,"M"); | |
let customer5 = new Map(); | |
customer5.set(2,"V"); | |
let customer6 = new Map(); | |
customer6.set(4,"V"); | |
let customer7 = new Map(); | |
customer7.set(2,"V"); | |
customer7.set(4,"V"); | |
customer7.set(0,"V"); | |
let customer8 = new Map(); | |
customer8.set(2,"V"); | |
let customer9 = new Map(); | |
customer9.set(1,"M"); | |
let customer10 = new Map(); | |
customer10.set(4,"V"); | |
customer10.set(0,"V"); | |
let customer11 = new Map(); | |
customer11.set(1,"M"); | |
let customer12 = new Map(); | |
customer12.set(4,"V"); | |
let customer13 = new Map(); | |
customer13.set(3,"M"); | |
let customer14 = new Map(); | |
customer14.set(4,"V"); | |
customer14.set(3,"M"); | |
let customers = [customer1,customer2,customer3,customer4,customer5,customer6,customer7,customer8,customer9,customer10,customer11,customer12,customer13,customer14]; | |
*/ | |
const quantityOfCurrys = 3; | |
let solution = [null,null,null]; | |
let customer1 = new Map(); | |
customer1.set(0,"V"); | |
customer1.set(1,"M"); | |
let customer2 = new Map(); | |
customer2.set(2 ,"V"); | |
let customer3 = new Map(); | |
customer3.set(2 ,"M"); | |
let customers = [customer1,customer2,customer3]; | |
let position = 0; | |
while(position < quantityOfCurrys && position >= 0){ | |
generateSolution(solution,position); | |
const conditions = customers.map(customer => { | |
return isUnsatified(solution,customer); | |
}); | |
const isAnyUnsatisfied = ! conditions.every(item => item == false); | |
if (isAnyUnsatisfied) { | |
if (solution[position] == 'M') { | |
while(solution[position] != 'V' && position >= 0) { | |
console.log("Unsetting in "+position+": "+solution[position]); | |
solution[position] = null; | |
position--; | |
} | |
} | |
}else{ | |
position++; | |
} | |
} | |
if (position == -1){ | |
console.log("No solution exists") | |
}else{ | |
console.log(solution) | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment