/*
Number of runs in a sequence in Picat.
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 = 6,
X = new_list(N),
X :: 1..N,
R :: 0..N,
runs(R,X),
R #= 3,
solve(X ++ [R]),
println(x=X),
println(r=R),
nl.
go2 =>
X = [1,2,3,1,1,1],
N = X.length,
R :: 0..N,
runs(R,X),
solve(X ++ [R]),
println(x=X),
println(r=R),
nl.
%
% runs(NRuns, X)
%
% The number of runs in X is the number of switches + 1.
% (A switch is where X[I-1] #!= X[I], i.e. RunsIX[I] #= 0.)
%
runs(NRuns, X) =>
N = length(X),
RunsIx = new_list(N),
RunsIx :: 0..1,
foreach(I in 2..N)
(RunsIx[I] #= 1) #<=> (X[I-1] #= X[I])
end,
RunsIx[1] #= 1,
% number of runs
NRuns #= 1 + sum([RunsIx[I] #= 0 : I in 1..N]).