ein_work2.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. function EV() {
  2. this.__e = {};
  3. }
  4. EV.prototype.__e = {};
  5. EV.prototype.emit = function(n) {
  6. var that = this;
  7. var args = [].slice.apply(arguments, [1]);
  8. (this.__e[n] || []).map(function(lis) {
  9. lis.map(function(fn) {
  10. fn.apply(that, args)
  11. })
  12. })
  13. return this;
  14. }
  15. EV.prototype.on = function(n) {
  16. var args = [].slice.apply(arguments, [1]);
  17. this.__e[n] = this.__e[n] || [];
  18. this.__e[n].push(args);
  19. return this;
  20. }
  21. function inherits(ctor, superCtor) {
  22. var Obj = function() {};
  23. Obj.prototype = superCtor.prototype;
  24. ctor.prototype = new Obj
  25. }
  26. function permutates(xs) {
  27. var ret = [];
  28. for (var i = 0; i < xs.length; i = i + 1) {
  29. var rest = permutates(xs.slice(0, i).concat(xs.slice(i + 1)));
  30. if (!rest.length) {
  31. ret.push([xs[i]])
  32. } else {
  33. for (var j = 0; j < rest.length; j = j + 1) {
  34. ret.push([xs[i]].concat(rest[j]))
  35. }
  36. }
  37. }
  38. return ret;
  39. }
  40. function mo() {
  41. this.variabler = {};
  42. this.tabel = {};
  43. this.tableHead = [];
  44. this.tableName = "";
  45. this.regler = [];
  46. this._internal_step_counter = 0;
  47. this.rmat = [];
  48. this.funcs = {};
  49. this.rulas = [];
  50. }
  51. inherits(mo, EV)
  52. mo.prototype.addVariabelSet = function(navn, arr) {
  53. this.variabler[navn] = arr;
  54. return this;
  55. }
  56. mo.prototype.addVariableTable = function(navn, arr) {
  57. this.tableHead = arr;
  58. this.tableName = navn;
  59. return this;
  60. }
  61. mo.prototype.addFunction = function(name, func) {
  62. this.funcs[name] = func;
  63. return this;
  64. }
  65. mo.prototype.addClause = function() {
  66. var args = Array.prototype.slice.apply(arguments, []);
  67. while (args.length) {
  68. var arg = args.shift();
  69. var props = Object.keys(arg);
  70. var propid1, propid2;
  71. if (this.variabler[props[0]]) {
  72. propid1 = Object.keys(this.variabler).indexOf(props[0]);
  73. }
  74. if (this.variabler[props[1]]) {
  75. propid2 = Object.keys(this.variabler).indexOf(props[1]);
  76. } else {}
  77. var arr = []
  78. if (propid1 > -1 && propid2 > -1) {
  79. val1 = this.variabler[props[0]].indexOf(arg[props[0]])
  80. val2 = this.variabler[props[1]].indexOf(arg[props[1]])
  81. arr = ["check", propid1, propid2, val1, val2, [props[0], arg[props[0]], props[1], arg[props[1]]].join(" ")];
  82. } else {
  83. var lookfor = props[props.length - 1];
  84. if (this.funcs[lookfor]) {
  85. arr = this.funcs[lookfor].apply(this, [arg])
  86. }
  87. }
  88. }
  89. this.rulas.push(arr);
  90. return this;
  91. }
  92. mo.prototype.build = function(tabl) {
  93. var r = [];
  94. var arr = [];
  95. var pertable = []
  96. for (var navn in this.variabler) {
  97. pertable.push(permutates((this.variabler[navn]).map(function(a, i) {
  98. return i
  99. })));
  100. }
  101. return pertable;
  102. }
  103. mo.prototype.checkn = function(prop1, prop2, val1, val2) {
  104. return this.current[this.sos[prop1]][val1] == val2
  105. }
  106. mo.prototype.check = function(prop1, prop2, val1, val2) {
  107. var i;
  108. // console.log(prop1,this.sos[prop1], prop2, this.current[this.sos[prop1]],this.current[this.sos[prop2]],val1,val2)
  109. for (i = 0; i < 5; i++) {
  110. if (this.current[this.sos[prop1]][i] == val1) {
  111. // console.log("II,",i,this.current[this.sos[prop1]][i],"=",val1,"&", this.current[this.sos[prop2]][i],"=",val2 );
  112. var p;
  113. p = (this.current[this.sos[prop2]][i] == val2)
  114. return p;
  115. }
  116. }
  117. return false;
  118. }
  119. mo.prototype.checkLeft = function(prop1, prop2, val1, val2) {
  120. var i;
  121. for (i = 0; i < 4; i++) {
  122. if (this.current[this.sos[prop1]][i] == val1) {
  123. var p;
  124. p = (this.current[this.sos[prop2]][i + 1] == val2)
  125. return p;
  126. }
  127. }
  128. return false;
  129. }
  130. mo.prototype.checkRight = function(prop1, prop2, val1, val2) {
  131. var i;
  132. for (i = 1; i < 5; i++) {
  133. if (this.current[this.sos[prop1]][i] == val1) {
  134. var p;
  135. p = (this.current[this.sos[prop2]][i - 1] == val2)
  136. return p;
  137. }
  138. }
  139. return false;
  140. }
  141. mo.prototype.checkBoth = function(prop1, prop2, val1, val2) {
  142. return this.checkLeft(prop1, prop2, val1, val2) || this.checkRight(prop1, prop2, val1, val2)
  143. }
  144. function intersection() {
  145. var result = [];
  146. var lists;
  147. if (arguments.length === 1) {
  148. lists = arguments[0];
  149. } else {
  150. lists = arguments;
  151. }
  152. for (var i = 0; i < lists.length; i++) {
  153. var currentList = lists[i];
  154. for (var y = 0; y < currentList.length; y++) {
  155. var currentValue = currentList[y];
  156. if (result.indexOf(currentValue) === -1) {
  157. var existsInAll = true;
  158. for (var x = 0; x < lists.length; x++) {
  159. if (lists[x].indexOf(currentValue) === -1) {
  160. existsInAll = false;
  161. break;
  162. }
  163. }
  164. if (existsInAll) {
  165. result.push(currentValue);
  166. }
  167. }
  168. }
  169. }
  170. return result;
  171. }
  172. mo.prototype.solve = function() {
  173. var self = this;
  174. self.current = this.build("House").shift();
  175. var sa = Object.keys(this.variabler);
  176. self.sos = sa.map(function(a) {
  177. return 0; //self.current.length - 1; //Math.floor(Math.random()*self.current.length-1);
  178. })
  179. self.subspace = self.current.length;
  180. self.varspace = sa.length;
  181. self.space = Math.pow(self.subspace, self.varspace - 1);
  182. var iteration = 0;
  183. var maxi = 0;
  184. var ioa = -1;
  185. var soso = [];
  186. var sat = false;
  187. self.rulas = self.rulas; //slice(0, 9);
  188. /* self.rulas.sort(function(a, b) {
  189. return a[1] > b[1] ? -1 : a[1] < b[1] ? 1 : a[2] > b[2] ? 1 : a[2] < b[2] ? -1 : 0
  190. })
  191. */
  192. // console.log(self.rulas);
  193. // return false;
  194. // først find variabler som bruges i samme opslag.
  195. var tas = {}
  196. var firsta = [];
  197. self.rulas.map(function(ru) {
  198. console.log(ru);
  199. if (ru[1] == ru[2]) {
  200. firsta.push(ru);
  201. }
  202. })
  203. self.sos = self.sos.map(function(a) {
  204. return 0
  205. })
  206. var osa = -1
  207. console.log(firsta);
  208. var cands = [];
  209. for (var x = 0; x < firsta.length; x++) {
  210. self.sos = self.sos.map(function(a) {
  211. return 0
  212. })
  213. var t = firsta[x];
  214. var ca = [];
  215. for (var i = 0; i < self.subspace; i++) {
  216. var ff = false;
  217. self.sos[t[1]] = i
  218. ff = self[t.slice(0, 1)[0]].apply(self, t.slice(1))
  219. if (ff) {
  220. ca.push(i);
  221. }
  222. }
  223. cands.push([t[1], ca]);
  224. }
  225. console.log(cands);
  226. // var inta=intersection.apply(this,cands.map(function(a){ return a[1]}));
  227. self.sos = self.sos.map(function(a) {
  228. return 0
  229. })
  230. var cc = cands.length;
  231. var ticks = [0, 0, 0, 0, 0];
  232. var ic = 0;
  233. var iomax = self.rulas.length;
  234. var ioa = 0;
  235. for (var i0 = 0; i0 < cands[0][1].length; i0++) {
  236. if (cands[1]) {
  237. for (var i1 = 0; i1 < cands[1][1].length; i1++) {
  238. if (cands[2]) {
  239. for (var i2 = 0; i2 < cands[2][1].length; i2++) {
  240. if (cands[3]) {
  241. } else {
  242. var st = [0, 1, 2, 3, 4]
  243. self.sos[cands[0][0]] = cands[0][1][i0]
  244. self.sos[cands[1][0]] = cands[1][1][i1]
  245. self.sos[cands[2][0]] = cands[2][1][i2]
  246. st.splice(st.indexOf(cands[0][0]), 1);
  247. st.splice(st.indexOf(cands[1][0]), 1);
  248. st.splice(st.indexOf(cands[2][0]), 1);
  249. for (var i3 = 0; i3 < self.subspace; i3++) {
  250. for (var i4 = 0; i4 < self.subspace; i4++) {
  251. ic++
  252. self.sos[st[0]] = i3
  253. self.sos[st[1]] = i4
  254. var io = 0;
  255. var ff = true;
  256. while (io < iomax && ff) {
  257. ff = false;
  258. var t = self.rulas[io];
  259. ff = self[t.slice(0, 1)[0]].apply(self, t.slice(1))
  260. if (ff) {
  261. io++;
  262. }
  263. }
  264. if (io > ioa) {
  265. ioa = io;
  266. var tt = self.rulas.map(function(t) {
  267. return [self[t.slice(0, 1)[0]].apply(self, t.slice(1))].concat(t);
  268. })
  269. self.emit("part", ic, io, ioa, iomax, self.sos.join(" "), tt.map(function(a) {
  270. return a.join(" ")
  271. }))
  272. }
  273. if (ioa >= iomax) {
  274. return self.sos;
  275. }
  276. if (ic % 1000000 === 0) {
  277. console.log("X2", ic, io, ioa, iomax, self.sos[cands[0][0]], self.sos[cands[1][0]], self.sos[cands[2][0]], i3, i4);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. } else {
  284. var st = [0, 1, 2, 3, 4]
  285. self.sos[cands[0][0]] = cands[0][1][i0]
  286. self.sos[cands[1][0]] = cands[1][1][i1]
  287. // self.sos[cands[2][0]] = cands[2][1][i2]
  288. st.splice(st.indexOf(cands[0][0]), 1);
  289. st.splice(st.indexOf(cands[1][0]), 1);
  290. // st.splice(st.indexOf(cands[2][0]), 1);
  291. for (var i2 = 0; i2 < self.subspace; i2++) {
  292. for (var i3 = 0; i3 < self.subspace; i3++) {
  293. for (var i4 = 0; i4 < self.subspace; i4++) {
  294. ic++
  295. self.sos[st[0]] = i2
  296. self.sos[st[1]] = i3
  297. self.sos[st[2]] = i4
  298. var io = 0;
  299. var ff = true;
  300. while (io < iomax && ff) {
  301. ff = false;
  302. var t = self.rulas[io];
  303. ff = self[t.slice(0, 1)[0]].apply(self, t.slice(1))
  304. if (ff) {
  305. io++;
  306. }
  307. }
  308. if (io > ioa) {
  309. ioa = io;
  310. var tt = self.rulas.map(function(t) {
  311. return [self[t.slice(0, 1)[0]].apply(self, t.slice(1))].concat(t);
  312. })
  313. self.emit("part", ic, io, ioa, iomax, self.sos.join(" "), tt.map(function(a) {
  314. return a.join(" ")
  315. }))
  316. }
  317. if (ioa >= iomax) {
  318. return self.sos;
  319. }
  320. if (ic % 1000000 === 0) {
  321. console.log("X3", ic, io, ioa, iomax, ":>", self.sos[cands[0][0]], self.sos[cands[1][0]], i2, i3, i4);
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. return false;
  331. // console.log("II",inta);
  332. /* for (var i = 0; i < self.subspace; i++) {
  333. self.sos[0] = i;
  334. var mio = 0;
  335. var ff = true;
  336. var iomax = 1; // firsta.length;
  337. while (mio < iomax && ff) {
  338. var t = firsta[mio];
  339. ff = self[t.slice(0, 1)[0]].apply(self, t.slice(1))
  340. if (ff) {
  341. mio++;
  342. }
  343. }
  344. if (mio > 0) {
  345. cands.push(i);
  346. } else {}
  347. }
  348. self.sos[0] = 0;
  349. */
  350. //cands = [105,81]
  351. var iomax = self.rulas.length;
  352. while (!sat && cands.length > 0) {
  353. var cand1 = cands.shift();
  354. self.sos[0] = cand1;
  355. iteration = 0;
  356. while (!sat && iteration < self.space) {
  357. var io = 0;
  358. var ff = true;
  359. while (io < iomax && ff) {
  360. var t = self.rulas[io];
  361. ff = self[t.slice(0, 1)[0]].apply(self, t.slice(1))
  362. if (ff) {
  363. io++;
  364. }
  365. }
  366. if (io > ioa) {
  367. soso.push([].concat(self.sos));
  368. ioa = io;
  369. var tt = self.rulas.map(function(t) {
  370. return [self[t.slice(0, 1)[0]].apply(self, t.slice(1))].concat(t);
  371. })
  372. console.log(iteration, io, ioa, iomax, self.sos.join(" "), tt.map(function(a) {
  373. return a.join(" ")
  374. }));
  375. }
  376. if (io >= iomax) {
  377. sat = true;
  378. // console.log((iteration).toFixed(0) + " ", ioa, "::", self.sos.join(" "), soso.join(" | "));
  379. } else {
  380. self.sos[1] = iteration % self.subspace;
  381. self.sos[2] = Math.floor(iteration / self.subspace) % self.subspace;
  382. self.sos[3] = Math.floor(iteration / (self.subspace * self.subspace)) % self.subspace;
  383. self.sos[4] = Math.floor(iteration / (self.subspace * self.subspace * self.subspace)) % self.subspace;
  384. // self.sos[0] = Math.floor(iteration / (self.subspace * self.subspace * self.subspace * self.subspace)) % self.subspace;
  385. }
  386. if (iteration % 10000000 === 0) {
  387. self.emit("status", (iteration / (self.space / 100)).toFixed(2), ioa + ":: " + cands.length + ":: " + self.sos.join(" "))
  388. }
  389. // console.log(iteration, self.sos.join(" "));
  390. iteration++;
  391. }
  392. }
  393. /* while (!sat && iteration < self.space) {
  394. var io = 0;
  395. var ff = true;
  396. while (io < iomax && ff) {
  397. var t = self.rulas[io];
  398. ff = self[t.slice(0, 1)[0]].apply(self, t.slice(1))
  399. if (ff) {
  400. io++;
  401. }
  402. }
  403. if (io > ioa) {
  404. soso.push([].concat(self.sos));
  405. ioa = io;
  406. var tt = self.rulas.map(function(t) {
  407. return [self[t.slice(0, 1)[0]].apply(self, t.slice(1))].concat(t);
  408. })
  409. console.log(iteration, io, ioa, iomax, self.sos.join(" "), tt.map(function(a) {
  410. return a.join(" ")
  411. }));
  412. }
  413. if (io >= iomax) {
  414. sat = true;
  415. // console.log((iteration).toFixed(0) + " ", ioa, "::", self.sos.join(" "), soso.join(" | "));
  416. } else {
  417. self.sos[4] = iteration % self.subspace;
  418. self.sos[1] = Math.floor(iteration / self.subspace) % self.subspace;
  419. self.sos[2] = Math.floor(iteration / (self.subspace * self.subspace)) % self.subspace;
  420. self.sos[3] = Math.floor(iteration / (self.subspace * self.subspace * self.subspace)) % self.subspace;
  421. self.sos[0] = Math.floor(iteration / (self.subspace * self.subspace * self.subspace * self.subspace)) % self.subspace;
  422. }
  423. if (iteration % 10000000 === 0) {
  424. self.emit("status", (iteration / (self.space / 100)).toFixed(2), self.sos.join(" "))
  425. }
  426. iteration++;
  427. }*/
  428. if (sat) {
  429. console.log("SOLUTION FOUND");
  430. }
  431. var tt = self.rulas.map(function(t) {
  432. return [self[t.slice(0, 1)[0]].apply(self, t.slice(1))].concat(t);
  433. })
  434. console.log(iteration, self.sos.join(" "), tt.map(function(a) {
  435. return a.join(" ")
  436. }));
  437. self.printit()
  438. }
  439. mo.prototype.printit = function(vals) {
  440. var self = this;
  441. var vals = vals || self.sos;
  442. var lpad = function(s, l) {
  443. while (s.length < l) {
  444. s = " " + s
  445. };
  446. return s;
  447. }
  448. var rpad = function(s, l) {
  449. while (s.length < l) {
  450. s = s + " "
  451. };
  452. return s;
  453. }
  454. var s = [];
  455. s.push([" ", 1, 2, 3, 4, 5].map(function(a) {
  456. return lpad("" + a, 12)
  457. }).join(" "));
  458. Object.keys(self.variabler).map(function(k, ia) {
  459. s.push([k, 0, 1, 2, 3, 4].map(function(a, io) {
  460. return a == k ? rpad(k + ":" + vals[ia], 20) : lpad("" + self.variabler[k][self.current[vals[ia]][a]], 12)
  461. }).join(" "))
  462. // s.push( )
  463. })
  464. console.log(s.join("\n") + "\n")
  465. }
  466. mo.prototype.stats = function() {
  467. console.log("steps ", this._internal_step_counter)
  468. console.log(this);
  469. }
  470. var t = new mo()
  471. .addVariabelSet("Nationality", ["Norwegian","Dane", "Swede", "Englishman", "German"])
  472. .addVariabelSet("Color", ["Red", "White", "Yellow", "Blue", "Green"])
  473. .addVariabelSet("Smokes", ["Prince", "Blend", "Dunhill", "BlueMasters", "PallMall"])
  474. .addVariabelSet("Drinks", [ "Bier","Water", "Coffee","Milk", "Tea" ])
  475. .addVariabelSet("Animals", ["Dogs", "Birds", "Cats", "Horses", "Fish"])
  476. .addVariableTable("House", [0, 1, 2, 3, 4])
  477. .addFunction("Neighbor", function(arg) {
  478. var prop1 = Object.keys(arg)[0];
  479. var prop2 = Object.keys(arg[Object.keys(arg)[1]])[0]
  480. var val1 = arg[prop1];
  481. var val2 = arg["Neighbor"][prop2];
  482. var propid1 = Object.keys(this.variabler).indexOf(prop1)
  483. var propid2 = Object.keys(this.variabler).indexOf(prop2)
  484. var valid1 = (this.variabler[prop1]).indexOf(val1)
  485. var valid2 = (this.variabler[prop2]).indexOf(val2)
  486. return ["checkBoth", propid1, propid2, valid1, valid2, [prop1, val1, "Besides", prop2, val2].join(" ")]
  487. })
  488. .addFunction("Neighbor_Before", function(arg) {
  489. var prop1 = Object.keys(arg)[0];
  490. var prop2 = Object.keys(arg[Object.keys(arg)[1]])[0]
  491. var val1 = arg[prop1];
  492. var val2 = arg["Neighbor_Before"][prop2];
  493. var propid1 = Object.keys(this.variabler).indexOf(prop1)
  494. var propid2 = Object.keys(this.variabler).indexOf(prop2)
  495. var valid1 = (this.variabler[prop1]).indexOf(val1)
  496. var valid2 = (this.variabler[prop2]).indexOf(val2)
  497. return ["checkLeft", propid1, propid2, valid1, valid2, [prop1, val1, "Before", prop2, val2].join(" ")]
  498. })
  499. .addFunction("Neighbor_After", function() {
  500. var prop1 = Object.keys(arg)[0];
  501. var prop2 = Object.keys(arg[Object.keys(arg)[1]])[0]
  502. var val1 = arg[prop1];
  503. var val2 = arg["Neighbor_After"][prop2];
  504. var propid1 = Object.keys(this.variabler).indexOf(prop1)
  505. var propid2 = Object.keys(this.variabler).indexOf(prop2)
  506. var valid1 = (this.variabler[prop1]).indexOf(val1)
  507. var valid2 = (this.variabler[prop2]).indexOf(val2)
  508. return ["checkRight", propid1, propid2, valid1, valid2, [prop1, val1, "After", prop2, val2].join(" ")]
  509. })
  510. .addFunction("House", function(arg) {
  511. var prop1 = Object.keys(arg)[0];
  512. var prop2 = prop1
  513. var propid1 = Object.keys(this.variabler).indexOf(prop1)
  514. var val1 = arg[prop1];
  515. var val2 = arg["House"];
  516. var valid1 = (this.variabler[prop1]).indexOf(val1)
  517. return ['checkn', propid1, propid1, valid1, val2, [prop1, val1, "House", val2].join(" ")]
  518. })
  519. .addClause({
  520. "Nationality": "Norwegian",
  521. "House": 0
  522. })
  523. .addClause({
  524. "Drinks": "Milk",
  525. "House": 2
  526. })
  527. .addClause({
  528. "Nationality": "Norwegian",
  529. "Neighbor": {
  530. "Color": "Blue"
  531. }
  532. })
  533. .addClause({
  534. "Nationality": "Englishman",
  535. "Color": "Red"
  536. })
  537. .addClause({
  538. "Smokes": "Blend",
  539. "Neighbor": {
  540. "Drinks": "Water"
  541. }
  542. })
  543. .addClause({
  544. "Nationality": "Swede",
  545. "Animals": "Dogs"
  546. })
  547. .addClause({
  548. "Nationality": "Dane",
  549. "Drinks": "Tea"
  550. })
  551. .addClause({
  552. "Color": "Green",
  553. "Drinks": "Coffee"
  554. })
  555. .addClause({
  556. "Smokes": "PallMall",
  557. "Animals": "Birds"
  558. })
  559. .addClause({
  560. "Color": "Yellow",
  561. "Smokes": "Dunhill"
  562. })
  563. .addClause({
  564. "Nationality": "German",
  565. "Smokes": "Prince"
  566. })
  567. .addClause({
  568. "Smokes": "Blend",
  569. "Neighbor": {
  570. "Animals": "Cats"
  571. }
  572. })
  573. .addClause({
  574. "Animals": "Horses",
  575. "Neighbor": {
  576. "Smokes": "Dunhill"
  577. }
  578. })
  579. .addClause({
  580. "Smokes": "BlueMasters",
  581. "Drinks": "Bier"
  582. })
  583. .addClause({
  584. "Color": "Green",
  585. "Neighbor_Before": {
  586. "Color": "White"
  587. }
  588. })
  589. /* .addClause("$House('Nationality','Norwegian')","eq","$House().first")
  590. .addClause("$House('Drinks','Milk')","eq","$House().mid")
  591. .addClause("$House('Nationality','Englishman')","eq","$House('Color','Red')")
  592. .addClause("Math.abs($House('Smokes','Blend') - $House('Drinks','Water'))===1")
  593. */
  594. /*.addClause(0,"Norwegian",-1,-1,-1,-1)
  595. .addClause(2,-1,-1,-1,"Milk",-1)
  596. .addClause(-1,"Englishman","Red",-1,-1,-1,-1)
  597. .addClause(-1,"N Norwegian","Blue",-1,-1,-1,-1)
  598. .addClause("Math.abs($House('Nationality','Norwegian') - $House('Color','Blue'))===1")
  599. .addClause("Math.abs($House('Smokes','Blend') - $House('Drinks','Water'))===1")
  600. .addClause("$House('Nationality','Swede')===$House('Animals','Dogs')")
  601. .addClause("$House('Nationality','Dane')===$House('Drinks','Tea')")
  602. .addClause("($House('Color','Green')+1)===$House('Color','White')")
  603. .addClause("$House('Color','Green')===$House('Drinks','Coffee')")
  604. .addClause("$House('Smokes','PallMall')===$House('Animals','Birds')")
  605. .addClause("$House('Color','Yellow')===$House('Smokes','Dunhill')")
  606. .addClause("Math.abs($House('Smokes','Blend')-$House('Animals','Cats'))===1")
  607. .addClause("$House('Smokes','BlueMasters')===$House('Drinks','Bier')")
  608. //.addClause("Math.abs($House('Animals','Horses')-$House('Smokes','Dunhill'))===1")
  609. //.addClause("$House('Nationality','German')===$House('Smokes','Prince')")
  610. */
  611. /*
  612. The Englishman lives in the red house.<br>
  613. The Swede keeps dogs.<br>
  614. The Dane drinks tea.<br>
  615. The green house is just to the left of the white one.<br>
  616. The owner of the green house drinks coffee.<br>
  617. The Pall Mall smoker keeps birds.<br>
  618. The owner of the yellow house smokes Dunhills.<br>
  619. The man in the center house drinks milk.<br>
  620. The Norwegian lives in the first house.<br>
  621. The Blend smoker has a neighbor who keeps cats.<br>
  622. The man who smokes Blue Masters drinks bier.<br>
  623. The man who keeps horses lives next to the Dunhill smoker.<br>
  624. The German smokes Prince.<br>
  625. The Norwegian lives next to the blue house.<br>
  626. The Blend smoker has a neighbor who drinks water.<br>
  627. */
  628. //console.log(t.randoma("House"));
  629. //t.stats();
  630. t.on("status", function(per, found) {
  631. console.log("PER", per, found);
  632. })
  633. t.on("part", function() {
  634. console.log("PART", arguments);
  635. })
  636. var result = t.solve();
  637. console.log(t.printit(result));
  638. /*
  639. var Colors = ["Red", "White", "Yellow", "Blue", "Green"],
  640. Nationality = ["Norwegian", "Dane", "Swede", "Englishman", "German"],
  641. Cigarettes = ["Prince", "Blend", "Dunhill", "Blue Masters", "Pall Mall"],
  642. Drinks = ["Water", "Bier", "Milk", "Tea", "Coffee"],
  643. Animals = ["Dogs", "Birds", "Cat", "Horses", "Fish"];*/