/****************************************************************/ Solutions in B-Prolog for the 2011 Prolog Programming Contest http://users.ugent.be/~tschrijv/ppc/ppc2011_probs.pdf Team members: Salvador Abreu, Ulrich Neumerkel, and Neng-Fa Zhou ****************************************************************/ %% chicken.pl % sample query ?-chicken(3). chicken(N):- NR is 5+2*(N-1), NC is 7*2*(N-1)+6, new_array(A,[NR,NC]), place_u(A,2*(N-1)+1,1,N), (N mod 2=:=0->place_c2(A,1,7*(N-1)+1);place_c1(A,1,7*(N-1)+1)), place_d(A,3,7*(N-1)+7,N), foreach(R in 1..NR, (foreach(C in 1..NC, [E], (E@=A[R,C],(var(E)->write(' ');format("~s",[E])))),nl)). place_u(A,R,C,I):-I=<1,!. place_u(A,R,C,I):- place_cb(A,R,C), R1 is R-2, C1 is C+7, I1 is I-1, place_u(A,R1,C1,I1). place_d(A,R,C,I):-I=<1,!. place_d(A,R,C,I):- place_bc(A,R,C), R1 is R+2, C1 is C+7, I1 is I-1, place_d(A,R1,C1,I1). place_c1(A,R,C):- place_s(A,R,C, " \\\\ "), place_s(A,R+1,C," (o>"), place_s(A,R+2,C,"\\\\_//)"), place_s(A,R+3,C," \\_/_)"), place_s(A,R+4,C,"___|__"). place_c2(A,R,C):- place_s(A,R,C, " // "), place_s(A,R+1,C," "), place_s(A,R+2,C,"\\\\_//) "), place_s(A,R+3,C," \\_/_)|"), place_s(A,R+4,C,"___|__|"). place_bc(A,R,C):- place_s(A,R,C, " // "), place_s(A,R+1,C," =1, I1==1, J1==1, I1==1, J1==1, I1==1, J1==1, I1==1, J1==1, I1==1, J1==1, I1==1, J1==1, I1==1, J1==1, I1==1, J1=true;true), W is max(In+De,Ne). nw(X,Js,Ss,Path,C,W):- (member(street(X,Y),Ss); member(street(Y,X),Ss)), \+ member(Y,Path), C1 is C-1, nw(Y,Js,Ss,[X|Path],C1,W1), (member(joint(X,Ne,In,De),Js)->true;true), W is max(W1+In+De,Ne). t(N):- war([joint(m,5,5,5),joint(p,10,5,5),joint(s,5,1,1)], [street(m,s),street(p,s)],N). %% plumber.pl % sample query ?-t. plumber(Grid, Ps) :- length(Grid,N), TagPs @= [(P,Tag) : P in Ps,[Tag]], foreach(I in 1..N, J in 1..N, place(Grid,I,J,N,TagPs)). place(Grid,I,J,N,Ps):- place_piece(Grid,I,J,Ps,P), (I==1->no_up(P); I==N->no_down(P); true), (J==1->no_left(P); J==N->no_right(P); true), I1 is I+1, (I1= place_piece(Grid,I1,J,Ps,Pd), match(P,Pd,down); true), J1 is J+1, (J1= place_piece(Grid,I,J1,Ps,Pr), match(P,Pr,right); true). place_piece(Grid,I,J,Ps,P):- P @= Grid[I,J], (var(P)->member((P,Tag),Ps),var(Tag),Tag=1;true). match(pipe(X,_,_,_),pipe(_,_,X,_),down). match(pipe(_,_,_,X),pipe(_,X,_,_),right). no_up(pipe(no,_,_,_)). no_right(pipe(_,no,_,_)). no_down(pipe(_,_,no,_)). no_left(pipe(_,_,_,no)). t :- Grid = [[_,_],[pipe(yes,yes,no,no),_]], Pieces = [pipe(yes,no,no,yes),pipe(no,yes,yes,no),pipe(no,no,yes,yes)], plumber(Grid, Pieces), writeln(Grid), Grid = [[pipe(no,yes,yes,no),pipe(no,no,yes,yes)], [pipe(yes,yes,no,no),pipe(yes,no,no,yes)]].