:- lib(fd).

solve(List) :-
	q1(List),
	q2(List),
	q3(List),
	q4(List),
	q5(List),
	q6(List),
	q7(List),
	q8(List),
	q9(List),
	q10(List).

% Question 1.  The first question whose answer is 'a' is:
q1(List) :-
	List=[First|_],
	q1cons(First,List).

% a: 4
q1cons(a,[X1,X2,X3,X4|_]) :- X1##a, X2##a, X3##a, X4=a.
% b: 3
q1cons(b,[X1,X2,X3|_]) :- X1##a, X2##a, X3=a.
%c: 2
q1cons(c,[X1,X2|_]) :- X1##a, X2=a.
% d: 1
q1cons(d,[X1|_]) :- X1=a.
% e: none of the above
q1cons(e,[X1,X2,X3,X4|_]) :- X1##a, X2##a, X3##a, X4##a.

% Question2.  The ONLY two questions with identical answers are:

% (Error in Fernandez and Hill's SRQ examples: neither the 50 var model   
% nor the 10 var model  enforce that the two questions are the ONLY two 
% consecutive  questions with identical answers!)   
q2(List) :-
	List=[_,Second|_],
	q2cons(Second,List).

% a: 3 and 4
q2cons(a,[X1,X2,X3,X4,X5,X6,X7,X8,X9,X10]) :-
	X2##X1, X3##X2, X4=X3, X5##X4, X6##X5, X7##X6, X8##X7, X9##X8, X10##X9.
% b: 4 and 5
q2cons(b,[X1,X2,X3,X4,X5,X6,X7,X8,X9,X10]) :-
	X2##X1, X3##X2, X4##X3, X5=X4, X6##X5, X7##X6, X8##X7, X9##X8, X10##X9.
% c: 5 and 6
q2cons(c,[X1,X2,X3,X4,X5,X6,X7,X8,X9,X10]) :-
	X2##X1, X3##X2, X4##X3, X5##X4, X6=X5, X7##X6, X8##X7, X9##X8, X10##X9.
% d: 6 and 7
q2cons(d,[X1,X2,X3,X4,X5,X6,X7,X8,X9,X10]) :-
	X2##X1, X3##X2, X4##X3, X5##X4, X6##X5, X7=X6, X8##X7, X9##X8, X10##X9.
% e: 7 and 8
q2cons(e,[X1,X2,X3,X4,X5,X6,X7,X8,X9,X10]) :-
	X2##X1, X3##X2, X4##X3, X5##X4, X6##X5, X7##X6, X8=X7, X9##X8, X10##X9.

% Question 3. The next question with answer a is:
q3(List) :-
	List=[_,_,X3|After],
	q3cons(X3,After).

% a: 4
q3cons(a,[a|_]).
% b: 5
q3cons(b,[X4,a|_]) :- X4##a.
% c: 6
q3cons(c,[X4,X5,a|_]) :- X4##a, X5##a.
% d: 7
q3cons(d,[X4,X5,X6,a|_]) :- X4##a, X5##a, X6##a.
% e: 8
q3cons(e,[X4,X5,X6,X7,a|_]) :- X4##a, X5##a, X6##a,X7##a.

% Question 4. The first even numbered question with answer b is:
q4(List) :-
	List=[_,_,_,X4|_],
	q4cons(X4,List).

% a: 2
q4cons(a,[_,b,_,_|_]).
% b: 4
q4cons(b,[_,X2,_,b|_]) :- X2##b.
% c: 6
q4cons(c,[_,X2,_,X4,_,b|_]) :- X2##b, X4##b.
% d: 8
q4cons(d,[_,X2,_,X4,_,X6,_,b|_]) :- X2##b, X4##b, X6##b.
% e: 10
q4cons(e,[_,X2,_,X4,_,X6,_,X8,_,b]) :- X2##b, X4##b, X6##b, X8##b.

% Question 5.  The only odd-numbered question with answer c is;
q5(List) :-
	List=[_,_,_,_,X5|_],
	q5cons(X5,List).

% a: 1
q5cons(a,[c,_,X3,_,X5,_,X7,_,X9,_]) :- X3##c, X5##c, X7##c, X9##c.
% b: 3
q5cons(b,[X1,_,c,_,X5,_,X7,_,X9,_]) :- X1##c, X5##c, X7##c, X9##c.
% c: 5
q5cons(c,[X1,_,X3,_,c,_,X7,_,X9,_]) :- X1##c, X3##c, X7##c, X9##c.
% d: 7
q5cons(d,[X1,_,X3,_,X5,_,c,_,X9,_]) :- X1##c, X3##c, X5##c, X9##c.
% e: 9
q5cons(e,[X1,_,X3,_,X5,_,X7,_,c,_]) :- X1##c, X3##c, X5##c, X7##c.

% Question 6.  A question with answer d:
q6(List) :-
	length(Before,5), length(After,4),
	append(Before,[X6|After],List),
	q6cons(X6,Before,After).

% a: comes before this one, but not after this one
q6cons(a,Before,After) :-
	member(d,Before), nonmember(d,After).
% b: comes after this one, but not before this one
q6cons(b,Before,After) :-
	nonmember(d,Before), member(d,After).
% c: comes before and after this one
q6cons(c,Before,After) :-
	member(d,Before), member(d,After).
% d: does not occur at all(!)
q6cons(d,Before,After) :- 
	d##d,
	nonmember(d,Before), 
	nonmember(d,After).
% None of the above (encoding follows Fernandez and Hill's)
q6cons(e,_Before,_After) :- 
	e #= d.


% Question 7.  The last question whose answer is e is:
q7(List) :-
	reverse(List,RevList),
	q7cons(RevList).
% a: 5
q7cons([X10,X9,X8,a,X6,e|_]) :-
	X10##e, X9##e, X8##e, X6##e.
% b: 6
q7cons([X10,X9,X8,b,e|_]) :-
	X10##e, X9##e, X8##e.
% c: 7
q7cons([X10,X9,X8,e|_]) :- 
	c#=e, X10##e, X9##e, X8##e.
% d: 8
q7cons([X10,X9,e,d|_]) :- X10##e, X9##e.
% e: 9
q7cons([X10,e,_,e|_]) :- X10##e.

% Question 8. The number of questions whose answers are consonants is:
q8(List) :-
	List=[_,_,_,_,_,_,_,X8|_],
	q8cons(X8,List).

% a: 7
q8cons(a,List) :-
	cntconsonants(List,7).
% b: 6
q8cons(b,List) :-
	cntconsonants(List,6).
% c: 5
q8cons(c,List) :-
	cntconsonants(List,5).
% d: 4
q8cons(d,List) :-
	cntconsonants(List,4).
% e: 3
q8cons(e,List) :-
	cntconsonants(List,3).

% Question 9.  The number of questions whose answers are vowels is:
q9(List) :-
	List=[_,_,_,_,_,_,_,_,X9|_],
	q9cons(X9,List).

% a: 0
q9cons(a,List) :-
	cntvowels(List,0).
% b: 1
q9cons(b,List) :-
	cntvowels(List,1).
% c: 2
q9cons(c,List) :-
	cntvowels(List,2).
% d: 3
q9cons(d,List) :-
	cntvowels(List,3).
% e: 4
q9cons(e,List) :-
	cntvowels(List,4).

% Question 10.  The answer to this question is:
q10(List) :-
	List=[_,_,_,_,_,_,_,_,_,X10],
	q10cons(X10).
% a: a
q10cons(a).
% b: b
q10cons(b).
% c: c
q10cons(c).
% d: d
q10cons(d).
% e: e
q10cons(e).


cntconsonants([],0).
cntconsonants([H|T],N) :-
	consonant(H),
	M#=N-1,
	cntconsonants(T,M).
cntconsonants([H|T],N) :-
	vowel(H),
	cntconsonants(T,N).

cntvowels([],0).
cntvowels([H|T],N) :-
	vowel(H),
	M#=N-1,
	cntvowels(T,M).
cntvowels([H|T],N) :-
	consonant(H),
	cntvowels(T,N).

consonant(b).
consonant(c).
consonant(d).

vowel(a).
vowel(e).

