function mo() { this.variabler = {}; this.tabel = {}; this.tabel_navn = ""; this.regler = []; this._internal_step_counter = 0; this.funcs = {}; } mo.prototype.addVariabelSet = function(navn, arr) { this.variabler[navn] = arr; return this; } mo.prototype.addVariableTable = function(navn, arr) { this.tabel[navn] = arr; this.tabel_navn = navn; return this; } mo.prototype.addClause = function() { var args = Array.prototype.slice.apply(arguments, []); this.regler.push(args.shift()); return this; } mo.prototype.addFunction = function(name,func) { this.funcs[name] = func; return this; } /*mo.prototype.randoma = function(tabl) { var ll = this.tabel[tabl].length, l = 0; var r = []; var taken = {} for (var navn in this.variabler) { taken[navn] = {} } while (l < ll) { var o = {} o[tabl] = this.tabel[tabl][l]; for (var navn in this.variabler) { var m = this.variabler[navn][Math.floor(Math.random() * this.variabler[navn].length)]; while (taken[navn][m]) { m = this.variabler[navn][Math.floor(Math.random() * this.variabler[navn].length)]; this._internal_step_counter++; } taken[navn][m] = 1; o[navn] = m } r.push(o); l++ } return r; }*/ mo.prototype.buildBase = function() { var tabl = this.tabel_navn; var ll = this.tabel[tabl].length, l = 0; var r = []; var taken = {} for (var navn in this.variabler) { taken[navn] = {} } while (l < ll) { var o = {} o[tabl] = this.tabel[tabl][l]; for (var navn in this.variabler) { var m = this.variabler[navn][l]; /* while (taken[navn][m]) { m = this.variabler[navn][Math.floor(Math.random() * this.variabler[navn].length)]; this._internal_step_counter++; }*/ // taken[navn][m] = 1; o[navn] = m } r.push(o); o._sure = []; l++ } return r; } function rra(w){ return function(a){ return a!==w } } mo.prototype.solve = function() { var current = this.buildBase() var tabl = this.tabel_navn; this.regler.map(function(rule){ if(rule[tabl]){ var ruleTarget = current.filter(function(a){ return a[tabl] === rule[tabl]}).shift(); Object.keys(rule).filter(rra(tabl)).map(function(prop){ var currentHolder = current.filter(function(a){ return a[prop]===rule[prop]}).shift(); currentHolder[prop] = ruleTarget[prop] ruleTarget[prop] = rule[prop]; ruleTarget._sure.push(prop) }) } }) var rep = this.regler.filter(function(rule){ return !rule[tabl] }) //this.randoma("House"); var sat = false; var ii = 0; while (!sat && ii < 100000) { ii++ // console.log(this.regler.length); /* var ffs = this.regler.map(function(reg) { var t = false; try { t = eval(reg); } catch (e) { // console.log("EE",e); } return t; }) var longa = ffs.filter(function(a) { return a === true }).length; if(longa >= longest){ longest = longa; ffou = current; } */ /* if (longa === ffs.length) { sat = true; } else { current = this.randoma("House"); } */ if (ii % 100000 === 0) { console.log(ii); } } // console.log(longest); // console.log("TEST",t); // console.log(current); // console.log(this.regler); if (sat) { console.log("Found Solution:", current); } console.log(current); } mo.prototype.stats = function() { console.log("steps ", this._internal_step_counter) console.log(this); } var t = new mo() .addVariabelSet("Color", ["Green","Red", "White", "Yellow", "Blue" ]) .addVariabelSet("Nationality", ["Norwegian", "Dane", "Swede", "Englishman", "German"]) .addVariabelSet("Smokes", ["Prince", "Blend", "Dunhill", "BlueMasters", "PallMall"]) .addVariabelSet("Drinks", ["Water", "Bier", "Milk", "Tea", "Coffee"]) .addVariabelSet("Animals", ["Dogs", "Birds", "Cat", "Horses", "Fish"]) .addVariableTable("House", [1, 2, 3, 4, 5]) .addFunction("Neighbor", function() {}) .addFunction("Neighbor_Before", function() {}) .addFunction("Neighbor_After", function() {}) .addClause({ "Nationality": "Norwegian", "House": 1 }) .addClause({ "Drinks": "Milk", "House": 3 }) .addClause({ "Nationality": "Norwegian", "Neighbor": { "Color": "Blue" } }) .addClause({ "Nationality": "Englishman", "Color": "Red" }) .addClause({ "Smokes": "Blend", "Neighbor": { "Drinks": "Water" } }) .addClause({ "Nationality": "Swede", "Animals": "Dogs" }) .addClause({ "Nationality": "Dane", "Drinks": "Tea" }) .addClause({ "Color": "Green", "Neighbor_Before": { "Color": "White" } }) .addClause({ "Color": "Green", "Drinks": "Coffee" }) .addClause({ "Smokes": "PallMall", "Animals": "Birds" }) .addClause({ "Color": "Yellow", "Smokes": "Dunhill" }) .addClause({ "Smokes": "Blend", "Neighbor": { "Animals": "Cats" } }) .addClause({ "Animals": "Horses", "Neighbor": { "Smokes": "Dunhill" } }) .addClause({ "Smokes": "BlueMasters", "Drinks": "Bier" }) .addClause({ "Nationality": "German", "Smokes": "Prince" }) t.solve(); /* The Englishman lives in the red house.
The Swede keeps dogs.
The Dane drinks tea.
The green house is just to the left of the white one.
The owner of the green house drinks coffee.
The Pall Mall smoker keeps birds.
The owner of the yellow house smokes Dunhills.
The man in the center house drinks milk.
The Norwegian lives in the first house.
The Blend smoker has a neighbor who keeps cats.
The man who smokes Blue Masters drinks bier.
The man who keeps horses lives next to the Dunhill smoker.
The German smokes Prince.
The Norwegian lives next to the blue house.
The Blend smoker has a neighbor who drinks water.
*/ //console.log(t.randoma("House")); //t.stats(); /* var Colors = ["Red", "White", "Yellow", "Blue", "Green"], Nationality = ["Norwegian", "Dane", "Swede", "Englishman", "German"], Cigarettes = ["Prince", "Blend", "Dunhill", "Blue Masters", "Pall Mall"], Drinks = ["Water", "Bier", "Milk", "Tea", "Coffee"], Animals = ["Dogs", "Birds", "Cat", "Horses", "Fish"];*/