/* Grade puzzle in Picat. From Prolog code http://www.anselm.edu/internet/compsci/faculty_staff/mmalita/HOMEPAGE/logic/gradepuzzle.txt """ From: http://cis2000.bizland.com/puzzles07.htm Solution: MM Five friends in the sixth form took the same combination of A- level subjects. Each obtained a different grade in each subject taken, and no two students had the same grade in the same subject. From the information given below, can you work out the full details of each student's results? - Andrew outscored Bridget in Physics, and Neil in Math. - Wendy was the only girl to get a "C" grade, but she managed no "A" grades.. - The pupil with an "E" in Math gained a "B" in Chemistry, but was not awarded a "C" in Physics. - Paul's Physics grade was a "D" and his highest grade was a "C". - The "B" in Math did not go to the same student as the "E" in Physics. - Bridget's best result was in Chemistry, but her Math grade was lower than Paul's What are the obtained grade for each subject by each student? ------------ Solution. Andrew had A in Math, C in Chemistry and B in Physics. Bridget had D in Math, A in Chemistry and E in Physics. Neil had E in Math, B in Chemistry and A in Physics. Paul had C in Math, E in Chemistry and D in Physics. Wendy had B in Math, D in Chemistry and C in Physics. Solution in Prolog. | ?- start(I). [[andrew,a,c,b],[bridget,d,a,e],[neil,e,b,a],[paul,c,e,d],[wendy,b,d,c]] """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go ?=> start(S), println(S), fail, nl. go => true. /* S= [[Name,Math,Chemistry,Physics],[Name,Math,Chemistry,Physics],..] */ grades(Grades) => Grades = [a,b,c,d,e]. start(S) => S=[[andrew,Am,Ac,Ap],[bridget,Bm,Bc,Bp],[neil,Nm,Nc,Np],[paul,Pm,Pc,Pp],[wendy,Wm,Wc,Wp]], % Paul's Physics grade was a "D" Pp=d, % no two students had the same grade in the same subject set_equal([Ac,Nc,Pc,Bc,Wc],[a,b,c,d,e]), set_equal([Am,Nm,Pm,Bm,Wm],[a,b,c,d,e]), set_equal([Ap,Np,Pp,Bp,Wp],[a,b,c,d,e]), % Each obtained a different grade in each subject taken set([Am,Ac,Ap]), set([Nm,Nc,Np]), set([Pm,Pc,Pp]), set([Bm,Bc,Bp]), set([Wm,Wc,Wp]), % and his(Paul) highest grade was a "C" best([Pm,Pc,Pp],c), % Andrew outscored Bridget in Physics, and Neil in Math best([Ap,Bp],Ap),best([Am,Nm],Am), % Wendy was the only girl to get a "C" grade, but she managed no "A" grades.. not member(c,[Bm,Bc,Bp]), % Brigdet )was the other girl) and she has no C not member(a,[Wm,Wc,Wp]), % Wendy has no A % Bridget's best result was in Chemistry best([Bm,Bc,Bp],Bc), % but her Math grade was lower than Paul's best([Pm,Bm],Pm), % The "B" in Math did not go to the same student as the "E" in Physics member([_,b,_,Y],S),Y!=e, % The pupil with an "E" in Math gained a "B" in Chemistry, member([_,e,b,Gradep],S), % but was not awarded a "C" in Physics Gradep!=c. /* best/2 finds the best grade ?-best([b,c,d,a],R). R=b */ % best(L,First) => R= sort(L),R=[First|_]. best(L,First) =>sort(L)=[First|_]. /* set/1 tests if a list has no duplicates | ?- set([b,a,a]). no | ?- set([a,b,c]). yes */ set([]) => true. set([H|T]) => not member(H,T),set(T). % set(L) => L = L.remove_dups(). /* set_equal/2 finds all permutations | ?- set_equal(I,[a,b,c]). I = [a,b,c] ; ... I = [c,b,a] ; */ % set_equal([],[]). % set_equal([H|T],L):- member(H,L),remove(H,L,R),set_equal(T,R). set_equal(P1,P2) => permutation(P1,P2).