Last active
August 29, 2015 14:19
-
-
Save yohanes/cbc4976f5a608b4deed1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% to run it: swipl -g 'conclusion' -t halt -q -s cheryl.pl | |
% http://www.straitstimes.com/news/singapore/education/story/primary-5-maths-question-goes-viral-stumps-adults-20150413 | |
unique(A) :- aggregate_all(count, A, M), M==1. | |
birthday_list(15, may). | |
birthday_list(16, may). | |
birthday_list(19, may). | |
birthday_list(29, may). | |
birthday_list(17, june). | |
birthday_list(18, june). | |
birthday_list(14, july). | |
birthday_list(16, july). | |
birthday_list(14, august). | |
birthday_list(15, august). | |
birthday_list(17, august). | |
% bernard date is one in the list | |
bernard_date(D):- | |
birthday_list(D, _). | |
% bernard knows cheryl birthday for sure if the date is unique | |
bernard_knows(D) :- | |
unique(bernard_date(D)). | |
% albert knows the month M | |
% albert will think that bernard might know if the month M contains a unique date | |
bernard_might_know(M):- | |
birthday_list(D, M), | |
bernard_knows(D). | |
% albert is sure that bernard doesn't know, so we can narrow down the possible dates | |
possible_birthday(D, M):- birthday_list(D, M), | |
not(bernard_knows(D)), | |
not(bernard_might_know(M)). | |
% bernard can make a conclusion based on the D that he knows | |
% it means that the date is unique to a particular month | |
bernard_conclusion(D):- | |
possible_birthday(D, _), | |
unique(possible_birthday(D, _)). | |
% albert knows the month M | |
% albert knows that bernard can make a conclusion for the date D in that month | |
albert_month(M):- | |
possible_birthday(D, M), | |
bernard_conclusion(D). | |
% because albert can also make a conclusion, | |
% so there must be only one unique month | |
albert_conclusion(M):- | |
possible_birthday(_, M), | |
unique(albert_month(M)). | |
% the final conclusion is the month from albert and date from bernard | |
birthday(D,M):- | |
possible_birthday(D, M), | |
albert_conclusion(M), | |
bernard_conclusion(D). | |
conclusion:- | |
setof([D, M], birthday(D,M), L), | |
length(L, N),N==1, | |
birthday(D, M), | |
write(D), write(' '), write(M), write('\n'), !. | |
conclusion:- | |
write('Can not make conclusion\n'), | |
setof([D, M], birthday(D, M), L), write('Possible answers:'), write(L), write('\n'). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment