Skip to content

Instantly share code, notes, and snippets.

@mattdodge mattdodge/liars.pl
Last active Feb 23, 2019

Embed
What would you like to do?
A Prolog solution to the FiveThirtyEight island liars Riddler (2/22/19)
% import our CLP library for constraint declarations
:- use_module(library(clpfd)).
% A Claimant is a liar if their age is over the threshold. We also
% explicitly declare the "not liar" condition here since `not` seems
% to behave weirdly with constraints
liar(Claimant, LieThreshold) :- Claimant #>= LieThreshold.
not_liar(Claimant, LieThreshold) :- Claimant #< LieThreshold.
% A Person is older than an Age if the Claimaint is not a liar, otherwise
% they are younger than or equal to that age
claim_older(Person, Age, Claimant, LieThreshold) :-
liar(Claimant, LieThreshold),
Person #=< Age.
claim_older(Person, Age, Claimant, LieThreshold) :-
not_liar(Claimant, LieThreshold),
Person #> Age.
claim_younger(Person, Age, Claimant, LieThreshold) :-
liar(Claimant, LieThreshold),
Person #>= Age.
claim_younger(Person, Age, Claimant, LieThreshold) :-
not_liar(Claimant, LieThreshold),
Person #< Age.
claim_equal(Person, Age, Claimant, LieThreshold) :-
liar(Claimant, LieThreshold),
Person #\= Age.
claim_equal(Person, Age, Claimant, LieThreshold) :-
not_liar(Claimant, LieThreshold),
Person #= Age.
claim_not_equal(Person, Age, Claimant, LieThreshold) :-
liar(Claimant, LieThreshold),
Person #= Age.
claim_not_equal(Person, Age, Claimant, LieThreshold) :-
not_liar(Claimant, LieThreshold),
Person #\= Age.
% Run this by giving a list of 5 islanders and an age threshold
% Example:
% ?- run([A, B, C, D, E], L).
run(Islanders, L) :-
% Constrain our ages from 10 to 40 years old
Islanders ins 10..40,
L in 10..40,
[A, B, C, D, E] = Islanders,
% Claims from the original Riddler product statement
claim_older(B, 20, A, L),
claim_older(D, 16, A, L),
claim_older(C, 18, B, L),
claim_younger(E, 20, B, L),
claim_younger(D, 22, C, L),
claim_equal(A, 19, C, L),
claim_not_equal(E, 17, D, L),
claim_equal(B, 20, D, L),
claim_older(A, 21, E, L),
claim_younger(C, 18, E, L).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.