Because I said: C++ is the best language to model general problems. So it is.
Almost all are logical propositions. Except those few keywords, such program won't
be recognized as a C++ program, instead of some kind of declarative language.
Zebra puzzle is an interesting programing exercise because it is not too easy
and also not too difficult.
------------
/* Copyright is licensed by GNU LGPL, see file COPYING. by I.J.Wang 2023
Example of solving the zebra puzzle by using propositional logic.
Zebra Puzzle
https://en.wikipedia.org/wiki/Zebra_Puzzle 1.There are five houses.
2.The Englishman lives in the red house.
3.The Spaniard owns the dog.
4.Coffee is drunk in the green house.
5.The Ukrainian drinks tea.
6.The green house is immediately to the right of the ivory house.
7.The Old Gold smoker owns snails.
8.Kools are smoked in the yellow house.
9.Milk is drunk in the middle house.
10.The Norwegian lives in the first house.
11.The man who smokes Chesterfields lives in the house next to the man with the fox.
12.Kools are smoked in the house next to the house where the horse is kept.
13.The Lucky Strike smoker drinks orange juice.
14.The Japanese smokes Parliaments.
15.The Norwegian lives next to the blue house.
Note: This program takes lots of time/space, press cntrl-C to exit.
(a_logic_p2.cpp is more practical)
*/
#include <Wy.stdio.h>
#include <Sc/PropExpr.h>
using namespace Wy;
using namespace Sc;
constexpr WeName Red("Red");
constexpr WeName Yellow("Yellow");
constexpr WeName Blue("Blue");
constexpr WeName Ivory("Ivory");
constexpr WeName Green("Green");
constexpr WeName Norweg("Norweg");
constexpr WeName Ukrain("Ukrain");
constexpr WeName English("English");
constexpr WeName Spani("Spani");
constexpr WeName Japan("Japan");
constexpr WeName Water("Water");
constexpr WeName Tea("Tea");
constexpr WeName Milk("Milk");
constexpr WeName Orange("Orange"); // OrangeJuice
constexpr WeName Coffee("Coffee");
constexpr WeName Kools("Kools"); // Kools
constexpr WeName Chest("Chest"); // Chesterfield
constexpr WeName OldGold("OldGold"); // OldGold
constexpr WeName Lucky("Lucky"); // LuckyStrike
constexpr WeName Parl("Parl"); // Parliament
constexpr WeName Fox("Fox");
constexpr WeName Horse("Horse");
constexpr WeName Snail("Snail");
constexpr WeName Dog("Dog");
constexpr WeName Zebra("Zebra");
constexpr WeName No1("No1");
constexpr WeName No2("No2");
constexpr WeName No3("No3");
constexpr WeName No4("No4");
constexpr WeName No5("No5");
PropExpr same_house(WeName m1, WeName m2) {
return propSEL(
propAND(Prop(No1,m1),Prop(No1,m2)),
propAND(Prop(No2,m1),Prop(No2,m2)),
propAND(Prop(No3,m1),Prop(No3,m2)),
propAND(Prop(No4,m1),Prop(No4,m2)),
propAND(Prop(No5,m1),Prop(No5,m2)) );
};
PropExpr next_house(WeName m1, WeName m2) {
return propSEL(
PropExpr(Prop(No1,m1))&&PropExpr(Prop(No2,m2)),
PropExpr(Prop(No2,m1))&&propXOR(Prop(No1,m2),Prop(No3,m2)),
PropExpr(Prop(No3,m1))&&propXOR(Prop(No2,m2),Prop(No4,m2)),
PropExpr(Prop(No4,m1))&&propXOR(Prop(No3,m2),Prop(No5,m2)),
propAND(Prop(No5,m1),(Prop(No4,m2))) );
};
PropExpr map11(WeName n1, WeName n2, WeName n3, WeName n4, WeName n5)
{
return
propSEL(Prop(No1,n1),Prop(No1,n2),Prop(No1,n3),Prop(No1,n4),Prop(No1,n5))&&
propSEL(Prop(No2,n1),Prop(No2,n2),Prop(No2,n3),Prop(No2,n4),Prop(No2,n5))&&
propSEL(Prop(No3,n1),Prop(No3,n2),Prop(No3,n3),Prop(No3,n4),Prop(No3,n5))&&
propSEL(Prop(No4,n1),Prop(No4,n2),Prop(No4,n3),Prop(No4,n4),Prop(No4,n5))&&
propSEL(Prop(No5,n1),Prop(No5,n2),Prop(No5,n3),Prop(No5,n4),Prop(No5,n5))&&
propSEL(Prop(No1,n1),Prop(No2,n1),Prop(No3,n1),Prop(No4,n1),Prop(No5,n1))&&
propSEL(Prop(No1,n2),Prop(No2,n2),Prop(No3,n2),Prop(No4,n2),Prop(No5,n2))&&
propSEL(Prop(No1,n3),Prop(No2,n3),Prop(No3,n3),Prop(No4,n3),Prop(No5,n3))&&
propSEL(Prop(No1,n4),Prop(No2,n4),Prop(No3,n4),Prop(No4,n4),Prop(No5,n4))&&
propSEL(Prop(No1,n5),Prop(No2,n5),Prop(No3,n5),Prop(No4,n5),Prop(No5,n5))
;
};
// 1.There are five houses.
const PropExpr R1= map11(Red,Yellow,Blue,Ivory,Green)&&
map11(Norweg,Ukrain,English,Spani,Japan)&&
map11(Water,Tea,Milk,Coffee,Orange)&&
map11(Kools,Chest,OldGold,Lucky,Parl)&&
map11(Fox,Snail,Horse,Dog,Zebra);
// 2.The Englishman lives in the red house.
const PropExpr R2( same_house(Red,English) );
// 3.The Spaniard owns the dog.
const PropExpr R3( same_house(Spani, Dog) );
// 4.Coffee is drunk in the green house.
const PropExpr R4( same_house(Green,Coffee) );
// 5.The Ukrainian drinks tea.
const PropExpr R5( same_house(Ukrain, Tea) );
// 6.The green house is immediately to the right of the ivory house.
const PropExpr R6= propSEL(
propAND(Prop(No1,Ivory), Prop(No2,Green)),
propAND(Prop(No2,Ivory), Prop(No3,Green)),
propAND(Prop(No3,Ivory), Prop(No4,Green)),
propAND(Prop(No4,Ivory), Prop(No5,Green))
) && propNOR(Prop(No5,Ivory),Prop(No1,Green));
// 7.The Old Gold smoker owns snails.
const PropExpr R7( same_house(OldGold, Snail) );
// 8.Kools are smoked in the yellow house.
const PropExpr R8( same_house(Yellow, Kools) );
// 9.Milk is drunk in the middle house.
const PropExpr R9( Prop(No3,Milk) );
// 10.The Norwegian lives in the first house.
const PropExpr R10( Prop(No1,Norweg) );
// 11.The man who smokes Chesterfields lives in the house next to the man with the fox.
const PropExpr R11( next_house(Chest, Fox) );
// 12.Kools are smoked in the house next to the house where the horse is kept.
const PropExpr R12( next_house(Kools, Horse) );
// 13.The Lucky Strike smoker drinks orange juice.
const PropExpr R13( same_house(Lucky, Orange) );
// 14.The Japanese smokes Parliaments.
const PropExpr R14( same_house(Japan, Parl) );
// 15.The Norwegian lives next to the blue house.
const PropExpr R15( next_house(Norweg, Blue) );
void t0() {
PropExpr tt( R1 && R2 && R3 && R4 && R5 && R6 && R7 && R8 && R9 &&
R10 && R11 && R12 && R13 && R14 && R15
);
//tt.resolve_pstat(Descrip1);
cout << wrd(tt) << WY_ENDL;
};
int main()
try {
cout << "check " __FILE__ WY_ENDL;
t0();
cout << "OK" WY_ENDL;
return 0;
}
catch(const Errno& e) {
cerr << wrd(e) << WY_ENDL;
return -1;
}
catch(...) {
cerr << "main caught(...)" WY_ENDL;
return -1;
};