/* SEND+MORE=MONEY LP style in Picat. Ported from the Prolog code in http://www.inf.ed.ac.uk/teaching/courses/lp/slides/lp9.pdf slide 5 1) Without any extra constraints: about 1100 solutions with a lot of undistict solutions. 2) With constraints M > 0: got about 167 solutions 3.9s, 3) With all_are_different/1 constraint: 1 unique solution in 3.8s This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go ?=> solve_money([S,E,N,D], [M,O,R,E], [M,O,N,E,Y]), println([[S,E,N,D], [M,O,R,E], [M,O,N,E,Y]]), fail, % ensure unique solution nl. go => true. solve_money([S,E,N,D], [M,O,R,E], [M,O,N,E,Y]) => add([0,S,E,N,D], [0,M,O,R,E], [M,O,N,E,Y]), % hakank: added these two constraints M > 0, all_are_different([S,E,N,D,M,O,R,Y]). % hakank: simple all_different/0 LP style. % (don't want to use "all_different" here. all_are_different([]) ?=> true. all_are_different([H|T]) => not membchk(H,T), all_are_different(T). add(A, B, C) => add(A, B, C, 0). % add([], [], [], 0) ?=> true. add(A, B, C, D) ?=> A = [], B = [], C = [], D = 0. add([A|As], [B|Bs], [C|Cs], Carry) => between(0, 9, A), between(0, 9, B), add(As, Bs, Cs, NextCarry), C is (A + B + NextCarry) mod 10, Carry is (A + B + NextCarry) // 10.