/* A magic triangle in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 111. A magic triangle I have placed the numbers 1, 2, and 3 at the vertices of a triangle. Arrange 4, 5, 6, 7, 8, and 9 along the sides of the triangle so that the numbers along each side add to 17. This is harder: without being told which numbers to place at the vertices, make a similar arrangement of the numbers from 1 through 9, adding up to 20 along each side. (Sev- eral solutions are possible.) (puzzle 22 from Kordemsky (1992)) (Fig. 11.4) """ Part 1 configuration: 1/a b i c h 2/d e f 3/g Part 1: 16 solutions 1 5 7 9 6 2 4 8 3 1 5 6 9 7 2 4 8 3 1 5 7 9 6 2 8 4 3 ... Part 2: 288 solutions 1 3 8 7 6 9 2 4 5 1 3 6 7 8 9 2 4 5 ... 9 7 2 3 4 1 8 6 5 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => N = 9, member(Part,1..2), Sum = cond(Part == 1,17,20), println([part=Part,sum=Sum]), X = new_list(N), X :: 1..N, all_different(X), if Part == 1 then X[1] #= 1, X[4] #= 2, X[7] #= 3 end, sum(X[1..4]) #= Sum, sum(X[4..7]) #= Sum, sum(X[7..9]) + X[1] #= Sum, solve(X), println(X), printf(" %d\n",X[1]), nl, printf(" %d %d\n",X[2],X[9]), nl, printf(" %d %d\n",X[3],X[8]), nl, printf(" %d %d %d %d\n",X[4],X[5],X[6],X[7]), nl, fail, nl.