Friday, 18 September 2020

Magic Triangle - Solution

I first saw this puzzle in CSIRO’s Double Helix here.

The problem we are solving is described as:

Given a triangle with circle on each point and on each side:

I first saw this puzzle in CSIRO’s Double Helix here.

The Magic Triangle problem we are solving is described as:

You are given a triangle with circle on each point and on each side:

Magic Triangle

Magic Triangle

Then, using the numbers from 1 to 6, arrange them in a triangle with three numbers on each side. Swap them around until the sides all add up to the same number.

Finally, sum each side to 10.

Method

Let’s label the triangle: starting from any vertex label the nodes:

Labelled Magic Triangle

Labelled Magic Triangle

The method to solve this problem is broken into the following steps:

  • get all permutations of numbers 1 to 6 as a, b, c, d, e, f

  • filter permutation to satisfy conditions:

    • a + b + c == c + d + e == e + f + a

    • and final condition: a + b +c == 10

Using Haskell

All permutations of numbers 1 to 6:

This will give 6! = 720 permutations.

Filter on sides summing up to the same value:

Which gives all solutions where the sides are equal sums:

  [(3,2,5),(5,4,1),(1,6,3)]
  [(2,4,3),(3,5,1),(1,6,2)]
  [(1,5,3),(3,4,2),(2,6,1)]
  [(1,4,5),(5,2,3),(3,6,1)]
  [(5,3,4),(4,2,6),(6,1,5)]
  [(6,2,4),(4,3,5),(5,1,6)]
  [(4,5,2),(2,3,6),(6,1,4)]
  [(6,3,2),(2,5,4),(4,1,6)]
  [(2,3,6),(6,1,4),(4,5,2)]
  [(4,1,6),(6,3,2),(2,5,4)]
  [(3,4,2),(2,6,1),(1,5,3)]
  [(1,6,2),(2,4,3),(3,5,1)]
  [(5,4,1),(1,6,3),(3,2,5)]
  [(4,3,5),(5,1,6),(6,2,4)]
  [(6,1,5),(5,3,4),(4,2,6)]
  [(3,6,1),(1,4,5),(5,2,3)]
  [(5,2,3),(3,6,1),(1,4,5)]
  [(2,6,1),(1,5,3),(3,4,2)]
  [(3,5,1),(1,6,2),(2,4,3)]
  [(1,6,3),(3,2,5),(5,4,1)]
  [(5,1,6),(6,2,4),(4,3,5)]
  [(2,5,4),(4,1,6),(6,3,2)]
  [(6,1,4),(4,5,2),(2,3,6)]
  [(4,2,6),(6,1,5),(5,3,4)]

Filter on sides summing to 10:

Which gives our final list of solutions:

  [(3,2,5),(5,4,1),(1,6,3)]
  [(1,4,5),(5,2,3),(3,6,1)]
  [(5,4,1),(1,6,3),(3,2,5)]
  [(3,6,1),(1,4,5),(5,2,3)]
  [(5,2,3),(3,6,1),(1,4,5)]
  [(1,6,3),(3,2,5),(5,4,1)]

Note here that the solutions aren’t unique: there are repetitions if you consider rotations or node reversals. Can we filter these out to get the only unique solution?

Try ordering:

The idea here is that as the nodes are unique, we can order them. This yields our final solution:

  [(5,2,3),(3,6,1),(1,4,5)]

Using one other Haskell refinement we can write this as:

Solved Magic Triangle

Solved Magic Triangle

Check your answer on CSIRO page here.

Using Python

Python now has list comprehensions just like many other programming languages, so the solution is much the same. Also, use the built-in permutations function from itertools:

Which yields the same results as our previous solution in Haskell:

>>> [[(5, 2, 3), (3, 6, 1), (1, 4, 5)]]