Where is gold challenge with Prolog


My #prolog solution for “Where is gold?” proposed by dmcommunity.org challenge Jun 2021: 0 means empty, 1 means gold

?- solution(Box1,Box2,Box3).
Box1 = Box3, Box3 = 0,
Box2 = 1.

below the code


sentence1(    1,_Box2,_Box3).

sentence2(    _Box1,0,_Box3).

sentence3(    0,_Box2,_Box3).

true_only_one_sentence(Box1, Box2, Box3):-
	(       sentence1(Box1,Box2,Box3), not_sentence2(Box1,Box2,Box3), not_sentence3(Box1,Box2,Box3) ) ;
	(   not_sentence1(Box1,Box2,Box3),     sentence2(Box1,Box2,Box3), not_sentence3(Box1,Box2,Box3) ) ;
	(   not_sentence1(Box1,Box2,Box3), not_sentence2(Box1,Box2,Box3),     sentence3(Box1,Box2,Box3) ).

solution(Box1, Box2, Box3):-
	Box1 in 0..1, /* 0 empty, 1 gold */
	Box2 in 0..1,
	Box3 in 0..1,
	Box1 + Box2 + Box3 #= 1, /* only one box contains gold */
	true_only_one_sentence(Box1, Box2, Box3).

Enter your instance's address

More posts like this

Smart investment Problem with Prolog

2024-07-14 | #programming #prolog

Below my #prolog solution for Smart investment problem. It is a sample of Linear Programming using Prolog. A client of an investment firm has $10000 available for investment. He has instructed that his money be invested in particular stocks, so that no more than $5000 is invested in any one stock but at least $1000 be invested in each stock.

Continue reading 

Stable Marriage Problem with Prolog

2024-06-07 | #programming #prolog

My #prolog solution for Stable Marriage Problem proposed by dmcommunity.org challenge Jun 2024. Given n men and n women, where each person has ranked all members of the opposite sex in order of preference, marry the men and women together such that there are no two people of opposite sex who would both rather have each other than their current partners.

Continue reading 