/* Global constraint ith_pos_different_from_0 in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Cith_pos_different_from_0.html """ ith_pos_different_from_0 (ITH, POS, VARIABLES) Purpose POS is the position of the ITHth non-zero item of the sequence of variables VARIABLES. Example (2, 4, 〈3, 0, 0, 8, 6〉) The ith_pos_different_from_0 constraint holds since 4 corresponds to the position of the 2th non-zero item of the sequence 3 0 0 8 6. """ 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 = 5, X = new_list(N), X :: 0..8, Ith :: 1..N, Pos :: 1..N, X = [3,0,0,8,6], % Pos #= 4, % Ith #= 2, ith_pos_different_from_0(Ith, Pos, X), Vars = X ++ [Ith,Pos], solve(Vars), println(x=X), println(ith=Ith), println(pos=Pos), nl, fail, nl. go => true. ith_pos_different_from_0(Ith, Pos, X) => N = X.len, sum([X[I] #!= 0 #/\ Pos #= I #/\ Ith #= sum([X[J] #!= 0 : J in 1..I]) : I in 1..N]) #>= 1.