/* Devil's word in Picat. Translate each character in a word to ASCII value and then try to sum its values (either positive or negative) to a total. E.g. "hakankjellerstrand" and total 666 gives 359 solutions. Here is the first: +104 +97 +107 +97 +110 +107 +106 -101 +108 +108 -101 +114 +115 +116 -114 -97 -110 -100 Also, see http://www.hakank.org/data_snooping/666.html Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => Name = [h,a,k,a,n, k,j,e,l,l,e,r,s,t,r,a,n,d], convert_list(Name, Res), printf("%s", Name), nl, Total #= 666, writeln(total=Total), devils_word(Res, SignedRes,Total), solve([ff],SignedRes), writeln([Total, SignedRes]), nl. % % Let's see how many solutions there are. % go2 => Name = [h,a,k,a,n, k,j,e,l,l,e,r,s,t,r,a,n,d], convert_list(Name, Res), printf("%s", Name), nl, Total #= 666, L = findall(SignedRes, $(devils_word(Res, SignedRes,Total), solve([ff],SignedRes)) ), printf("There are %d solutions.\n", L.length), nl. % Let us go crazy and set Total free as well go3 => Name = [h,a,k,a,n], % Name = [k,j,e,l,l,e,r,s,t,r,a,n,d], convert_list(Name, Res), printf("%s", Name), nl, Total :: 1..sum([abs(R) : R in Res]), L = findall([Total,SignedRes], $(devils_word(Res, SignedRes,Total), solve([ff],SignedRes))), foreach([Total2,W] in L) writeln([total=Total2,w=W]) end, printf("There are %d solutions.\n", L.length), nl. scalar_product(A, X, Product) => Product #= sum([S : I in 1..A.length, S #= A[I]*X[I]]). % % Signs is the list of [-1,1] for which % List[i]*Signs[i] = Total % % SignedRes is the resulting list of % SignedRes[i] = List[i]*Signs[i] % devils_word(List, SignedRes, Total) => println($devils_word(List, SignedRes, Total)), Len = length(List), Signs = new_list(Len), writeln(here), Signs :: [-1,1], writeln(here2), scalar_product(List,Signs,Total), % create the resulting list of ASCII codes with +/- signs SignedRes = [ SR : {S,R} in zip(Signs,List), SR #= S*R]. % % convert a list of atoms to ASCII code % convert_list(List, Res) => Res = [I : El in List, I = ord(El)].