'''
December Math Puzzler

Arrange each of the digits 1 through 9 in the grid below
so that all six equations (three horizontal and three
vertical) are true.

A + B * C = 17
*   *   *
D + E * F = 12
+   +   +
G + H * I = 30
=   =   =
51  17  12
  
'''
from __future__ import print_function
import itertools as i

def checkNumber(n):
  '''
  Receive a list of the digits (1->9, each used only once) and
  check the appropriate equations.  Return True if all equations
  are satified
  '''
  row0 = n[0] + n[1] * n[2] == 17
  row1 = n[3] + n[4] * n[5] == 12
  row2 = n[6] + n[7] * n[8] == 30
  col0 = n[0] * n[3] + n[6] == 51
  col1 = n[1] * n[4] + n[7] == 17
  col2 = n[2] * n[5] + n[8] == 12

  return (row0 and row1 and row2 and col0 and col1 and col2)



if __name__ == "__main__":

  #Make a list of all the possible numbers using digits 1->9, each only
  # once, in any position.
  d = list(i.permutations(range(1,10)))
  lst = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
  #Check each one to see if it meets the requirements
  for n in d: 
    if checkNumber(n):
      for m in range(9):
        print(lst[m] + " = "+str(n[m]))