/* Enigma 725 - Perfect Puzzle in Picat. From https://enigmaticcode.wordpress.com/2025/06/16/enigma-725-perfect-puzzle/ """ Enigma 725: Perfect puzzle From New Scientist #1880, 3rd July 1993 [link] P E R F E C T P U Z Z L E In this puzzle each letter represents a different digit, the same digit throughout. Each digit in the second line is the difference of the two digits above (that is, the larger take away the smaller). Find the value of PERFECT PUZZLE. """ Using CP: vars = [1,8,5,7,4,2,9,6,3] perfect = [4,8,2,5,8,1,9] = 4825819 puzzle = [4,6,3,3,7,8] = 463378 CPU time 0.0 seconds. Backtracks: 87 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. import util. main => go. go ?=> Vars = [C,E,F,L,P,R,T,U,Z], Vars :: 0..9, all_different(Vars), PERFECT = [P,E,R,F,E,C,T], PUZZLE = [P,U,Z,Z,L,E], foreach(I in 1..PERFECT.len-1) PUZZLE[I] #= abs(PERFECT[I]-PERFECT[I+1]) end, solve(Vars), println(vars=Vars), println(perfect=PERFECT=to_num(PERFECT)), println(puzzle=PUZZLE=to_num(PUZZLE)), nl, fail, nl. go => true. /* Using permutations: perfect = [4,8,2,5,8,1,9] = 4825819 puzzle = [4,6,3,3,7,8] = 463378 CPU time 1.407 seconds. Backtracks: 0 Without fail/0: 0.975s */ go2 ?=> Digits = 0..9, permutation([C,E,F,L,P,R,T,U,Z,_],Digits), PERFECT = [P,E,R,F,E,C,T], PUZZLE = [P,U,Z,Z,L,E], foreach(I in 1..PERFECT.len-1) PUZZLE[I] == abs(PERFECT[I]-PERFECT[I+1]) end, println(perfect=PERFECT=to_num(PERFECT)), println(puzzle=PUZZLE=to_num(PUZZLE)), nl, fail, nl. go2 => true. to_num(List) = Num => Base = 10, Len = length(List), Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]).