Crypt-arithmetic puzzle help: SANTA-CLAUS=XMAS

206 views Asked by At

I'm trying to solve the crypt-arithmetic puzzle: SANTA-CLAUS=XMAS in ECLIPSE Prolog. I am using the puzzle "SEND+MORE=MONEY" as a guide to do so. My problem that I'm not sure how to implement the "borrowing" part of subtraction. In addition, there's a carry over which can be added to the next terms as you continue adding. But in subtraction, we have to borrow and subtract a tens from the number you borrow from. Below is the code for the addition puzzle which I am using as a guide:

solve([S,E,N,D,M,O,R,Y]) :-
    car(M), M > 0, car(C100),
    dig(S), S > 0,
    M is (S+M+C100) // 10, O is (S+M+C100) mod 10,
    dig(E), car(C10),
    N is (E+O+C10) mod 10, C100 is (E+O+C10) // 10,
    dig(R), car(C1),
    E is (N+R+C1) mod 10, C10 is (N+R+C1) // 10,
    dig(D),
    Y is (D+E) mod 10, C1 is (D+E) // 10,
    all_diff([S,E,N,D,M,O,R,Y]). 
    
car(0). car(1).

dig(0). dig(1). dig(2). dig(3). dig(4). 
dig(5). dig(6). dig(7). dig(8). dig(9).

all_diff([]).
all_diff([N|L]) :- not member(N,L), all_diff(L).

member(N,[N|L]). 
member(N,[M|L]) :- member(N,L).

I just need help understanding how to implement borrowing in subtraction.

0

There are 0 answers