-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstrained-clp.pl
73 lines (61 loc) · 2.26 KB
/
constrained-clp.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
:- use_module(library(clpfd)).
solve:-
Houses = [1, 2, 3, 4, 5],
Colors = [Red, Green, Ivory, Blue, Yellow],
Nationalities = [Englishman, Spaniard, Ukrainian, Norwegian, Japanese],
Pets = [Dog, Snake, Zebra, Fox, Horse],
Drinks = [Tea, OrangeJuice, Milk, Water, Coffee],
Cigarettes = [OldGold, Kools, Chesterfield, LuckyStrike, Parliament],
HouseValues = [Colors,Nationalities,Pets,Drinks,Cigarettes],
Colors ins 1..5,
Nationalities ins 1..5,
Pets ins 1..5,
Drinks ins 1..5,
Cigarettes ins 1..5,
Norwegian #= 1,
Blue #= 2,
Milk #= 3,
Green #= (Ivory+1),
Coffee #= Green,
(Yellow #= (Horse-1)) #\/ (Yellow #= (Horse+1)),
Yellow #= Kools,
Englishman #= Red,
Spaniard #= Dog,
Ukrainian #= Tea,
OldGold #= Snake,
(Chesterfield #= (Fox + 1)) #\/ (Chesterfield #= (Fox - 1)),
LuckyStrike #= OrangeJuice,
Japanese #= Parliament,
%no house with same value
maplist(all_different,HouseValues),
%label value as used to be sure every value has been set
maplist(label,HouseValues),
printHouses(Houses,HouseValues).
printHouses([],_).
printHouses([HouseNr|RestHouseNr],HouseValues) :-
printHouse(HouseNr,HouseValues),
printHouses(RestHouseNr,HouseValues).
printHouse(HouseNr,HouseValues) :-
ColorsLabel = ['Red', 'Green', 'Ivory', 'Blue', 'Yellow'],
NationalitiesLabel = ['Englishman', 'Spaniard', 'Ukrainian', 'Norwegian', 'Japanese'],
PetsLabel = ['Dog', 'Snake', 'Zebra', 'Fox', 'Horse'],
DrinksLabel = ['Tea', 'Orange Juice', 'Milk', 'Water', 'Coffee'],
CigarettesLabel = ['OldGold', 'Kools', 'Chesterfield', 'LuckyStrike', 'Parliament'],
HouseValueLabels = [ColorsLabel, NationalitiesLabel, PetsLabel ,DrinksLabel ,CigarettesLabel],
string_concat('House ', HouseNr, ThisHouseStr),
string_concat(ThisHouseStr, ': ', ThatHouseStr),
write(ThatHouseStr),
printValues(HouseNr,HouseValues, HouseValueLabels),
nl.
printValues(_,[],_).
printValues(HouseNr,[Value],[ValueLabel|LabelRest]) :-
printValue(HouseNr,Value,ValueLabel),
printValues(HouseNr,[],LabelRest).
printValues(HouseNr,[Value|Rest],[ValueLabel|LabelRest]) :-
printValue(HouseNr,Value,ValueLabel),
write(', '),
printValues(HouseNr,Rest,LabelRest).
printValue(HouseNr,Pos,Labels) :-
nth1(At,Pos,HouseNr),
nth1(At, Labels, Name),
write(Name).