Skip to content

Instantly share code, notes, and snippets.

@Ferada
Last active August 29, 2015 14:19
Show Gist options
  • Save Ferada/19de459d28cfa3724e83 to your computer and use it in GitHub Desktop.
Save Ferada/19de459d28cfa3724e83 to your computer and use it in GitHub Desktop.
% May 15 - May 16 - May 19
% June 17 - June 18
% July 14 - July 16
% August 14 - August 15 - August 17
% July 14 - July 16
% August 14 - August 15 - August 17
% July 16
birthday(may, 15).
birthday(may, 16).
birthday(may, 19).
birthday(june, 17).
birthday(june, 18).
birthday(july, 14).
birthday(july, 16).
birthday(august, 14).
birthday(august, 15).
birthday(august, 17).
all_days(Days) :-
findall(Day, birthday(_, Day), Days).
all_months(Months) :-
findall(Month, birthday(Month, _), AllMonths),
sort(AllMonths, Months).
member(X, [Y|Remainder], Remainder) :- X == Y, !.
member(X, [_|List], Remainder) :-
member(X, List, Remainder).
not_member(_, []).
not_member(X, [Y|Remainder]) :- X \= Y, not_member(X, Remainder), !.
member_once(X, List) :-
member(X, List, Remainder),
not_member(X, Remainder), !.
unique(List, Result) :-
findall(X, (member(X, List), member_once(X, List)), Result).
unique_days(Days) :-
all_days(AllDays),
unique(AllDays, Days).
bernard_cant_know_immediately(Months) :-
unique_days(UniqueDays),
all_months(AllMonths),
findall(Month, (member(Day, UniqueDays), birthday(Month, Day)), ImpossibleMonths),
findall(Month, (member(Month, AllMonths), not_member(Month, ImpossibleMonths)), Months).
days_for_months(Months, Days) :-
findall(Day, (member(Month, Months), birthday(Month, Day)), AllDays),
sort(AllDays, Days).
bar(Days, Month, Day) :-
member(Day2, Days),
birthday(Month, Day2),
Day \= Day2.
foo(Months, Days, Month, Day) :-
member(Day, Days),
member(Month, Months),
birthday(Month, Day),
not(bar(Days, Month, Day)).
bernard_knows_now(Months, Month, Day) :-
days_for_months(Months, AllDays),
findall(Day, (member(Month1, Months),
member(Month2, Months),
Month1 \= Month2,
birthday(Month1, Day),
birthday(Month2, Day)),
ImpossibleDays),
findall(Day, (member(Day, AllDays), not_member(Day, ImpossibleDays)), PossibleDays),
sort(PossibleDays, Days),
foo(Months, Days, Month, Day).
cheryl(Month, Day) :-
bernard_cant_know_immediately(Months),
bernard_knows_now(Months, Month, Day).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment