/****************************************************************/ Solutions in B-Prolog for the 2011 Prolog Programming Contest http://people.cs.kuleuven.be/~tom.schrijvers/Research/papers/ppc2010.pdf Team members: Salvador Abreu, Ulrich Neumerkel, and Neng-Fa Zhou ****************************************************************/ %% tartan.pl % sample query ?-tartan(5). tartan(N) :- M is 2*N-1, new_array(A,[M,M]), fill(A,N,1,M), foreach(I in 1..M, J in 1..M, [E], (E @= A[I,J], (var(E)->write(' ');format("~s",[E])), (J==M->nl;true))). fill(A,1,R0,_):-!, A[R0,R0] @= "*". fill(A,N,R0,Rn):- N mod 2=:= 0,!, foreach(J in R0..Rn, (A[R0,J] @= "*", A[Rn,J] @= "*")), N1 is N-1, NR0 is R0+1, NRn is Rn-1, fill(A,N1,NR0,NRn). fill(A,N,R0,Rn):- foreach(I in R0..Rn, (A[I,R0] @= "*", A[I,Rn] @= "*")), N1 is N-1, NR0 is R0+1, NRn is Rn-1, fill(A,N1,NR0,NRn). %% eric.pl % sample query: ?-eric(fork(treasury(10),treasury(20)),7,T). eric(C, N, T) :- eric(C, N, _, T), T>0. eric(treasury(T), N, 2, T) :- N>=2, !. eric(fork(L,R), N, NN, T) :- % both forks N1 is N-2, N1>=0, % 1 entry 1 exit eric(L, N1, NN1, T1), N2 is N1-NN1-1,N2>=0, % 1 extra guard eric(R, N2, NN2, T2), T is max(T1, T2), NN is NN1+NN2,!. eric(fork(L,_), N, NN, T) :- % left fork only N1 is N-2,N1>=0, % 1 entry 1 exit eric(L, N1, NN, T),!. eric(_, _, 0, 0). %% maglev.pl %% sample query: ?- maglev([station(a,b,[c,d]), station(b,z,[a,c,d])],a,z,Cost). :- table maglev(+,+,+,min). maglev(L,S,F,Cost) :- member(station(S,F,_),L),!,Cost=0. maglev(L,S,F,Cost) :- (member(station(S,N,Rs),L), maglev(L,N,F,Cost) ; append(L1,[station(S,N,Rs)],L2,L), append(Rs1,[R],Rs2,Rs), append(Rs1,[N],Rs2,NRs), % switch N and R, R becomes the preferred next station append(L1,[station(S,R,NRs)],L2,NL), maglev(NL,N,F,Cost1), Cost is Cost1+1). %% rqueens.pl % sample query ?-rqueens(5,Q). rqueens(N, Q) :- between(1,N,Q), rqueens1(N, Q),!. rqueens1(N, Q) :- length(L, N), L :: 0..N, new_array(B, [N,N]), count(0,L,#=, N-Q), foreach(I in 1..N, J in I+1..N, ((L[I] #\=0 #/\ L[J]#\=0) #=>(L[I] #\= L[J] #/\abs(L[I]-L[J]) #\= J-I))), labeling_ff(L), foreach(I in 1..N, [Qi],(Qi @= L[I],(Qi\=0->cover(B,Qi,I,N);true))), foreach(I in 1..N, J in 1..N, [Bij],(Bij @= B[I,J],nonvar(Bij))). cover(B,I,J,N):- foreach(K in 1..N,B[I,K] @= 1), foreach(K in 1..N,B[K,J] @= 1), foreach(I1 in 1..N, J1 in 1..N,(abs(I-I1) =:= abs(J-J1)->B[I1,J1] @= 1;true)). %% student.pl % sample query: ?-t(N). student(Program,Goal,N):- maxint(Max), between(0,Max,N), change(N,Program,Goal),!. change(0,Program,Init):-!,prov(Program,[Init]). change(N,Program,Init):- (Goal=Init; select_goal(Program,Goal)), change_goal(Goal), N1 is N-1, change(N1,Program,Init). select_goal([rule(_,Body)|_],Goal):- member(Goal,Body). select_goal([_|Rules],Goal):- select_goal(Rules,Goal). change_goal(Goal):- functor(Goal,_,Ari), between(1,Ari,I1), I11 is I1+1, between(I11,Ari,I2), arg(I1,Goal,A1), arg(I2,Goal,A2), setarg(I1,Goal,A2), setarg(I2,Goal,A1). prov(_Program,[]):-!. prov(Program,[G|Gs]):- member(Rule,Program), copy_term(Rule,Rule1), Rule1=rule(G,Body), append(Body,Gs,Gs1), prov(Program,Gs1). t(N):- student([rule(append([],L,L),[]), rule(append([X|Xs],Ys,[X|Zs]), [append(Xs,Ys,Zs)])], append([1],[2,3],[2,1,3]),N).