0

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.

false
  • 10,264
  • 13
  • 101
  • 209
sweets
  • 9
  • 1
  • 1
    Can't you just implement it as SANTA=CLAUS+XMAS? – jnmonette Oct 26 '20 at 18:31
  • No, unfortunately we have to subtract. – sweets Oct 26 '20 at 19:04
  • We can help you with your code if you write it and it doesn't work. I suggest that. Meanwhile... – Topological Sort Oct 28 '20 at 13:28
  • Write it out in English. Here's a start (not committing at this point to whether I should start at the right side or the left): CarryInRightColumn*10+A-S is S. CarryInNextToRightColumn*10+T-U is A+CarryInRightColumn. And so on. It may help to draw it out on paper with blanks for the carries. – Topological Sort Oct 28 '20 at 13:31
  • 1
    Thank you for your suggestions! I was able to implement it using left to right subtraction since it made accounting for the "borrowing" easier. – sweets Oct 29 '20 at 22:02

0 Answers0