/*
Book buy puzzle in Picat.
From Martin Chlond Integer Programming Puzzles:
http://www.chlond.demon.co.uk/puzzles/puzzles4.html, puzzle nr. 9.
Source: M Kraitchik, Mathematical Recreations(p37), Dover
"""
Four men, Peter and Paul and their sons Tom and Dick, buy books. When their
purchases are completed it turns out that each man has paid for each of his
books a number of dollars equal to the number of books he has bought. Each
family (father and son) has spent $65. Peter has bought one more book than
Tom, and Dick has bought only one book. Who is Dick's father?
"""
Answer: Peter is Dick's father.
This model was inspired by the AMPL model created by Martin Chlond.
http://www.chlond.demon.co.uk/puzzles/sol4s9.html
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 =>
M = 2,
% w = 1 if Peter is Tom's father, 0 otherwise
W :: 0..1,
% number of books (and price) bought by father i
X = new_list(M),
X :: 1..8,
% number of books (and price) bought by son j
Y = new_list(M),
Y :: 1..8,
Y[2] #= 1, % Dick buys one book
X[1] #= Y[1]+1, % Peter buys one more book than Tom
% each family spends $65
X[1]*X[1] + W*Y[1]*Y[1] + (1-W)*Y[2]*Y[2] #= 65,
X[2]*X[2] + (1-W)*Y[1]*Y[1] + W*Y[2]*Y[2] #= 65,
solve(X ++ Y ++ [W]),
writeln(w=W),
printf("%w is Dick's Father\n", cond(W == 1,"Paul","Peter")),
writeln(x=X),
writeln(y=Y),
nl.