30
\$\begingroup\$

Write a program (or function) that takes in a positive integer.

If the input is 1, print (or return) two diamonds neighboring side-by-side, each with a side length of 1 slash:

/\/\
\/\/

For every input N greater than 1, look at the output for N-1 and for each pair of neighboring diamonds, insert a new diamond in between them whose side length is the sum of the side lengths of the two neighbors. Print (or return) this new diamond pattern.

So when 2 is input, we look at the output for 1 and can see that there are two neighboring diamonds, both with side length 1. So we insert a side length 2 (1+1) diamond in between them:

   /\
/\/  \/\
\/\  /\/
   \/

For input 3 we look at the output for 2 and add two diamonds with side length 3 (1+2 and 2+1) in between the two pairs of neighboring diamonds:

    /\        /\
   /  \  /\  /  \
/\/    \/  \/    \/\
\/\    /\  /\    /\/
   \  /  \/  \  /
    \/        \/

Continuing the pattern, the output for 4 is:

                    /\            /\
     /\            /  \          /  \            /\
    /  \    /\    /    \        /    \    /\    /  \
   /    \  /  \  /      \  /\  /      \  /  \  /    \
/\/      \/    \/        \/  \/        \/    \/      \/\
\/\      /\    /\        /\  /\        /\    /\      /\/
   \    /  \  /  \      /  \/  \      /  \  /  \    /
    \  /    \/    \    /        \    /    \/    \  /
     \/            \  /          \  /            \/
                    \/            \/

And so on.

Your outputs may have trailing spaces on any lines but only up to one trailing newline (and no leading newlines).

The shortest code in bytes wins.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Relevant OEIS: oeis.org/A002487. \$\endgroup\$
    – orlp
    Commented Sep 4, 2015 at 2:09

5 Answers 5

9
\$\begingroup\$

Pyth, 50 49 bytes

L.rR"\/"_bjbyK.tsm+Jm+*\ k\\dyJu.iGsM.:G2tQjT9djK

Demonstration

Explanation:

L.rR"\/"_bjbyK.tsm+Jm+*\ k\\dyJu.iGsM.:G2tQjT9djK
                                                     Implicit:
                                                     Q = eval(input())
                                                     T = 10
                                                     d = ' '
                                                     b = '\n'
L                                                    def y(b): return
 .rR"\/"                                             Swap \ and / in
        _b                                           reversed input.
                                                     This effectively vertically
                                                     mirrors the input.
                               u                     Apply the function repeatedly
                                           jT9       Starting with [1, 1]
                                         tQ          and repeating Q - 1 times
                                .iG                  interlace G (input) with
                                     .:G2            All 2 element substrings of G
                                   sM                mapped to their sums.
                 m                                   map over these values
                                                     implicitly cast to ranges
                    m       d                        map over the range values
                                                     impicitly cast to ranges
                     +*\ k\\                         to k spaces followed by
                                                     a backslash.
                   J                                 Save to J, which is roughly:
                                                     \
                                                      \
                  +          yJ                      And add on y(J), giving
                                                     \
                                                      \
                                                      /
                                                     /
                s                                    Combine the half diamonds
                                                     into one list.
              .t                              d      Traspose, filling with ' '.
             K                                       Save to K, giving
                                                     something like:
                                                     \  /
                                                      \/
            y                                        Vertically mirror.
          jb                                         Join on newlines and print.
                                               jK    Join K on (implicitly)
                                                     newlines and print.
\$\endgroup\$
2
  • 1
    \$\begingroup\$ What are the odds? I also exactly have u.iGsM.:G2tQjT9 in my (partial) solution. I never looked at your answer... \$\endgroup\$
    – orlp
    Commented Sep 4, 2015 at 15:18
  • 2
    \$\begingroup\$ @orlp There's often only one best way to do something. \$\endgroup\$
    – isaacg
    Commented Sep 4, 2015 at 15:33
5
\$\begingroup\$

Common Lisp, 425

(labels((a(n)(if(> n 1)(loop for(x y)on(a(1- n))by #'cdr collect x when y collect(+ x y))'(1 1))))(lambda(~ &aux(l(a ~))(h(apply'max l))(w(*(apply'+ l)2))(o(* 2 h))(m(make-array(list o w):initial-element #\ ))(x 0)(y h))(labels((k(^ v)(setf(aref m y x)^(aref m(- o y 1)x)v)(incf x))(d(i)(when(plusp i)(k #\\ #\/)(incf y)(d(1- i))(decf y)(k #\/ #\\))))(mapc #'d l))(dotimes(j o)(fresh-line)(dotimes(i w)(princ(aref m j i))))))

Example

(funcall *fun* 4)

                    /\            /\                    
     /\            /  \          /  \            /\     
    /  \    /\    /    \        /    \    /\    /  \    
   /    \  /  \  /      \  /\  /      \  /  \  /    \   
/\/      \/    \/        \/  \/        \/    \/      \/\
\/\      /\    /\        /\  /\        /\    /\      /\/
   \    /  \  /  \      /  \/  \      /  \  /  \    /   
    \  /    \/    \    /        \    /    \/    \  /    
     \/            \  /          \  /            \/     
                    \/            \/                    

Ungolfed

(labels
    ((sequence (n)
       (if (> n 1)
           (loop for(x y) on (sequence (1- n)) by #'cdr
                 collect x
                 when y
                   collect(+ x y))
           '(1 1))))
  (defun argyle (input &aux
                  (list (sequence input))
                  (half-height (apply'max list))
                  (width (* (apply '+ list) 2))
                  (height (* 2 half-height))
                  (board (make-array
                          (list height width)
                          :initial-element #\ ))
                  (x 0)
                  (y half-height))
    (labels ((engrave (^ v)
               (setf (aref board y              x) ^ ;; draw UP character
                     (aref board (- height y 1) x) v ;; draw DOWN character (mirrored)
                     )
               (incf x) ;; advance x
               )
             (draw (i)
               (when (plusp i)
                 (engrave #\\ #\/)  ;; write opening "<" shape of diamond
                 (incf y)
                 (draw (1- i))   ;; recursive draw
                 (decf y)
                 (engrave #\/ #\\)  ;; write closing ">" shape of diamond
                 )))
      ;; draw into board for each entry in the sequence
      (mapc #'draw list))

    ;; ACTUAL drawing
    (dotimes(j height)
      (fresh-line)
      (dotimes(i width)
        (princ (aref board j i))))
    board))
\$\endgroup\$
5
\$\begingroup\$

CJam, 59 58 57 bytes

YXbri({{_2$+\}*]}*:,_:|,S*0'\tf{fm>_W%'\f/'/f*}:+zN*_W%N@

Thanks to @MartinBüttner for golfing off 1 byte.

Try it online in the CJam interpreter.

Idea

For input 3, for example, we generate

\  
/  
\  
 \ 
  \
  /
 / 
/  
\  
 \ 
 / 
/  
\  
 \ 
  \
  /
 / 
/  
\  
/  

by rotating the string  \ and replacing some backslashes with slashes.

Then, we zip the resulting array (transpose rows and columns) to obtain the lower half of the desired output.

The upper half is byte per byte equal to the lower half in reverse.

Code

YXb     e# Push A := [1 1] and 2 in unary.
ri(     e# Read an integer fro STDIN and subtract 1.
{       e# Do the following that many times:
  {     e#   For each I in A but the first:
    _2$ e#     Push a copy of I and the preceding array element.
    +\  e#     Compute the sum of the copies and swap it with I.
  }*    e#
  ]     e#   Collect the entire stack in an array.
}*      e#
:,      e# Replace each I in A with [0 ... I-1].
_       e# Push a copy of A.
:|      e# Perform set union of all the ranges.
,S*     e# Get the length (highest I in A) and push a string of that many spaces.
0'\t    e# Replace the first space with a backslash.
f{      e# For each range in A, push the generated string; then:
  fm>   e#   Rotate the string by each amount in the array.
  _W%   e#   Push a reversed copy of the resulting array of strings.
  '\f/  e#   In each string, split at backslashes.
  '/f*  e#   Join each string, separating with slashes.
}       e#
:+      e# Concatenate the resulting arrays of strings.
zN*     e# Zip and join, separating by linefeeds.
_W%     e# Push a reversed copy of the string.
N@      e# Push a linefeed and rotate the original string on top of it.
\$\endgroup\$
1
\$\begingroup\$

Rev 1: Ruby 170

New method avoiding creating the big diamond and reducing.

->n{a=[1]
m=1<<n-1
(m-1).times{|i|a<<a[i]<<a[i]+a[i+1]}
(-b=a.max).upto(b-1){|j|0.upto(m){|i|d=' '*q=a[-i]*2
(j*2+1).abs<q&&(d[j%q]=?\\;d[-1-j%q]=?/)   
print d}
puts""}}

Rev 0: Ruby, 187

->n{a=[1]
m=1<<n-1
(m-1).times{|i|a<<a[i]<<a[i]+a[i+1]}
(2*b=a.max).times{|j|
0.upto(m){|i|d=' '*b*2;d[(b+j)%(b*2)]='\\';d[(b-1-j)%(b*2)]=?/
r=b-a[-i]
d.slice!(b-r,r*2)
print d}
puts ""}}

The sizes of the diamonds are calculated in accordance with the recurrence relation from https://oeis.org/A002487 Thus we make the array a containing all the elements for all the rows from 1 to n. We are only interested in the last 1<<n-1 elements (Ruby allows us to get them from the array using negative indexes, -1 being the last element in the array), plus an inital 1 from position 0.

Line by line and diamond by diamond, we draw the row of characters for the biggest diamond, then cut out the middle columns to get the row for the required diamond. Rev 1 is shorter, but I liked this method.

Modular arithmetic is used to wrap around so that the same expression adds all / directly and likewise one expression adds all \ directly.

Ungolfed in test program

f=->n{
  a=[1]
  m=1<<n-1
  (m-1).times{|i|a<<a[i]<<a[i]+a[i+1]}                   #concatenate a[i] and a[i]+a[i+1] to the end of a
    (2*b=a.max).times{|j|                                #run through lines (twice the largest number in a
      0.upto(m){|i|                                      #run through an initial '1' plus the last m numbers in a
      d=' '*b*2;d[(b+j)%(b*2)]='\\';d[(b-1-j)%(b*2)]=?/  #d is the correct string for this line of the largest diamond
      r=b-a[-i]                                          #calculate number of characters to be deleted from middle of d
      d.slice!(b-r,r*2)                                  #and delete them
      print d                                            #print the result
    }
    puts ""                                              #at the end of the line, print a newline
  }
}

f.call(gets.to_i)
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 26 bytes

k₁$‹(:zṠY)ƛɾ\\↲øm↵∩Ṙ;føm↵§

Try it Online! Inspired by Dennis's CJam answer.

k₁                         # Starting with [1, 1]
  $‹                       # n-1 times...
    (----)                 # Do the following
     :  Y                  # Interleave with
      zṠ                   # Pairwise sums
          ƛ         ;      # Over each number
            \\↲            # Pad "\" to length
           ɾ               # Each of 1...n
               øm↵         # Mirror
                  ∩Ṙ       # Transpose and reverse
                     føm↵  # Flatten and mirror again
                         § # Transpose again
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.