% Lab 2
% 18/09/2008

% Exercise 1

athlete(marios,70).
athlete(homer,100).
athlete(bart,50).
athlete(scrat,10).

lightweight(X) :- athlete(X,W), W =< 63.
middleweight(X) :- athlete(X,W), W >= 63, W =< 80.
heavyweight(X) :- athlete(X,W), W >= 81.

% Exercise 2

input1(g1,1).
input1(g2,Out_G1) :- output(g1,Out_G1).
input1(g3,1).
input1(g4,1).
input1(g5,Out_G3) :- output(g3,Out_G3).

input2(g1,0).
input2(g2,1).
input2(g3,Out_G1) :- output(g1,Out_G1).
input2(g4,0).
input2(g5,Out_G4) :- output(g4,Out_G4).

xorgate(g1).
xorgate(g2).
andgate(g3).
andgate(g4).
orgate(g5).

%rand(1,0,0).
%rand(0,1,0).
%rand(1,1,1).

rand(I1,I2,R) :- I1 =:= 0, R is 0.
rand(I1,I2,R) :- I1 =:= 1, I2 =:= 1, R is 1.
rand(I1,I2,R) :- I1 =:= 1, I2 =:= 0, R is 0.

ror(I1,I2,R) :- X is I1 + I2, X > 0, R is 1.
ror(I1,I2,R) :- X is I1 + I2, X =:= 0, R is 0.

rxor(I1,I2,R) :- I1 =\= I2, R is 1.
rxor(I1,I2,R) :- I1 =:= I2, R is 0.

output(G,O) :- andgate(G), input1(G,I1), input2(G,I2), rand(I1,I2,O).
output(G,O) :- orgate(G), input1(G,I1), input2(G,I2), ror(I1,I2,O).
output(G,O) :- xorgate(G), input1(G,I1), input2(G,I2), rxor(I1,I2,O).