summaryrefslogtreecommitdiff
path: root/lang/prolog/1.8.7/src/puzzle
blob: 648beb6381361cef310983df286dfda3c61f561d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
                {Solution: 9,5,6,7,0,8,2} 
puzzle:- repeat, permute ((9,8,7,6,5,2,0), SENDMORY), 
                 write (SENDMORY), 
                 puzzle (SENDMORY, SEND, MORE, MONEY), 
                 elan (line), 
                 write (SEND), write (+), 
                 write (MORE), write (=), 
                 write (MONEY). 
 
puzzle([S,E,N,D,O,R,Y], SEND, MORE, MONEY):- 
  SEND   IS  ((S * 10 + E) * 10 + N) * 10 + D, 
  MORE   IS  ((10 + O) * 10 + R) * 10 + E, 
  MONEY  IS  (((10 + O) * 10 + N) * 10 + E) * 10 + Y, 
  MONEY  IS  SEND + MORE. 
 
permute ([], []). 
permute ([E|X], Z):- permute (X, Y), insert (E, Y, Z). 
 
insert  (E, X, [E|X]). 
insert  (E, [F|X], [F|Y]):- insert (E, X, Y). 
 
repeat. 
repeat:- repeat.