3 ms·
I got to writing this Prolog/constraint version which you can try on SWISH by clicking https://swish.swi-prolog.org/p/FYMhJEmj.pl https://swish.swi-prolog.org/p
by jodrellblank 2y ago
I got to writing this Prolog/constraint version which you can try on SWISH by clicking https://swish.swi-prolog.org/p/FYMhJEmj.pl https://swish.swi-prolog.org/p/FYMhJEmj.pl then clicking the blue 'Run' button in the lower right corner.
:-use_module(library(clpfd)).
% score/3 has mode ++- and maps
% a given answer and a correct answer to a score.
score(Given, Correct, Score) :-
zcompare(Order, Given, Correct),
score_(Order, Score).
score_(=, 10). % 10 points if given matches correct,
score_(<, 0). % else 0 points
score_(>, 0).
puzzle(ColinTotal, ColinScores, CorrectAnswers) :-
A = 0, % false
B = 1, % true
length(CorrectAnswers, 10), % ten unknown correct answers,
CorrectAnswers ins 0..1, % all either true or false.
% compare player answers with correct answers, add their scores.
maplist(score, [B,B,A,B,A,B,B,A,B,B], CorrectAnswers, Mary),
sum(Mary, #=, 70),
maplist(score, [B,A,A,A,B,A,B,A,A,A], CorrectAnswers, Dan),
sum(Dan, #=, 50),
maplist(score, [B,A,A,A,B,B,B,A,B,A], CorrectAnswers, Lisa),
sum(Lisa, #=, 30),
maplist(score, [B,B,A,A,A,B,B,A,A,A], CorrectAnswers, ColinScores),
sum(ColinScores, #=, ColinTotal).
Then this query:
?- puzzle(ColinTotal, ColinScores, CorrectAnswers).
Solves without a constraint solver search and gives four possible solutions, each giving the same score for Colin which I've aligned by hand for this comment:
ColinTotal = 60,
ColinScores = [10, 10, 10, 0, 10, 0, 10, 0, 10, 0],
CorrectAnswers = [ 1, 1, 0, 1, 0, 0, 1, 1, 0, 1]
ColinTotal = 60,
ColinScores = [10, 10, 10, 0, 10, 0, 0, 10, 10, 0],
CorrectAnswers = [ 1, 1, 0, 1, 0, 0, 0, 0, 0, 1]
ColinTotal = 60,
ColinScores = [10, 10, 0, 0, 10, 0, 10, 10, 10, 0],
CorrectAnswers = [ 1, 1, 1, 1, 0, 0, 1, 0, 0, 1]
ColinTotal = 60,
ColinScores = [ 0, 10, 10, 0, 10, 0, 10, 10, 10, 0],
CorrectAnswers = [ 0, 1, 0, 1, 0, 0, 1, 0, 0, 1]
Questions 2,4,5,6,9,10 have a fixed correct answer and 1,3,7,8 can vary and the puzzle is still coherent and solvable.