houses([
        house(first, _, _ ),
        house(second, _, _ ),
        house(third, _, _ ),
        house(forth, _, _ )
]).

left_of(A, B, [A, B | _]).
left_of(A, B, [_ | Y]) :- left_of(A, B, Y).
 
right_of(A, B, [B, A | _]).
right_of(A, B, [_ | Y]) :- right_of(A, B, Y).
 
next_to(A, B, [A, B | _]).
next_to(A, B, [B, A | _]).
next_to(A, B, [_ | Y]) :- next_to(A, B, Y).
 
mymember(X, [X|_]).
mymember(X, [_|Y]) :- mymember(X, Y).
 
not_in(_,[]).
not_in(X,[X|_]):-!,fail.
not_in(X,[_|A]):-not_in(X,A).
 
not_trainer(H):-mymember(house(_,roman,hunter),H).
not_trainer(H):-mymember(house(_,roman,doctor),H).
not_trainer(H):-mymember(house(_,roman,painter),H).
not_trainer(H):-mymember(house(_,roman,trainer),H),!,fail.
 
print_houses([]).
print_houses([A|B]) :-
        write(A), nl,
        print_houses(B).

false(X):-X,!,fail.
false(_).

solve(H):-houses(H),
               next_to(house(_,_,painter),
                       house(_,_,trainer),H),
                       
               next_to(house(_,_,doctor),
                       house(_,_,painter),H),
                       
               left_of(house(_,_,hunter),
                       house(_,_,doctor),H),
                       
               false(next_to(house(_,_,trainer),
                             house(_,_,hunter),H)),
                                                         
               right_of(house(_,_,painter),
                        house(_,semen,_),H),
                                       
               next_to(house(_,semen,_),
                       house(_,nikolay,_),H),
                       
               false(next_to(house(_,artur,_),
                             house(_,roman,_),H
                             )),
                             
               not_trainer(H),
                                  
               mymember(house(_,roman,_), H),
               mymember(house(_,artur,_), H),
               mymember(house(_,semen,_), H),
               mymember(house(_,nikolay,_), H),                                             
                                                                        
               print_houses(H).
 
:-solve(_).