% Run using the command
% dlv problem1.txt -filter=seating

#const n=6.

person(1..n).
chair(1..n).

% -----------------------------------------------------
% Test problem

married(1, 2).
married(3, 4).

dislikes(1, 3).
dislikes(1, 4).
dislikes(1, 5).
dislikes(2, 4).
dislikes(2, 5).
dislikes(2, 6).
dislikes(3, 5).
dislikes(3, 6).

% End of test problem
% -----------------------------------------------------

% married is mutual relations 
%married(X, Y) :- married(Y, X), X != Y. 

% Instruction:
% Use the predicate seated(P, C) to say that person P is seating on chair number C

%%% GENERATE candidate solutions

seated(P, C) | -seated(P, C) :- person(P), chair(C). 

% Make sure every person has assigned a chair  
has_seated(P) :- seated(P, C). 

:- person(P), not has_seated(P). 

% Make sure every chair has a person 
has_person(C) :- seated(P, C). 
:- chair(C), not has_person(C).

% One seat can only assign to one person 
-seated(P1, C) :- seated(P2,C), person(P1), P1 != P2.  

% One chair can only assign to one person 
-seated(P, C1) :- seated(P, C2), chair(C1), C1 != C2. 

%%% TEST whether a candidate solution is a real solution or not

%%% add next to constraint 
next_to(C1, C2) :- chair(C1), C2 = C1 + 1, C2 <= n. 
 
next_to(C1, C2) :- chair(C1), C2 = C1 - 1, C2 > 0.

%%% edge case: chair 1 next to chair 6
next_to(1, n). 
%%% edge case: chair 6 next to chair 1
next_to(n, 1). 

% chair X is not next chair Y, if there is no reason to belive X next to Y
-next_to(X, Y) :- chair(X), chair(Y), not next_to(X, Y).  

%%% add married constraint to seating 
-seated(P1, C1) :- seated(P2, C2), -next_to(C1, C2), married(P1, P2), P1 != P2.   

%%% add dislike constraint to seating
-seated(P1, C1) :- seated(P2, C2), next_to(C1, C2), dislikes(P1, P2), P1 != P2. 


% -----------------------------------------------------
% For easy display

% seating(P1, P2, P3, P4, P5, P6) says that person P1 sits on chair 1, P2 on chair 2, etc.

seating(P1, P2, P3, P4, P5, P6) :- seated(P1, 1), seated(P2, 2), seated(P3, 3), seated(P4, 4), seated(P5, 5), seated(P6, 6).






