SEND + MORE = MONEY #78

Closed
williamwsmith opened this Issue Sep 25, 2013 · 3 comments

2 participants

@williamwsmith

Tried to solve this classic problem just for fun in NOOLS. Goes off and never returns and Chrome says it died. Based it on a CLIPS version found here:

http://commentsarelies.blogspot.com/2006/11/send-more-money-in-clips.html

I formulated this NOOLS file below and "asserted" all the Digits (0..9). Any ideas?
Thought it might be a good baseline test.

rule SendMoreMoney {
when {
s : Digit s.value != 0 { value : s1 };
e : Digit e.value != s1 { value: e1};
n : Digit n.value != s1 && n.value != e1 { value : n1 };
d : Digit d.value != s1 && d.value != e1 && d.value != n1 { value : d1 };
m : Digit m.value != s1 && m.value != e1 && m.value != n1 && m.value != d1 && m.value != 0 { value : m1 };
o : Digit o.value != s1 && o.value != e1 && o.value != n1 && o.value != d1 && o.value != m1 { value : o1 };
r : Digit r.value != s1 && r.value != e1 && r.value != n1 && r.value != d1 && r.value != m1 && r.value != o1 { value : r1 };
y : Digit y.value != s1 && y.value != e1 && y.value != n1 && y.value != d1 && y.value != m1 && y.value != o1 && y.value != r1 { value : y1 };
test : Digit s11000 + e1100 + n110 + d1
+ m1
1000 + o1100 + r110 + e1 ==
m110000 + o11000 + n1100 + e110 + y1;

}
then {
    emit('pathDone', {sout : s, eout : e, nout : n, dout : d, mout : m, oout: o, rout : r, yout : y});
}

}

@doug-martin
C2FO member

Try

//sendMoreMoney.nools

rule SendMoreMoney {
    when {
        s : Number s != 0;
        e : Number e != s;
        n : Number n != s && n != e;
        d : Number d != s && d != e && d != n;
        m : Number m != 0 && m != s && m != e && m != n && m != d;
        o : Number o != s && o != e && o != n && o != d && o != m;
        r : Number r != s && r != e && r != n && r != d && r != m && r != o;
        y : Number y != s && y != e && y != n && y != d && y != m && y != o && y != r
            && (s*1000 + e*100 + n*10 + d + m*1000 + o*100 + r*10 + e) == (m*10000 + o*1000 + n*100 + e*10 + y);
    }
    then {
        console.log('pathDone', {s : s, e : e, n : n, d : d, m : m, o: o, r : r, y : y});
        console.log((s*1000 + e*100 + n*10 + d + m*1000 + o*100 + r*10 + e));
        console.log((m*10000 + o*1000 + n*100 + e*10 + y))
    }
}

//sendMoreMoney.js
"use strict";

var nools = require("nools"),
    flow = nools.compile(__dirname + "/sendMoreMoney.nools");

var start = new Date(),
    session;
console.log("starting");
(session = flow.getSession(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)).match().then(function () {
    console.log("%dms", +(new Date()) - start);
    session.dispose();
});
@doug-martin
C2FO member

This does use quite a bit of memory because of all the cross products that are created 362,880 locally it ran in ~20 seconds assertion and firing the rules.

@williamwsmith

Appreciate the response. I will give a try. I was very happy to stumble apon NOOLS. Have been looking for something like this in javascript for many years.

@doug-martin doug-martin added a commit to doug-martin/nools that referenced this issue Oct 14, 2013
@doug-martin doug-martin v0.2.0
* Nools now supports true modify!!!
   * This is a major leap forward for `nools` opening the door for more complex actions and expressions in the rules.
* Added support from for `from` conditions in the `rhs`.
* Fixed issue [#81](C2FO#81).
* Fixed issue [#82](C2FO#82).
* Added new `sudoku` web example.
* Added [Send More Money](http://en.wikipedia.org/wiki/Verbal_arithmetic) benchmark see [#78](C2FO#78).
e58883d
@doug-martin doug-martin referenced this issue Oct 14, 2013
Merged

v0.2.0 #83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment