Enigmatic Code

Programming Enigma Puzzles

Enigma 1115: New Christmas star

From New Scientist #2270, 23rd December 2000

enigma-1115

Here is another “magical” Christmas star of twelve triangles, in which can be seen 
six lines of five triangles (two horizontal and two in each of the diagonal directions). Your task is to place a digit in each of the twelve triangles so that:

all six digits in the outermost “points” of the star are odd;

the total of the five digits in each line is the same,
 and it is the same as the total of the six digits in the points of the star;

each of the horizontal lines of digits, when read as a 5-digit number, is a perfect square.

What are those two perfect squares?

Thanks to Hugh Casement for providing the source for this puzzle.

[enigma1115]

Advertisements

4 responses to “Enigma 1115: New Christmas star

  1. Jim Randell 15 May 2017 at 9:51 am

    We can use the SubstitutedExpression() solver from the enigma.py library to tackle this problem.

    This run file executes in 243ms.

    #!/usr/bin/env pypy -m enigma -r
    
    # consider the star as:
    #
    #       ^
    #      /A\
    #  -----------
    #  \B/C\D/E\F/
    #   *---*---*
    #  /G\H/I\J/K\
    #  -----------
    #      \L/
    #       v
    #
    # the lines are then:
    #   BCDEF, GHIJK, BCHIL, ADEJK, ACDGH, FEJIL
    
    # solver to use
    SubstitutedExpression
    
    # solver parameters
    --distinct=""
    --invalid="02468,ABFGKL"
    --answer="ordered(BCDEF, GHIJK)"
    
    # expressions to solve
    "A + B + F + G + K + L == B + C + D + E + F"
    "B + C + D + E + F == G + H + I + J + K"
    "G + H + I + J + K == B + C + H + I + L"
    "B + C + H + I + L == A + D + E + J + K"
    "A + D + E + J + K == A + C + D + G + H"
    "A + C + D + G + H == F + E + J + I + L"
    "is_square(BCDEF)"
    "is_square(GHIJK)"
    

    Solution: The two squares are 16641 and 50625.

    16641 = 129².
    50625 = 225².

    The six odd digits in the point of the star sum to 1+1+1+5+5+5 = 18, as do the digits on the six lines identified in the diagram.

    The ordered() function, recently added to the enigma.py library, just returns its arguments, sorted with sorted(), as a tuple().

  2. Brian Gladman 15 May 2017 at 4:35 pm
    from collections import defaultdict
    from itertools import combinations
    
    #                    ^
    #                   / \
    #                  / A \
    #                 /     \
    #        +-------+-------+-------+
    #         \     / \     / \     /
    #          \ B / C \ D / E \ F /
    #           \ /     \ /     \ /
    #            +-------+-------+
    #           / \     / \     / \
    #          / G \ H / I \ J / K \
    #         /     \ /     \ /     \
    #        +-------+-------+-------+
    #                 \     /
    #                  \ L /
    #                   \ / 
    #                    v
    
    # map the digit sums of five digit squares to lists of 
    # squares (and tuples of their digits) with these sums
    sm2sq = defaultdict(list)
    for s in (x * x for x in range(100, 317)):
      dgts = tuple(int(x) for x in str(s))
      if dgts[0] % 2 and dgts[-1] % 2:
        sm2sq[sum(int(x) for x in str(s))].append((s, dgts))
    
    # consider each possible digit sum  
    for sm, sql in sm2sq.items():
      if len(sql) < 2:
        continue
      # consider each possible combination of two squares
      for (s1, d1), (s2, d2) in combinations(sm2sq[sm], 2):
        # extract the five digits of the two squares
        B, C, D, E, F = d1
        G, H, I, J, K = d2
        # use two diagonal sums to compute A and L
        L = sm - (B + C + H + I)
        A = sm - (C + D + G + H)
        # check that A and L are both odd
        if not A & 1 == L & 1 == 1:
          continue
        # check for a correct sum of outer triangle values 
        if sum((A, B, F, G, K, L)) != sm:
          continue
        # check that the remaining two diagonal sums are correct
        if all(sum(x) == sm for x in ((E, F, I, J, L), (A, D, E, J, K))):
          print(s1, s2)
    
    • Jim Randell 18 May 2017 at 5:29 pm

      The conditions of the problem don’t require that the two squares are different, so we can use itertools.product() (or, if we want to remove duplicate solutions that are horizontal reflections, itertools.combinations_with_replacement()).

      from collections import defaultdict
      from itertools import product
      from enigma import irange, nsplit, printf
      
      # 5 digit squares that start and end with odd digits
      # record the digits by digit sum
      squares = defaultdict(list)
      for i in irange(101, 316, step=2):
        ds = nsplit(i * i)
        if ds[0] % 2 == 0: continue
        squares[sum(ds)].append(ds)
      
      # now find 2 squares with the same sum
      for (s, sqs) in squares.items():
        for ((B, C, D, E, F), (G, H, I, J, K)) in product(sqs, repeat=2):
          # compute A and L
          (A, L) = (s - (D + E + J + K), s - (B + C + H + I))
          if A < 0 or L < 0 or A > 9 or L > 9 or A % 2 == 0 or L % 2 == 0: continue
          # check the remaining lines
          if not(s == A + C + D + G + H == E + F + I + J + L == A + B + F + G + K + L): continue
          # output the solution
          printf("BCDEF={B}{C}{D}{E}{F} GHIJK={G}{H}{I}{J}{K} A={A} L={L}")
      
  3. geoffrounce 15 May 2017 at 8:07 pm

    I found two arrangements for the triangle letters (A – L) making up the star ie

    % [A, B, C, D, E, F, G, H, I, J, K, L]
    % [5, 5, 0, 6, 2, 5, 1, 6, 6, 4, 1, 1]
    % [1, 1, 6, 6, 4, 1, 5, 0, 6, 2, 5, 5]

    % A Solution in MiniZinc
    include "globals.mzn";
    
    % Variables for all triangles of the star
    var 0..9:A; var 1..9:B; var 0..9:C; var 0..9:D; var 0..9:E; 
    var 0..9:F; var 1..9:G; var 0..9:H; var 0..9:I; var 0..9:J;
    var 0..9:K; var 0..9:L; 
    
    % All corner triangles are odd numbers
    constraint A mod 2 == 1 /\ B mod 2 == 1 /\ F mod 2 == 1
    /\ G mod 2 == 1 /\ K mod 2 == 1 /\ L mod 2 == 1;
    
    % Sum of corner triangles
    var 1..60: corners = A + B + F + G + K + L;
    
    % Sum of each of six lines = sum of corner triangles A,B,F,G,K,L
    constraint (B + C + D + E + F) == corners
    /\ (G + H + I + J + K) == corners /\ (B + C + H + I + L) == corners
    /\ (A + D + E + J + K) == corners /\ (A + C + D + G + H) == corners
    /\ (F + E + J + I + L) == corners;
    
    % Two squares are BCDEF and GHIJK
    var 10000..99999: BCDEF = 10000*B + 1000*C + 100*D + 10*E + F; 
    var 10000..99999: GHIJK = 10000*G + 1000*H + 100*I + 10*J + K;
    
    set of int: sq5 = {n*n | n in 100..316};
    
    constraint BCDEF in sq5 /\ GHIJK in sq5;
    
    solve satisfy;
    
    output ["Two five digit squares are " ++ show(BCDEF) ++ " and " ++ show(GHIJK) ];
    
    % Multiple output configuration:
    % Two five digit squares are 50625 and 16641
    % Two five digit squares are 16641 and 50625
    % Finished in 128msec
    

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: