Solving the “No friends” math puzzle with a python algorithm

Emmanuel Kwakye Nyantakyi
4 min readJun 12, 2021

Place each of the digits 1,2,3,4,5,6,7 and 8 in separate boxes so that boxes that share common corners do not contain successive digits.

Figure 1: Boxes in problem statement

I’m sure more than half of you reading this have tried solving the puzzle above. If you were able to, bravo! Keep reading to see an algorithm I wrote to help solve the puzzle. If you weren’t able to, don’t worry. Check out my solution and how I used it to develop a algorithm to help tackle the problem.

Solving the problem on paper

To really understand what I needed to do, I rephrased the problem in my own words. Basically, the problem is asking us to put each of the digits into separate boxes such that the digits in any two boxes that share at least one corner have a difference greater than 1 (that is, if x and y are neighbors, then |a-b| should be greater than 1). I called digits of boxes that share common corners neighbors and any two digits with a difference of 1 friends. By that definition, any two digits that have a difference greater than 1 are strangers.

The next thing I did was classify the boxes into three different types depending on how many neighbors they have.

Figure 2: Boxes classified according to the number of neighbors

From figure 2, it can be seen that boxes A have three neighbors each, boxes B have four neighbors each and boxes C have 6 neighbors each. This means, for the right arrangement, boxes A, B and C should be surrounded by three, four and six strangers respectively. *You might want to get a pencil and a paper at this point* The next thing I did was find possible digits from the eight that could occupy the different boxes: this way I eliminated the digits till they had all been placed in a box. I started with boxes C because they are the most critical boxes. From the eight digits only 1 and 8 can have up to 6 strangers. And so I put 1 and 8 in the C boxes. It can be seen that the two Cs have only one friend and it turns out one C’s friend happens to be a stranger to the other C. This lead me to put the two digits(2 and 7) into boxes A, so that the friend of one C is behind the other C. So I put 2 behind 8 and 7 in front of 1. This left me with four more digits (3,4,5, and 6) to place in boxes B. At this stage, I just put any two digits who are strangers at the top such that the bottom two are also strangers. But I checked to make sure I did not make any friends in boxes B and boxes A neighbors. I ended up with {3,5} on top and {4,6} at the bottom.

Figure 3: My solution to the problem

After a careful look, one can notice that there are two more ways of arranging the digits to solve the problem:

  1. The whole figure has a vertical line of symmetry. By interchanging all values on the left of that line of symmetry with the values on the right, you form a different solution.
  2. Also, by switching the B_top values with the B_bottom values, you form another solution.

Writing an algorithm to solve the puzzle

Based on my reasoning above, I decided to write an algorithm that would do the sorting of the digits into different box types for me. I currently don’t have a pc so I had to write the code on my phone using Cocalc.. forgive the messy code;( . I wrote the code such that it should work for any 8 consecutive numbers which are ordered or not. I also altered the box types a bit to fit the code.

Figure 4: New classification of boxes for code

Code

Figure 5: Screenshot of Code on Cocalc Python Compiler
Figure 6: Screenshot of Code on Cocalc Python Compiler
Figure 7: Testing function with ordered and disordered number lists

Essentially, the code sorts out which pairs belong to which types of boxes, but the user ultimately decides which digit or number in the pair comes first in accordance with the rules of the puzzle.

It would be fascinating to see other ways of solving the puzzle in the comments sections.

Don’t forget to leave a like:)

--

--