Skip to content
denys-duchier edited this page Sep 2, 2011 · 1 revision

Send+Most=Money is a variant of the famous cryptarithmetic problem where not only do we want to find a solution, but we want the best solution, i.e. the one where we get the largest amount of money. Here is how to solve it with these bindings:

:- use_module(library(gecode)).

%   S E N D
% + M O S T
% ---------
% M O N E Y
send_most_money(Solution,Amount) :-
	Space := space,
	Letters := intvars(Space,8,0,9),
	[S,E,N,D,M,O,T,Y] = Letters,
	Space += rel(M,'IRT_NQ',0),
	Space += rel(S,'IRT_NQ',0),
	Space += distinct(Letters),
	C = [1000, 100, 10, 1,
	     1000, 100, 10, 1,
	     -10000, -1000, -100, -10, -1],
	X = [S,E,N,D,
	     M,O,S,T,
	     M,O,N,E,Y],
	Space += linear(C, X, 'IRT_EQ', 0),
	Money := intvar(Space,0,99999),
	Space += linear([10000,1000,100,10,1],[M,O,N,E,Y],'IRT_EQ',Money),
	Space += maximize(Money),
	Space += branch(Letters,'INT_VAR_SIZE_MIN','INT_VAL_MIN'),
	SolSpace := search(Space),
	Solution := val(SolSpace,Letters),
	Amount := val(SolSpace,Money).
Clone this wiki locally