forked from mmalita/PrologPuzzles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffalo.pl
49 lines (45 loc) · 1.8 KB
/
buffalo.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* File: buffalo.pl Author: Scott Marley
Title: Babysitting From: http://brownbuffalo.sourceforge.net
Publication: Dell Logic Puzzles April, 1998 Page: 7 Stars: 1
Each weekday, Bonnie takes care of five of the neighbors' children. The children's names are Keith, Libby, Margo, Nora, and Otto; last names are Fell, Gant, Hall, Ivey, and Jule. Each is a different number of years old, from two to six. Can you find each child's full name and age?
1. One child is named Libby Jule.
2. Keith is one year older than the Ivey child,
who is one year older than Nora.
3. The Fell child is three years older than Margo.
4. Otto is twice as many years old as the Hall child.
USES SWI: member/2, permutation/2
?- start(S), write_list(S), fail.
[keith,fell,5]
[libby,jule,6]
[margo,hall,2]
[nora,grant,3]
[otto,ivey,4]
*********************************************/
:- encoding(utf8).
:- use_module(library(lists)).
start(Sol) :-
F = [keith,libby,margo,nora,otto],
L = [fell,grant,hall,ivey,jule],
Sol = [[F1,L1,A1],[F2,L2,A2],[F3,L3,A3],[F4,L4,A4],[F5,L5,A5]],
F = [F1,F2,F3,F4,F5], % if order is not important!
member([libby,jule,_],Sol), % 1
permutation([L1,L2,L3,L4,L5],L), % write correspondence
permutation([A1,A2,A3,A4,A5],[2,3,4,5,6]),
member([keith,_,AgeK],Sol), % 2
member([_,ivey,AgeI],Sol), AgeK is AgeI+1,
member([nora,_,AgeN],Sol), AgeI is AgeN+1,
member([margo,_,AgeM],Sol),
member([_,fell,AgeF],Sol), AgeF is AgeM+3, %3
member([otto,_,AgeO],Sol),
member([_,hall,AgeH],Sol),
AgeO is AgeH*2. %4
write_list(L) :-
forall(member(X,L),
(write(X), nl)
).
/* There are two types of restrictions:
a) positive ones- when you fill the Solution
b) negative ones where you need the space to be instantiated already:
not X doesn't have sense unless you know from where to take X
! always put a) before b)
*/