We solve all 42 versions of the Harry Potter potion puzzle

There’s an interesting mystery at the end of Harry Potter and the Sorcerer's Stone. Harry and Hermione enter the room, after which the entrances to it are blocked by magic fire, and they can only get out of it by solving the following riddle:

There is danger in front of you, and salvation behind you,

Two will help you, whom you will find among us;

With one of the seven forward you will continue to move

The other will take you back immediately.

In the two of us you will find only nettle wine,

And three bear destruction, stand in a row secretly.

So choose from which to try,

For this, we give four clues.

In vain the poison tried to hide its deadly heat

You will always find him to the left of the wine,

And know that those who are on the edges, different keep the gift,

But if you want further, not a single one will help.

We are apart in size everything from edge to edge,

Your death sits not less, but not greater also;

The second from the right end and from the left the second

To taste - like twins, at least in appearance unlike.
[ from the "folk translation" of the book "Harry Potter and the Sorcerer's Stone" ]







Simply put, they need to understand which bottles contain which potions.



In this article, we will solve all 42 possible variants of this puzzle using programming and draw a diagram of the results (as in the picture above, only much larger).



Wait a second, and where did 42 options come from?



This is because the locations of the “smaller” and “larger” potions are not indicated. The big one can be in one of seven places, which gives the 6 remaining options for the smaller one, totaling 7 * 6 = 42. Finding out exactly what kind of arrangement Joan Rowling had in mind when inventing this riddle does not work, unless she tells about it in on your twitter. In the meantime, this inevitable day has not arrived, we can choose a random version and work with it. However, there will be no guarantees of its solvability, which is why we work for the common good, solving all 42 variants of the riddle (or proving their unsolvability).



YES DECIDE ALREADY



Firstly, here are all the limitations of the puzzle, reformulated in a simple way:



  1. There are two harmless potions, 3 poisoned, one that allows you to go forward, and one that allows you to go back.
  2. On the left side of each of the two harmless potions is poisonous.
  3. Potions from both ends are different, and none of them allows you to go forward.
  4. The largest and smallest bottles do not contain poison.
  5. The second bottle on the left and the second bottle on the right contain the same potion.


How to deal with this? Consider the following option. Note that, as stated in the riddle, in the row there is 1 bottle smaller than all the others in size, and 1 bottle is larger than all the others.







Let's try stupidly sorting through all the options - take one bottle and select all possible options for the content.



For example, the first bottle cannot contain a potion that pushes us forward due to restriction No. 3. It also does not contain a safe potion due to restriction No. 2 - poison cannot be located to the left of it. This leaves us with options with a poisoned potion and a potion that returns back. Let's try both options.



In the following images, green potions indicate poison, orange - safe drinks, blue - a potion moving backward, purple - moving forward.











Repeat this process for both working options - take the second bottle and alternately try all the valid content options. This will give us the following:



















Continuing to act in this vein, and discarding all working options in which a bottle cannot be filled with a potion without violating the restrictions listed above, we come to the only acceptable option:







Naturally, we had no guarantees to find a solution. There could be no solution, or there could be several (and if you have several solutions, this is equivalent to the fact that the riddle cannot be solved, since it is not known which potion is correct).



Applying the algorithm to all options gives us the following solutions. 8 puzzle options are solvable, 8 have no solutions and 26 have several solutions.







Learn more about solutions.



Is there anything in common with all the solved puzzle variants? Yes! Please note that in them the smallest or largest bottles are in 2nd or 6th places. This allows us to conclude that the 2nd and 6th bottles contain safe potions due to restrictions No. 4 and No. 5. Without this step, we cannot eliminate the possibility of poison in these bottles, and as a result we come to several possible solutions. Also solvable options require that the second “special” bottle (the smallest or largest) should be in 3rd or 4th place. Otherwise, the exact location of the potion that moves us forward cannot be found.



Summary



I will end the quote from the book.

Hermione exhaled noisily, and Harry was amazed to see that she was smiling - this was the last lesson that could have crossed his mind. “Brilliant,” said Hermione. - This is not magic - this is logic, a riddle. Many great magicians will not have a gram of logic, and they would be stuck here forever. "
But hey, maybe we can figure out the canonical version of the riddle based on the dialogue from the book:

“Got it,” she said. “The smallest bottle will lead us through the black fire, and to the Stone.”



...



"And which one will give you back through the purple fire?"



Hermione pointed to the rounded bottle on the right side of the row.
Hell. This option still gives us several solutions. Write tweets, DR.



The code



If you are interested in code to solve this puzzle and draw diagrams, you can download it here .



All Articles