/* Global constraint among_diff_0 in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Camong_diff_0.html """ NVAR is the number of variables of the collection VARIABLES that take a value different from 0. Example (3, <0, 5, 5, 0, 1>) The among_diff_0 constraint holds since exactly 3 values of the collection of values <0, 5, 5, 0, 1> are different from 0. """ Implements: - among_diff_0/2 - among_diff_c/3 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..N, NVar :: 0..N, % X = [0,5,5,0,1], among_diff_0(NVar, X), % among_diff_c(NVar, X, 0), Vars = X ++ [NVar], solve(Vars), println(x=X), println(nvar=NVar), nl, fail, nl. go => true. among_diff_0(NVar, X) => NVar #= sum([X[I] #!= 0 : I in 1..X.len]). % generalized version: % number of variables that are different from a value c among_diff_c(NVar, X, C) => NVar #= sum([X[I] #!= C : I in 1..X.len]).