/*
Clock Triplet problem in Picat.
Problem formulation
http://www.f1compiler.com/samples/Dean 20Clark 27s 20Problem.f1.html
"""
Dean Clark's Problem (Clock Triplets Problem)
The problem was originally posed by Dean Clark and then presented
to a larger audience by Martin Gardner.
The problem was discussed in Dr. Dobbs's Journal, May 2004 in an article
by Timothy Rolfe. According to the article, in his August 1986 column for
Isaac Asimov's Science Fiction Magazine, Martin Gardner presented this problem:
Now for a curious little combinatorial puzzle involving the twelve
numbers on the face of a clock. Can you rearrange the numbers (keeping
them in a circle) so no triplet of adjacent numbers has a sum higher
than 21? This is the smallest value that the highest sum of a triplet
can have.
""
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import cp.
main => go.
go =>
N = 12,
Sum = 21,
P = 3, % number in each list to sum
L = findall(Xs,$clock_triplet(N, P, Sum, Xs)),
foreach(LL in L) writeln(LL) end,
writef("Sum: %d Number of solutions: %d\n",Sum,L.length).
go2 =>
N = 12,
% Sum = 21,
% checks if 21 really is the smallest number...
Sum :: 2..30,
indomain(Sum),
P = 3, % number in each list to sum
L = findall(Xs,$clock_triplet(N, P, Sum, Xs)),
% writeln(L),
% foreach(LL in L, writeln(LL)),
writef("Sum: %d Number of solutions: %d\n",Sum,L.length),
% Test the next number if no solution
if L.length == 0 then fail end.
%
% This is slighly more general version where
% P is the length of the tuples to sum
% and N is the length of Xs (the numbers in the "clock")
%
clock_triplet(N, P, Sum, Xs) =>
Xs = new_list(N),
Xs :: 1..N,
all_different(Xs),
% symmetry breaking
Xs[1] #= 1,
Xs[2] #> Xs[N],
foreach(I in 0..N)
sum([XI1 : K in 0..P-1, I1 = 1+((I+K) mod N), XI1 = Xs[I1]]) #=< Sum
end,
solve(Xs).