# Where is Gold?¶

This is an implementation for the June 2021 DMCommunity challenge. The challenge is as follows:

Where is Gold?

There are three boxes, but only one of them has gold inside. Additionally, each box has a message printed on it. One of these messages is true and the other two are lies.

- The first box says, “Gold is in this box”.
- The second box says, “Gold is not in this box”.
- The third box says, “Gold is not in Box 1”.

Which box contains the gold?

As always with the DMCommunity challenges, this is quite a fun one to implement. We start by building our glossary. It is clear that we need a way to reason on boxes, so for we first introduce a type Box.

Type | ||
---|---|---|

Name | Type | Values |

Box | Int | [1..3] |

Next, we need a way to express what boxes “speak the truth”, and what boxes “lie”. Because the boolean nature of truth/lie, we can use a relation (representing true/false for every box’s statement).

Relation |
---|

Name |

Box speaks truth |

To express what box contains the gold, we can make perfect use of a constant, GoldBox. Similarly, to express the number of boxes speaking the truth, we introduce a constant NbTruth.

Constant | |
---|---|

Name | Type |

GoldBox | Box |

NbTruth | Int |

With our glossary finished, we continue to our decision/constraint tables. We start by defining when a box speaks the truth.

Box speaks truth | |||
---|---|---|---|

U | Box called b | GoldBox | b speaks truth |

1 | 1 | 1 | Yes |

2 | 2 | not(2) | Yes |

3 | 3 | not(1) | Yes |

The rules of the table can be read as follows:

- For box 1: if it contains the gold, it speaks the truth.
- For box 2: if it does not contain the gold, it speaks the truth.
- For box 3: if box 1 does not contain the gold, it speaks the truth.

We now count how many boxes speak the truth, in order to then create a constraint on it.

Count NbTruth | |||
---|---|---|---|

C+ | Box called b | b speaks truth | NbTruth |

1 | - | Yes | 1 |

Truth | |
---|---|

E* | NbTruth |

1 | 1 |

The first table expresses that “For every box b, if it speaks the truth, add 1 to NbTruth”. The second table then expresses a constraint that NbTruth should always be 1, i.e., only one box may speak the truth.

With those two tables created, we are done! We can now run our model using the cDMN solver, to find out which box contains the gold.

```
Model 1
==========
GoldBox:={->2}
NbTruth:={->1}
Box_speaks_truth:={1->false; 2->false; 3->true}
No more models.
Elapsed Time:
0.679s
```