/* Brain Twister #74: Tripple Digits in Picat. https://enigmaticcode.wordpress.com/2025/05/22/braintwister-74-triple-digits/ """ From New Scientist #3544, 24th May 2025 [ https://www.newscientist.com/article/mg26635443-500-braintwister-74-triple-digits/ ] Using the digits 1, 2, 3, 4, 5, 6, 7, 8 and 9 once each, create three numbers - each with three digits — and find their sum. (a) What is the smallest total it is possible to make in this way? (b) What is the second-smallest total? (c) Is it possible to get a total of exactly 1000? If not, how close can you get? """ Solutions: a = [{1,4,7,2,5,8,3,6,9},{1,4,7},{2,5,8},{3,6,9},774] a = 774 b = 783 c = 999 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import cp. main => go. go ?=> time(p(a, Xa,Aa,Ba,Ca,Za)), println(a=[Xa,Aa,Ba,Ca,Za]), println(a=Za), time(println(b=findall(Zb2,p(b,_Xb,_Ab,_Bb,_Cb,Zb2)).sort_remove_dups.take(2).second)), time(p(c,_Xc,_Ac,_Bc,_Cc,Zc)), println(c=Zc), nl. go => true. % Convert list -> number t(L,V) => V #= 100*L[1] + 10*L[2] + L[3]. p(Q,X,A,B,C,Z) => N = 9, X = new_array(N), X :: 1..N, all_different(X), t(X[1..3],A), t(X[4..6],B), t(X[7..9],C), increasing([A,B,C]), % symmetry breaking Z #= A + B + C, if Q == a then % (a) What is the smallest total it is possible to make in this way? % Note: This could be solved together with b), but this is faster solve($[degree,updown,min(Z)],X) elseif Q == b then % (b) What is the second-smallest total? solve($[degree,updown],X) else % (c) Is it possible to get a total of exactly 1000? If not, how close can you get? T #= abs(Z - 1000), solve($[degree,updown,min(T)],X) end.