Skip to content

Instantly share code, notes, and snippets.

@douglasrizzo
Last active June 6, 2019 00:15
Show Gist options
  • Save douglasrizzo/1c070ce2942b0435f9baee34c05e695b to your computer and use it in GitHub Desktop.
Save douglasrizzo/1c070ce2942b0435f9baee34c05e695b to your computer and use it in GitHub Desktop.
Jobs Puzzle in Answer Set Programming
% https://www.mcs.anl.gov/~wos/mathproblems/jobs.html
%%% people and genders
man(pete;steve).
woman(thelma;roberta).
person(P) :- man(P).
person(P) :- woman(P).
-man(P) :- woman(P).
-woman(P) :- man(P).
%%% jobs
job(chef;
guard;
nurse;
clerk;
cop;
teacher;
actor;
boxer).
% list of jobs that needs education
needsStudy(nurse;cop;teacher).
% each person has exactly two jobs
2 { works(P,E) : job(E) } 2 :- person(P).
% each job is done by only one person
1 { works(P,E) : person(P) } 1 :- job(E).
% roberta is not a boxer
:- works(roberta, boxer).
% person P cannot be a nurse and not be a man
:- works(P, nurse), -man(P).
%%% the chefs`s husband is a clerk
% chef and clerk can`t be two jobs of the same person
:- works(P, chef), works(P, clerk).
% clerk can`t be a man
:- works(P, clerk), -man(P).
% chef can`t be a woman
:- works(P, chef), -woman(P).
% a job that needs education can't be done by someone without education
:- works(P,E), needsStudy(E), -studied(P).
% pete has no education
-studied(pete).
% actor is a male job, so it has to be a man
:- works(P, actor), -man(P).
%%% roberta, , police officer and chef played golf together, so they must be 3 different people
% roberta can`t be the chef or the cop
:- works(roberta, (chef; cop)).
% the same person can`t be a chef and police officer
:- works(P, chef), works(P, cop).
#show works/2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment