We solve all 42 versions of the riddle about potions from Harry Potter.

At the end of the book "Harry Potter and the Philosopher's Stone," there is an interesting riddle. Harry and Hermione enter a room, after which the entrances are blocked by magical fire, and they can only exit by solving the following riddle:

Before you lies danger, and behind you is safety,
Two will help you, whom you find among us;
With one of the seven, you will continue moving forward
The other will transport you back immediately.
In two of us, you will find only nettle wine,
And three bring demise, standing in a row secretly.
So choose well, which one you are destined to taste,
For this, we give you four hints.
The poison has tried in vain to hide its deadly heat,
You will always find it to the left of the wine,
And know that those on the edges hold different gifts,
But if you want to go further, none will help you.
We all differ in size, from edge to edge,
Your death sits not in the lesser, but also not in the greater;
The second from the right end and the second from the left
In taste, like twins, though they look different.

[from the "folk translation" of the book "Harry Potter and the Philosopher's Stone"]

We solve all 42 versions of the riddle about potions from Harry Potter.

In simpler terms, they need to understand which potions are in which bottles.

In this article, we will solve all 42 possible variations of this puzzle using programming and draw a diagram of the results (like the picture above, but much larger).

Wait a second, where did the 42 variations come from?

It's because the locations of the "lesser" and "greater" potions are not specified. The greater one can be in one of seven places, giving 6 remaining options for the lesser, totaling 7 * 6 = 42. It won't be possible to know which arrangement J.K. Rowling had in mind when crafting this riddle unless she shares it on her Twitter. For now, before that unavoidable day comes, we can choose a random version and work with it. However, there will be no guarantees of its solvability, which is why we are working for the common good, solving all 42 variations of the riddle (or proving their unsolvability).

JUST SOLVE IT ALREADY

First of all, here are all the limitations of the riddle, rephrased in simple terms:

  1. There are two harmless potions, 3 poisoned ones, one that allows you to go forward, and one that allows you to go back.
  2. To the left of each of the two harmless potions stands a poisonous one.
  3. The potions on both sides are different, and none of them allow you to progress.
  4. The largest and smallest bottles do not contain poison.
  5. The second bottle from the left and the second bottle from the right contain the same potion.

What should we do about this? Let's consider the following option. Note that, as stated in the riddle, there is 1 bottle that is smaller than all the others, and 1 bottle that is larger than all the others.

We solve all 42 versions of the riddle about potions from Harry Potter.

Let's try to brute force all options – taking one bottle at a time and matching all possible contents.

For example, the first bottle cannot contain a potion that advances us due to restriction #3. It also does not contain a safe potion due to restriction #2 – there can't be poison to its left. This leaves us with options for a poisoned potion and a potion that moves us backward. Let's try both options.

In the following images, green potions indicate poison, orange ones indicate safe drinks, blue ones indicate the potion that moves backward, and purple indicates the potion that moves forward.

We solve all 42 versions of the riddle about potions from Harry Potter.

We solve all 42 versions of the riddle about potions from Harry Potter.

We will repeat this process for both working options – taking the second bottle and alternately trying all acceptable contents. This will give us the following:

We solve all 42 versions of the riddle about potions from Harry Potter.

We solve all 42 versions of the riddle about potions from Harry Potter.

We solve all 42 versions of the riddle about potions from Harry Potter.

We solve all 42 versions of the riddle about potions from Harry Potter.

Continuing in this manner and discarding all working options where a bottle cannot be filled with a potion without violating the listed restrictions, we will arrive at the only acceptable option:

We solve all 42 versions of the riddle about potions from Harry Potter.

Naturally, we had no guarantees of finding a solution. There might not have been any solutions, or there could be multiple (and if you have several solutions, this is equivalent to saying that the riddle cannot be solved since it is unknown which potion is correct).

Applying the algorithm to all options gives us the following results. 8 options for the riddle are solvable, 8 have no solutions, and 26 have multiple solutions.

We solve all 42 versions of the riddle about potions from Harry Potter.

More about the solutions

Is there something common among all the proposed solutions to the riddle? Yes! Notice that the smallest or largest bottles are in positions 2 or 6. This allows us to conclude that the 2nd and 6th bottles contain safe potions due to constraints #4 and #5. Without this step, we cannot eliminate the possibility that there is poison in these bottles, resulting in several possible solutions. Additionally, the viable options require that the second "special" bottle (either the smallest or largest) be in positions 3 or 4. Otherwise, we cannot determine the exact location of the potion that moves us forward.

Summary

I will finish with a quote from the book.

Hermione exhaled sharply, and Harry was amazed to notice that she was smiling – it was the last class he could have imagined. "Brilliant," said Hermione. "This isn't magic – it's logic, a riddle. Many of the greatest wizards wouldn't even have a gram of logic, and they would be stuck here forever."

But wait – maybe we can understand the canonical version of the riddle based on the dialogue from the book:

"Got it," she said. "The smallest bottle will take us through the black fire, and to the Stone."

…

"And which one will let you return through the purple fire?"

Hermione pointed to the round bottle on the right side of the row.

Damn. This option still gives us several solutions. Tweet it, DR.

Code

If you're interested in the code for solving this riddle and drawing diagrams, it can be found here.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster