/* Enigma problem (counting pennies) in Picat. Problem formulation from http://www.vbforums.com/showthread.php?p=2108230 """ Joe asked Penny to think of four different postive digits and add up the 24 different 4-digit numbers that could be made using the four digits. Then he asked her to subtract just one of the 4-digits numbers from the total and write down the new total. When asked what number she had written down, Penny replied: "122**0". Joe didnt quite hear the 4th or 5th digits. But he was able to work out the number Penny had subtracted. What was that number? """ Note: From the comments in the thread it seems to that it is a Enigma problem from New Scientist of 23 July 2005. I don't know what Enigma number it is, though. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp,util. main => go. go ?=> N = 4, M = 24, % The 24 permutations of 4 items (as a template) Perms = permutations(1..N), % the four digits chosen Digits = new_list(N), Digits :: 0..9, % the 24 numbers of the four digits X = new_list(M), X ::1000..9999, DigitsSum :: 100000..999999, % sum of the 24 numbers % array representation of the sum DigitsSumA = new_list(6), DigitsSumA :: 0..9, DigitsSumA = [1,2,2,_,_,0], all_different(Digits), Digits[1] #> 0, % get all the 24 permutations of the 4 digits foreach(I in 1..M) to_num([Digits[Perms[I,J]] : J in 1..N], 10, X[I]) end, % the sought sum is from subtracting one of the 24 numbers from the sum of x sum([ DigitsSum #= sum(X) - X[I] : I in 1..M ]) #>= 1, % convert the sum to array and use it as a template to_num(DigitsSumA, 10, DigitsSum), % symmetry breaking increasing(Digits), Vars = X ++ DigitsSumA ++ Digits, solve($[degree,split],Vars), println(x=X), println(digits=Digits), println(digitssum=DigitsSum), println(digitssuma=DigitsSumA), nl, fail, nl. go => true. % % 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]).