Solving a Logic Puzzle with… Logic

In my post Solving a Logic Puzzle with Code, I took a New Scientist puzzle and applied logic and code to solve it. But code is not always necessary to solve logic puzzles. In fact, the previous puzzle could have been solved with logic alone. The logical thinking you go through to solve a puzzle can become the basis for an algorithm to solve a logic puzzle, and you would certainly want to use code if the puzzle had a greater number of variables. But sometimes, the logical thinking is enough to solve the puzzle, without code.

This time, the puzzle I challenged myself to solve was the New Scientist Puzzle #103: Trouble Brewing. The problem is this:

Our office vending machine normally allows me to get any combination of tea, coffee, chocolate, milk and sugar, each with its own button. Unfortunately it has developed a glitch.

What happens now is that instead of delivering its own ingredient, each button delivers two other ingredients instead. Each button delivers a different pair, and each of the ingredients is delivered by two of the buttons.

However, if two buttons demand the same ingredient, such as tea, they cancel each other out and I don’t get tea at all.

The result? If I ask for chocolate with milk, I get tea with sugar. If I ask for tea with milk and sugar, I get those three ingredients plus coffee!

What do I get if I press the coffee and milk buttons?

The first time I solved this puzzle, I was looking for an answer, and I got one, fairly quickly. It was only when I worked through the puzzle the second time, rigorously applying logic, that I discovered that there are actually two solutions for how the ingredients can be assigned to the buttons. So how did I arrive at an answer? Read on to find out.

Step 1: Pull out Key Information

In the text of this logic puzzle contains some key information we need to solve it:

  1. There are 5 buttons: tea, coffee, chocolate, milk, sugar
  2. Each button delivers two other ingredients
  3. Each button delivers a different pair
  4. Each of the ingredients is delivered by two of the buttons
  5. If two buttons have an ingredient that is the same, that ingredient is cancelled out when those buttons are pressed
  6. Pressing the chocolate and milk buttons gives tea and sugar
  7. Pressing the tea, milk and sugar buttons gives tea, milk, sugar and coffee

Step 2: From the Key Information, make Logical Inferences

What information can we infer from the key information given?

From key info item 2, we can deduce that no button returns the ingredient labelled on the button. The phrase ‘two other ingredients’ means ‘ingredients other than the button ingredient’.

From key info items 2 and 4, we can infer that no button returns two ingredients that are the same. Each button only delivers two ingredients, and each of the ingredients has to be delivered by two of the buttons. If the same ingredient was on the same button twice, it could only be delivered by one of the buttons.

From key info items 2, 3 and 4, we can deduce that each ingredient appears exactly twice. Each button delivers a different pair, where those pairs can’t be of the same ingredient. Each of the ingredients is delivered by two of the buttons. This means that each ingredient can only appear twice, as each ingredient can only appear once per button, and each ingredient is on two of the buttons.

To summarise:

  1. No button returns the ingredient on the button
  2. No button returns the same ingredient twice
  3. Each ingredient appears exactly twice

Step 3: Draw up a starting matrix

We can draw up a table that shows each button in columns, and the ingredients on each button in the rows. Where we don’t yet know the ingredient, we can put ‘?’. Where we know an ingredient is NOT present, we can put ‘No’. Where we know an ingredient is present, we can put ‘Yes’.

The only thing we know for sure right now is that each button does not contain its label ingredient, so we can enter ‘No’ for those ingredients.

Here is the matrix:

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffee?No???
chocolate??No??
milk???No?
sugar????No

Step 4: Narrow down possibilities

With logic puzzles, you can either try to work out what is true, or what definitely isn’t true, or some combination of both. This will help to narrow down the possible solutions and enable you to use process of elimination to narrow down the possibilities even further.

Let’s look at key info 6: Pressing the chocolate and milk buttons gives tea and sugar. What’s interesting to note is what the chocolate and milk buttons don’t produce. They don’t produce coffee, chocolate or milk. Coffee could be on both buttons, thus cancelling out (key info 5), or it could be on neither button. Chocolate can’t be on the chocolate button, so for it not to be produced, it must also not be on the milk button. Milk can’t be on the milk button, so for it not to be produced, it must also not be on the chocolate button.

We can update the matrix with our logical deductions:

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffee?No???
chocolate??NoNo?
milk??NoNo?
sugar????No

OK, we’re off to a good start. Let’s look at key info 7: Pressing the tea, milk and sugar buttons gives tea, milk, sugar and coffee. Again, looking at what isn’t produced, we find the only ingredient missing is chocolate. However, we have already eliminated chocolate from the chocolate and milk buttons, and it needs to be on two buttons (key info 4). That means it has to be on one or more of the tea and sugar buttons. It can’t be on only one of the tea and sugar buttons, as it would be present in the produced ingredients otherwise. This means it has to be on both the tea and sugar buttons.

The matrix becomes:

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffee?No???
chocolateYes?NoNoYes
milk??NoNo?
sugar????No

We can now see that chocolate is on two of the buttons, so we can fill in the rest of the chocolate row with ‘No’ for the other buttons.

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffee?No???
chocolateYesNoNoNoYes
milk??NoNo?
sugar????No

Looking at key info item 7 again (Pressing the tea, milk and sugar buttons gives tea, milk, sugar and coffee), we can see that as coffee is produced, it must be on one of the tea, milk or sugar buttons, but it can only be on one of these (otherwise it would be cancelled out if it was on two). This means coffee must also be on the chocolate button, because coffee must be on two of the buttons, and as it can’t be on the coffee button, the chocolate button is the only other button it can be on.

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffee?NoYes??
chocolateYesNoNoNoYes
milk??NoNo?
sugar????No

Circling back to key info item 6 (Pressing the chocolate and milk buttons gives tea and sugar), we can see that coffee is not produced, so it must be on both the chocolate and milk buttons, or neither. We now know that it’s on the chocolate button, so it must be also on the milk button.

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffee?NoYesYes?
chocolateYesNoNoNoYes
milk??NoNo?
sugar????No

Coffee is now on two buttons, so we can fill in the rest of the coffee row for the other buttons with ‘No’.

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milk??NoNo?
sugar????No

From key info items 6 and 7, we have a few more possibilities to narrow down:

The tea, milk and sugar buttons produce milk, which means milk must be on either the tea or sugar buttons, but not both (we know it can’t be on the milk button).

The chocolate and milk buttons produce sugar, as do the tea, milk and sugar buttons. Sugar must therefore be present on one of the chocolate or milk buttons and one of the tea or milk buttons (we know it can’t be on the sugar button). It can’t be on the chocolate AND milk buttons, as they would cancel out in the chocolate/milk button combination. It can’t be on the tea AND milk buttons either, as it would cancel out in the tea/milk/sugar button combination. Therefore sugar can be on the chocolate AND tea buttons, or just on the milk button.

The chocolate and milk buttons produce tea, as do the tea, milk and sugar buttons. Tea must be present on one of the chocolate or milk buttons and one of milk or sugar (we know it can’t be on the tea button). It can’t be on the chocolate AND milk buttons, as it would cancel out in the chocolate/milk button combination. Therefore, tea can be on the chocolate AND sugar buttons or just on the milk button.

In summary:

milk -> button_tea OR button_sugar
sugar -> button_chocolate, button_tea OR milk_button
tea -> button_chocolate, button_sugar OR button_milk

Step 5: Trial and Error

We’ve used logic to narrow down the possibilities as much as we can. We’ve now come to a fork in the road. Our only way forward is to use trial and error. We’ll try a solution, see if it works, and see what answer it gives. Then we’ll try another solution. We’ll continue until we’ve exhausted the possibilities and we arrive at an answer.

In the last step we narrowed our remaining options down to these:

milk -> button_tea OR button_sugar
sugar -> button_chocolate, button_tea OR milk_button
tea -> button_chocolate, button_sugar OR button_milk

Find Solution 1

First, we’ll try putting milk on the tea button. This gives us:

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkYes?NoNo?
sugar????No

This means the tea button has two ingredients, so we can fill in the rest of the tea button column with ‘No’.

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkYes?NoNo?
sugarNo???No

The sugar now has to go on the milk button, as it can’t go on the tea button, as it already contains two ingredients. It can’t go on the chocolate button because it would cancel out in the chocolate/milk button combination.

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkYes?NoNo?
sugarNo??YesNo

The milk button now has two ingredients, and can be filled in with ‘No’ for the last ingredient:

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo??No?
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkYes?NoNo?
sugarNo??YesNo

Tea must now go on the chocolate and sugar buttons, as the milk button is full:

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo?YesNoYes
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkYes?NoNo?
sugarNo??YesNo

The chocolate and sugar buttons are now full and can be filled in with ‘No’ for the remaining ingredients:

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo?YesNoYes
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkYes?NoNoNo
sugarNo?NoYesNo

Tea is now on two buttons and so the coffee button can be eliminated.

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNoNoYesNoYes
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkYes?NoNoNo
sugarNo?NoYesNo

There are only two unknown ingredients, and they are on the coffee button. Since the rest of the ingredients are not on the coffee button, it must contain milk and sugar:

button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNoNoYesNoYes
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkYesYesNoNoNo
sugarNoYesNoYesNo

We have a solution!

Test Solution 1

Let’s test that our solution works:

Are there two different ingredients on each button? Yes!

Are there no two buttons with the same pair of ingredients? Yes!

Are there two instances of each ingredient? Yes!

Key info item 6 tells us that the chocolate and milk buttons should give us tea and sugar. So what do we get?

Chocolate button = tea + coffee
Milk button = coffee + sugar
Chocolate button + Milk button = tea + coffee + coffee + sugar 
                               = tea + sugar

After coffee is eliminated, we get tea and sugar, as expected.

So far, so good. Let’s test key info item 7 (Pressing the tea, milk and sugar buttons gives tea, milk, sugar and coffee):

Tea button = chocolate + milk
Milk button = coffee + sugar
Sugar button = tea + chocolate
Tea button + 
Milk button + 
Sugar button = chocolate + milk + coffee + sugar + tea + chocolate 
             = milk + coffee + sugar + tea

Chocolate is cancelled out to give us the ingredients we expect.

It looks as if we’ve found a correct solution!

Determine Answer from Solution 1

Let’s see if we can answer the question posed by the puzzle, which was ‘What do I get if I press the coffee and milk buttons?’

Coffee button = milk + sugar
Milk button = coffee + sugar
Coffee button + Milk button = milk + sugar + coffee + sugar 
                            = milk + coffee

The sugar cancels out to give us… milk and coffee. Strangely, the buttons give us what we asked for!

It would be tempting to stop here. We’ve found a solution, and it works with the example button combinations.

But what about the other possible solution(s)? Would they have worked? Let’s find out.

Find Solution 2

If you recall, in Step 4 we narrowed down our options to these:

milk -> button_tea OR button_sugar
sugar -> button_chocolate, button_tea OR milk_button
tea -> button_chocolate, button_sugar OR button_milk

Last time, we put the milk on the tea button. Now we will try putting it on the sugar button.

 button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo????
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milk??NoNoYes
sugar????No

The sugar button now has two ingredients, so we can fill in the rest of the column with ‘No’.

 button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo???No
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milk??NoNoYes
sugar????No

The tea must now go on the milk button, as it can’t go on the sugar button (as it’s full), and it can’t go on the chocolate button without also going on the sugar button.

 button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo??YesNo
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milk??NoNoYes
sugar????No

The milk button now has two ingredients, so the rest of the column can be filled with ‘No’.

 button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo??YesNo
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milk??NoNoYes
sugar???NoNo

The sugar must now go on the chocolate and tea buttons (as it can’t go on the milk button, because it’s now full):

 button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo??YesNo
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milk??NoNoYes
sugarYes?YesNoNo

The tea and chocolate buttons now have two ingredients each, and can be filled in with ‘No’ for the other ingredients:

 button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo?NoYesNo
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkNo?NoNoYes
sugarYes?YesNoNo

Sugar is now on two buttons, so can’t go on the coffee button:

 button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNo?NoYesNo
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkNo?NoNoYes
sugarYesNoYesNoNo

Now there are only two ingredients to fill in, and they are the only two that can be on the coffee button:

 button_teabutton_coffeebutton_chocolatebutton_milkbutton_sugar
teaNoYesNoYesNo
coffeeNoNoYesYesNo
chocolateYesNoNoNoYes
milkNoYesNoNoYes
sugarYesNoYesNoNo

We have another solution!

Test Solution 2

Are there two different ingredients on each button? Yes!

Are there no two buttons with the same pair of ingredients? Yes!

Are there two instances of each ingredient? Yes!

Key info item 6 tells us that the chocolate and milk buttons should give us tea and sugar. So what do we get?

Chocolate Button = coffee + sugar
Milk Button = tea + coffee
Chocolate Button + Milk Button = coffee + sugar + tea + coffee 
                               = sugar + tea

Coffee is eliminated to give us what we expect.

Let’s test key info item 7 (Pressing the tea, milk and sugar buttons gives tea, milk, sugar and coffee):

Tea Button = chocolate + sugar
Milk Button = tea + coffee
Sugar Button = chocolate + milk
Tea Button + 
Milk Button + 
Sugar Button = chocolate + sugar + tea + coffee + chocolate + milk 
             = sugar + tea + coffee + milk

Chocolate is cancelled out, and we get the expected result.

Our second solution checks out too!

Determine Answer from Solution 2

Let’s see what answer we get this time for the question posed by the puzzle, which was ‘What do I get if I press the coffee and milk buttons?’

Coffee button = tea + milk
Milk button = tea + coffee
Coffee button + Milk button = tea + milk + tea + coffee 
                            = milk + coffee

This time, tea is cancelled out to give us milk and coffee, the same answer we got with the first solution!

Conclusion

Working through the logic, we found two possible configurations that the buttons could have, that satisfied all the constraints. However, both button configurations resulted in the same answer to the question posed by the puzzle: ‘What do I get if I press the coffee and milk buttons?’ In both cases, the buttons produced the same two ingredients, once other ingredients had cancelled out. So, even though there were two ways to arrange the ingredients on the buttons, the result of pressing the coffee and milk buttons was still the same, allowing us to give one answer to the question.

Pressing the coffee and milk buttons gives you coffee and milk!

I appreciate the irony 😉

Thanks for reading, see you next time!