/* One off digit problem in Picat. From Richard Wiseman http://richardwiseman.wordpress.com/2012/10/19/its-the-friday-puzzle-182/ """ in the equation: 43 + 57 = 207 every digit is precisely one way from its true value. What is the correct sum? """ 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 = 2+2+3, % The incorrect numbers: 43, 57, and 207 G = [4,3, 5,7, 2,0,7], % decision variables X = new_list(N), X :: 0..9, X12 #= X[1]*10+X[2], X34 #= X[3]*10+X[4], X58 #= X[5]*100+X[6]*10+X[7], X12 + X34 #= X58, % one off foreach(I in 1..N) abs(G[I]-X[I]) #= 1 end, solve(X), println("orig: 43 + 57 = 207"), printf("sol : %d + %d = %d\n",X12, X34, X58), println(sum=X58), fail, nl. go => true.