Enigmatic Code

Programming Enigma Puzzles

Enigma 1631: Joe’s Pyramid

From New Scientist #2797, 29th January 2011 [link]

This is Joe’s pyramid. Every stone is marked with a different one or two digit positive number.

Where a stone rests on two others, its number is the sum of the numbers marked on the two stones on which it rests.

What number is X?

[enigma1631]

2 responses to “Enigma 1631: Joe’s Pyramid

  1. Jim Randell 13 December 2011 at 10:57 pm

    This Python program runs in 815ms – I can get it down to 280ms by explicitly coding up the sums involved, but this is much neater.

    # the layers are:
    # A B C D E F
    # A+B B+C C+D D+E E+F
    # A+2B+C B+2C+D C+2D+E D+2E+F
    # A+3B+3C+D B+3C+3D+E C+3D+3E+F
    # A+4B+6C+4D+E B+4C+6D+4E+F
    # A+5B+10C+10D+5E+F < 100 => C,D < 8, B,E < 11, A,F < 21
    
    def squish(l):
      if len(l) < 2: return []
      return [l[0] + l[1]] + squish(l[1:])
    
    def check(l):
      s = set(l)
      while len(l) > 1:
        l2 = squish(l)
        if max(l2) > 99: return False
        n = len(s)
        s.update(l2)
        if len(s) < n + len(l2): return False
        l = l2
      return True
    
    for C in range(1, 8):
      for D in range(C + 1, 8):
        for B in set(range(1, 11)).difference((C, D)):
          if not check([B, C, D]): continue
          for E in set(range(1, 11)).difference((C, D, B)):
            if not check([B, C, D, E]): continue
            for A in set(range(1, 21)).difference((C, D, B, E)):
              if not check([A, B, C, D, E]): continue
              for F in set(range(1, 21)).difference((C, D, B, E, A)):
                l = [A, B, C, D, E, F]
                if not check(l): continue
    
                while len(l) > 0:
                  print(' '.join(map(str, l)))
                  l = squish(l)
    

    Solution: X = 98.

  2. geoffrounce 7 December 2017 at 4:36 pm

    Another pyramid – I found two solutions, one bring a mirror image of the other, with X = 98 in both cases

    % A Solution in MiniZinc
    include "globals.mzn";
    
    %        Joe's Pyramid                Solution No 1 (X = 98)       Solution No 2 (X = 98)              
    
    %              X                            98                          98
    %            A   B                        52  46                      46  52
    %          C   D   E                    32  20  26                  26  20  32
    %        F   G   H   I                 21 11  9  17               17  9  11  21       
    %      J   K   L   M   N              14  7  4  5  12            12  5  4   7   14
    %    P   Q   R   S   T   U           8   6  1  3  2  10        10  2  3   1   6    8
    %
    
    var 1..99:X;   var 1..99:A;   var 1..99:B;   var 1..99:C;
    var 1..99:D;   var 1..99:E;   var 1..99:F;   var 1..99:G;
    var 1..99:H;   var 1..99:I;   var 1..99:J;   var 1..99:K;
    var 1..99:L;   var 1..99:M;   var 1..99:N;   var 1..99:P;
    var 1..99:Q;   var 1..99:R;   var 1..99:S;   var 1..99:T;
    var 1..99:U;
    
    constraint all_different ( [X,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U] );
    
    constraint X == A + B;
    constraint C + D == A /\ D + E == B;
    constraint F + G == C /\ G + H == D /\ H + I == E;
    constraint J + K == F /\ K + L == G /\ L + M == H /\ M + N == I;
    constraint P + Q == J /\ Q + R == K /\ R + S == L /\ S + T == M /\ T + U == N;
    
    solve satisfy;
    
    output [ "[X,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U]  = " ++ 
    show ( [X,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U] ) ];
    
    % [X,   A,  B,  C,  D,  E,  F,  G, H,  I,  J, K, L, M, N,  P, Q, R, S, T, U]  = 
    % [98, 52, 46, 32, 20, 26, 21, 11, 9, 17, 14, 7, 4, 5, 12, 8, 6, 1, 3, 2, 10]
    
    % 2nd solution
    % [X,   A,  B,  C,  D,  E,  F, G,  H,  I,  J, K, L, M,  N,  P, Q, R, S, T, U]  = 
    % [98, 46, 52, 26, 20, 32, 17, 9, 11, 21, 12, 5, 4, 7, 14, 10, 2, 3, 1, 6, 8]
    
    % ----------
    % Finished in 61msec
    
    

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: