%
% Coins puzzle in ASP.
%
% Problem from
% Tony Hurlimann: "A coin puzzle - SVOR-contest 2007"
% http://www.svor.ch/competitions/competition2007/AsroContestSolution.pdf
%
% """
% In a quadratic grid (or a larger chessboard) with 31x31 cells, one should
% place coins in such a way that the following conditions are fulfilled:
% 1. In each row exactly 14 coins must be placed.
% 2. In each column exactly 14 coins must be placed.
% 3. The sum of the quadratic horizontal distance from the main diagonal
% of all cells containing a coin must be as small as possible.
% 4. In each cell at most one coin can be placed.
% The description says to place 14x31 = 434 coins on the chessboard each
% row containing 14 coins and each column also containing 14 coins.
% """
%
% Cf the LPL model:
% http://diuflx71.unifr.ch/lpl/GetModel?name=/puzzles/coin
%
%
% This was created by Hakan Kjellerstrand, hakank@bonetmail.com
% See also http://www.hakank.org/answer_set_programming/
%
#const n = 10. % 31.
#const c = 4. % 14.
values(0..1).
size(1..n).
% combinations of rows/columns are different
1 { x(Row, Col, Val) : values(Val) } 1 :- size(Row;Col).
% Checking (debugging) the column/row sums
% cols(Col, S) :- S = [ x(Row, Col, Val) : values(Val) : size(Row) = Val ], size(Col).
% rows(Row, S) :- S = [ x(Row, Col, Val) : values(Val) : size(Col) = Val ], size(Row).
c [ x(Row, Col, Val) : size(Row) : values(Val) = Val ] c :- size(Col).
c [ x(Row, Col, Val) : size(Col) : values(Val) = Val ] c :- size(Row).
% Object: minimize the quadratic horizontal distance
% #minimize [x(Row, Col, Val) : values(Val) = Val*#abs(Row-Col)*#abs(Row-Col) ].
% Alternative: using |...| instead of #abs()
#minimize [ x(Row, Col, Val) : values(Val) = Val*|Row-Col|*|Row-Col| ].