12
\$\begingroup\$

I was walking through a pumpkin patch the other day for a birthday party, and noticed the pumpkin vines making a nifty pattern, with whirls, loops, and offshoots. We're going to simulate that here with some ASCII art.

          (())
            \
   p--q      p-----q
  /    \    /       \
(())    b--d       (())

Vine Construction Rules

  • There is only one main vine, composed solely of \ / - p q b d characters.
  • The vine only travels from left to right across the screen. In other words, suppose you were an ant starting at the left-most vine character. As you progress to the next adjacent character on the main vine, you must move one column to the right - never to the left.
  • When the vine changes direction, one of the p q b d characters are required to simulate a loop. The p joins vines traveling northeast to east, the q for east to southeast, the b from southeast to east, and the d from east to northeast. Note that the "loop" of the letter connects to the horizontal vine, and the "stem" of the letter connects to the diagonal.
  • The beginning of the vine must be one of p or b (your choice, doesn't have to be random), and starts horizontally. The end of the vine must be one of q or d (your choice, doesn't have to be random), and must end horizontally.
  • Note that loops can be placed immediately adjacent to other loops (e.g., pd is a valid substring of the vine), but that may make it more difficult to place the pumpkins later. You may want to always have one of - / \ immediately after a loop (as I did in my examples), but it's not required.

Pumpkin Rules

  • The pumpkins are composed solely of (()) (this exact string).
  • From the main vine, the pumpkins are attached by offshoots. These offshoots can only be attached to the p q b d loops, are precisely one \ or / in length, and attach to the pumpkin so the "end" of the offshoot is in the middle.
  • They can connect above or below the main vine.
  • The offshoots can connect going to the "left."
  • Only one pumpkin can attach per offshoot, and only one offshoot per loop.

Randomness

  • When traveling horizontally, the vine has a 50% chance of continuing horizontally, a 25% chance of turning northeast, and a 25% chance of turning southeast.
  • When traveling diagonally, the vine has a 90% chance of turning horizontal and a 10% chance of continuing diagonally.
  • There must be sufficient turns to support the input number of pumpkins, though more turns are allowed.
  • Once the vine is constructed, pumpkins can be randomly placed at any corner not already occupied by a pumpkin.
  • Pumpkins cannot overlap the vine or other pumpkins.

The Challenge

Given an input number, output a randomized pumpkin patch following the above rules. Executing the code multiple times with the same input should yield different results. All possible pumpkin patches for a given input number should have some (not necessarily equal) non-zero chance of occurring.

Input

A single integer n representing the number of pumpkins in the patch, in any convenient format. For brevity of code, you can assume the input is 0 < n < 256.

Output

The resulting pumpkin patch, either printed/displayed to the screen or returned as a string/string-array/etc.

Rules

  • This is so all the usual rules for golfing apply, and the shortest code (in bytes) wins.
  • Use our standard definition of "Random."
  • Either a full program or function are acceptable.
  • Standard loopholes are forbidden.
  • Leading and trailing whitespace / newlines are all completely optional.

Examples

For input n = 3, here are some VALID examples of a pumpkin patch following the above rules (separated by blank newlines).

          (())
            \
   p--q      p-----q
  /    \    /       \
(())    b--d       (())

(()) (())
  \   /
   b-q (())
      \ /
       b-q

p-----------------------q (())
                       / \ /
                     (()) b-q
                           /
                         (())

Here are some INVALID examples for input n = 3, with explanations #.

    (()) (())
     /    /
p---q----q
 \
(())
# The vine continued horizontally after a loop was placed.

(()(())
  \ /
   p---q
        \
       (())
# The pumpkins are both overlapping and sprouting from the same loop.

p----------------q
 \      \         \
(())   (())      (())
# The pumpkin is attached to the middle of the vine, not at a loop.
\$\endgroup\$
  • 7
    \$\begingroup\$ And now design a 2D language using this as the syntax. :) \$\endgroup\$ – Martin Ender Oct 12 '16 at 18:37
1
\$\begingroup\$

Python 2, 819 bytes

Takes n as input

Always places pumkpins on the 'outside' of corners (randomly left/right)

While the vine is constructed, pumpkins are added, and when enough pumpkins are there, the vine stops.

r=lambda:__import__('random').random()
s=1
v=[s]*4
U=[-9]
D=[-9]
i=input()
while len(U)+len(D)<i+2:s=[[0,1][r()<.9],[[0,2][r()<.5],1][r()<.5],[2,1][r()<.9]][s];exec['',[['','U+=[len(v)]'][U[-1]<len(v)-7],'',['','D+=[len(v)]'][D[-1]<len(v)-7]][v[-1]-s+1]][r()<.8];v+=[s]*[1,2][v[-1]!=s]
v+=[1]*5
m=M=s=0
for i in v:s+=i-1;m=[m,s][m>s];M=[M,s][M<s]
R=[[' ']*(M-m+5)for x in v]
m=-m+2
R[2][m]='p'
for x in range(3,len(v)-3):X=v[x-1];R[x][m]='/d p-b q\\'[v[x]*3+X];m+=v[x]-1
R[-3][m]='q'
M=[len(a)-len(a.lstrip())for a in map(''.join,R)]
R=map(list,zip(*R))
B,L,a='-/U'
K="\\"*4
W="""exec("for p in "+a+"[1:]:x=M[p];b=r()<.5;exec('R[x"+B+"1][p'+['+1]=\\""+L+"\\"','-1]=\\""+K+"\\"'][b]);i=p-[0,3][b];l='(';exec('R[x"+B+"2][i]=l;i+=1;'*2+'l=\\")\\";')*2")"""
exec W+";B,a='+D';L,K=K,L;"+W
for x in R:print''.join(map(str,x))

Examples:

n=4

                (())   
                 /     
                p---q  
 (())          /       
   \       p--d        
  p-q     /    \       
     \   /    (())     
      b-d              
       \               
      (())             

n=20

                            (())                                                                                             
                              \                                                                                              
                            p--q                                                                                             
                           /    \                                                                                            
                          /      b--q                                                                                        
           (())     p----d      /    \                                                                                       
  (())       \     /          (())    b-q (())                                                                               
    \         p---d                      \  \                                                                                
  p--q       /     \                      b--q                                                                               
      \     /     (())                   /    \                        (())                                           (())   
       b---d                           (())    b-q                       \                                             /     
        \                                         \          (())         p-q                                         p---q  
       (())                                        \           \         /   \                                       /       
                                                    b-----------q     p-d     b-q                            (())p--d        
                                                                 \   /       /   \                            / /    \       
                                                                  b-d      (())   b-q   (())  (())   p-q     p-d    (())     
                                                                   /                 \   /      \   /   \   /                
                                                                 (())                 b-q        p-d     b-d                 
                                                                                       \ \      /         \                  
                                                                                      (())b----d         (())                
                                                                                              /                              
                                                                                            (())                             
\$\endgroup\$
  • \$\begingroup\$ Congrats, you win by default! :D \$\endgroup\$ – AdmBorkBork Oct 19 '16 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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