/* Shopping puzzle in Picat. From Martin Chlond Integer Programming Puzzles: http://www.chlond.demon.co.uk/puzzles/puzzles4.html, puzzle nr. 10. Source: M Kraitchik, Mathematical Recreations(p37), Dover This model was inspired by the AMPL model created by Martin Chlond. http://www.chlond.demon.co.uk/puzzles/sol4s10.html Solution: x = [33,17,15] y = [6,10,30] W: [0,0,1] [0,1,0] [1,0,0] This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => M = 3, % Husbands:Arthur, Bernard, Charles % Wifes : Ann, Barbara, Cynthia % w[i,j]=1 if husband i married to wife j, 0 otherwise W = new_array(M,M), W :: 0..1, X = new_list(M), % number of articles bought by husband i X :: 1..100, Y = new_list(M), % number of books (and price) bought by son j Y :: 1..100, % Each husband has one wife foreach(I in 1..M) sum([W[I,J] : J in 1..M]) #= 1 end, % Each wife has one husband foreach(J in 1..M) sum([W[I,J] : I in 1..M]) #= 1 end, % Arthur has bought 23 more articles than Barbara X[1] #= Y[2] + 23, % Bernard has bought 11 more articles than Ann X[2] #= Y[1] + 11, % each husband has spent $63 more than his wife foreach(I in 1..M) X[I]*X[I] #= sum([W[I,J]*Y[J]*Y[J]+63 : J in 1..M]) end, Vars = W.vars ++ X ++ Y, solve(Vars), println(x=X), println(y=Y), println("W:"), foreach(Row in W) println(Row.to_list) end, nl, fail, nl.