'''
September 2019 Puzzler
Both of the diagrams below include "nodes" connected by edges.
Two nodes that are connected by an edge are said to be "adjacent".
For each diagram, place the integers 1, 3, 5, 7, 9, 11, and 13 in
the nodes so that the positive differences between each pair of
adjacent nodes are all different. As an example, a failed
attempt is included for the first diagram.
A F
A - B - C - D - E - F - G C - D - E
B G
Diagram 1 Diagram 2
1 - 3 - 7 - 13 - 5 - 11 - 9
2 4 6 8 6 2
Failed attempt for Diagram 1
The numbers above each of the edges in the failed attempt indicates
the positive difference between the corresponding adjacent edges. As
we can see, the positive differences are not all different (2 and 6
are repeated).
'''
from __future__ import print_function
import itertools as i
#All possible ways to arrange the numbers
allPossibilities = i.permutations([1,3,5,7,9,11,13])
straightSolns = [] #Solutions for Diagram 1
bentSolns = [] #Solutions for Diagram 2
#Test each possibility
# for diagram 1 and diagram 2
for n in allPossibilities:
#Create a set that contains all differences of nodes for
# diagram 1
straightDiffs = set()
straightDiffs.add(abs(n[0]-n[1]))
straightDiffs.add(abs(n[1]-n[2]))
straightDiffs.add(abs(n[2]-n[3]))
straightDiffs.add(abs(n[3]-n[4]))
straightDiffs.add(abs(n[4]-n[5]))
straightDiffs.add(abs(n[5]-n[6]))
#Ditto for diagram 2
bentDiffs = set()
bentDiffs.add(abs(n[0]-n[2]))
bentDiffs.add(abs(n[1]-n[2]))
bentDiffs.add(abs(n[2]-n[3]))
bentDiffs.add(abs(n[3]-n[4]))
bentDiffs.add(abs(n[4]-n[5]))
bentDiffs.add(abs(n[4]-n[6]))
#If there are 6 entries in a set (i.e. all different)
# then we have a solution
if len(straightDiffs) == 6:
straightSolns.append(n)
if len(bentDiffs) == 6:
bentSolns.append(n)
print(str(len(straightSolns)) + " Straight Solutions:")
print("Display order: 1, 2, 3, 4, 5, 6, 7 = ")
print(" 1-2-3-4-5-6-7")
print()
for n in straightSolns:
print(n)
print()
print(str(len(bentSolns)) + " Branched Solutions:")
print("Display order: 1, 2, 3, 4, 5, 6, 7 = ")
print(" 1 6")
print(" 3-4-5")
print(" 2 7")
print()
for n in bentSolns:
print(n)