/* A grouping of integers 1 through 15 in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 118. A grouping of integers 1 through 15 See how elegantly the integers 1 through 15 can be arranged in 5 arithmetic progres- sions of 3 integers: |1 |4 |2 |3 |11 d5 = 7 |8 d4 = 5|9 d3 = 4|6 d2 = 2|5 d1 = 1|12 |15 |14 |10 |7 |13 For example, 8 - 1 = 15 - 8 = 7, so the difference d5 is 7 for the first triplet. Now, keeping the first triplet, make four new triplets, still with d = 5, 4, 2, and 1. On your own, try arranging the integers from 1 through 15 with other values of d. (puzzle 112 from Kordemsky (1992)) """ With the assumptions that the differences (D) should be distinct (and increasing) there is one more solutions. The two solutions are thus x = {{3,4,5},{9,11,13},{6,10,14},{2,7,12},{1,8,15}} d = [1,2,4,5,7] x = {{11,12,13},{3,5,7},{2,6,10},{4,9,14},{1,8,15}} d = [1,2,4,5,7] where the second one is the given from the example. If the constraint of the differences is just that they should be increasing but not necessarily distinct, then there are in total 738 solutions, among them: x = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}} d = [1,1,1,1,1] x = {{2,4,6},{3,5,7},{10,12,14},{9,11,13},{1,8,15}} d = [2,2,2,2,7] x = {{13,14,15},{8,10,12},{2,4,6},{1,5,9},{3,7,11}} d = [1,2,2,4,4] 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 = 5, M = 3, X = new_array(N,M), X :: 1..N*M, D = new_list(N), D :: 1..N*M, all_different(X.vars), increasing_strict(D), % symmetry and distinct % increasing(D), % just symmetry breaking foreach(I in 1..N) increasing(X[I]), foreach(J in 1..M-1) X[I,J] + D[I] #= X[I,J+1] end end, Vars = X.vars ++ D, solve(Vars), println(x=X), println(d=D), nl, fail, nl.