/* Consecutive digits puzzle in Picat. Martin Gardner (Kuly 1971) """ A B C D D C B A + . . . . --------- 1 2 3 0 0 ABCD are four consecutive digits in increasing order, DBCA are the same four in decreasing order. The four dots represent the same four digits in an unknown order. If the sum is 12300, what number is represented by the four dots. (From W.T. Williams and G.H. Savage, 'The Strand Problems Book'). """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> N = 4, FD = [A,B,C,D], FD :: 1..9, Dots = new_list(N), Dots :: 1..9, DotsNum :: 1000..9999, ABCD #= 1000*A + 100*B + 10*C + D, DCBA #= 1000*D + 100*C + 10*B + A, all_different(FD), increasing(FD), all_different(Dots), to_num(Dots,10,DotsNum), 12300 #= ABCD + DCBA + DotsNum, % dots consist of the the digits A, B, C, and D contains_all(FD,Dots), % contains(A, Dots), % contains(B, Dots), % contains(C, Dots), % contains(D, Dots), Vars = FD ++ Dots, solve(Vars), println(fd=FD), println(dots=Dots), printf("%d + %d + %d = %d\n", ABCD,DCBA,DotsNum,12300), nl, fail, nl. go => true. contains(E, A) => sum([A[I] #= E : I in 1..A.len]) #>= 1. contains_all(L, A) => foreach(D in 1..L.len) sum([A[I] #= L[D] : I in 1..A.len]) #>= 1 end. % % converts a number Num to/from a list of integer List given a base Base % to_num(List, Base, Num) => Len = length(List), Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]).