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 dcharacters.
- 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 dcharacters are required to simulate a loop. The
pjoins vines traveling northeast to east, the
qfor east to southeast, the
bfrom southeast to east, and the
dfrom 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
b(your choice, doesn't have to be random), and starts horizontally. The end of the vine must be one of
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.,
pdis 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.
- 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 dloops, are precisely one
/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.
- 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.
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.
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.
The resulting pumpkin patch, either printed/displayed to the screen or returned as a string/string-array/etc.
- This is code-golf 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.
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.