4
\$\begingroup\$

Your task is to draw a requested number of cells in a flat-topped/pointy-sided hex pattern. Each cell is 5 characters high and 8 characters wide (4 characters wide at the top and bottom).

This is a valid layout for 6 cells:

          ____
         /    \
    ____/      \____
   /    \      /    \
  /      \____/      \
  \      /    \      /
   \____/      \____/
   /    \      /
  /      \____/ 
  \      /    \ 
   \____/      \
        \      /
         \____/

You can choose the exact layout, as long as you follow these rules:

  1. All cells must be touching, and laid out within a regular hex grid pattern.
  2. If there are >= 3 cells, every cell must have at least one vertex that is part of 3 cells.
  3. The difference in length between the longest rows in each dimension (northeast and southeast) must be <= 2. (The above diagram has longest rows of 3 and 2).

That is, you can't simply make a long line of cells, they must expand out in 2 dimensions at roughly even pace. (If it turns out the formulated rules are insufficient to enforce this goal, they will be extended, no loopholes allowed).

Input

The number of cells to draw, an integer N >= 1. (You can take that integer as 0-based if you like.)

Output

The text output in the format above, made only of /\_ and space characters. Extra whitespace is fine.

(Formats such as a string of newline-separated text, arrays of arrays of characters etc, are fine).

Scoring

Code golf.

Sample data

N=1

  ____
 /    \
/      \
\      /
 \____/

N=2

  ____
 /    \
/      \
\      /
 \____/
 /    \
/      \
\      /
 \____/

N=3

  ____
 /    \
/      \____
\      /    \
 \____/      \
 /    \      /
/      \____/
\      /
 \____/

N=4 INVALID

  ____        ____
 /    \      /    \
/      \____/      \
\      /    \      /
 \____/      \____/
 /    \      /
/      \____/
\      /
 \____/

(Longest dimensions are 3 and 2, but there is a cell with no vertex touching 3 cells)

N=4 OK

  ____ 
 /    \
/      \____
\      /    \
 \____/      \
 /    \      /
/      \____/
\      /    \
 \____/      \
      \      /
       \____/

(Longest dimensions are 2 and 2)

N=5 INVALID

                    ____
                   /    \
  ____        ____/      \
 /    \      /    \      /
/      \____/      \____/
\      /    \      /
 \____/      \____/
 /    \      /
/      \____/
\      /
 \____/

(Longest dimensions are 4 and 2, but there are two cells with no vertex touching 3 cells)

N=5 OK

    ____        ____
   /    \      /    \
  /      \____/      \
  \      /    \      /
   \____/      \____/
   /    \      /    \
  /      \____/      \
  \      /    \      /
   \____/      \____/

(Longest dimensions are 3 and 3)

N=8

          ____
         /    \
    ____/      \____
   /    \      /    \
  /      \____/      \____
  \      /    \      /    \
   \____/      \____/      \
   /    \      /    \      /
  /      \____/      \____/
  \      /    \      /
   \____/      \____/
        \      /
         \____/
\$\endgroup\$
7
  • \$\begingroup\$ Related: codegolf.stackexchange.com/questions/70166/… (The big differences here: no need to label, also you need to handle any number of cells, not just regular grids). \$\endgroup\$ Commented May 25, 2025 at 10:27
  • \$\begingroup\$ The "7 cell" layout at the start is actually 6 cells. \$\endgroup\$ Commented May 25, 2025 at 19:42
  • \$\begingroup\$ Thanks, I changed it from a 7 cell layout, forgot to update the text. \$\endgroup\$ Commented May 25, 2025 at 22:01
  • \$\begingroup\$ What does longest dimension mean? \$\endgroup\$ Commented May 26, 2025 at 4:24
  • \$\begingroup\$ That should read, "longest row in each dimension", as spelt out in the rules. \$\endgroup\$ Commented May 26, 2025 at 4:26

3 Answers 3

4
\$\begingroup\$

Ruby, 157 bytes

->n{s="%#{w=9*v=(n**0.5).round}s"%$/*w
n.times{|i|5.times{|j|s[i%v*6+(j+i%v%2*2+i/v*4)*w+2*k=0**j,m=8>>k]='____ \____/ \      //      \ /    \ '[-j*8,m]}}
s}

Try it online!

Anonymous function taking an integer n as an argument and returning a newline-separated string with v=round(sqrt(n)) columns of hexagons.

Fairly straightforward: make a canvas s of w newline separated strings of w-1 spaces, and iterate through hexagons, positioning hexagon number i horizontally at (i%v*6 , i%v%2*2 + i/v*4) and run through 5 rows with inner j loop replacing the existing spaces with hexagon parts.

The only wrinkle is that we replace 8 characters for j=1 through j=4 but only 4 for j=0 because we don't want to plot any spurious characters next to the top of the hexagon (they could be either spaces or slashes depending if there are other hexagons above it to the left or right.) Therefore we set up a variable k=0**j which is 1 when j=0 (0 otherwise) and use it to reduce the length of the string replaced from 8 to 4 by rightshifting. The string of hexagon parts is indexed with -j*8 which picks hexagon parts from the right for j=1 through j=4 and picks up the short part ____ from the left for j=0, avoiding any issues with indexing of different length strings.

\$\endgroup\$
3
\$\begingroup\$

Jelly,  72  53 bytes

Made a bunch of improvements, including moving away from a spiral to a partially filled almost-square (like Level River St's Ruby answer).

Perhaps 52 bytes, but maybe cases like \$n=14\$ aren't compact enough?

Ḷd½+.ḞƊ×2,3ḂU+ƊḤ+8ŒṬ€Ịa⁶»“⁹Ḍ36ạɠḟ⁶⁼6’ṃ“ \/_”¤s8¤UṚ€»/

A monadic Link that accepts a positive integer, \$n\$, and yields a list of lists of characters (lines) representing an area of \$n\$ hegagons.

Try it online! (The footer removes all the extra whitespace that would otherwise be present.)

How?

First create the locations of the hexagons:

Ḷd½+.ḞƊ×2,3ḂU+ƊḤ - Link: positive integer, N
Ḷ                - lowered range -> [0..N-1]
      Ɗ          - last three links as a monad - f(N):
  ½              -   square-root
   +.            -   add a half
     Ḟ           -   floor
 d               - {[0..N-1]} divmod {rounded root}
       ×2,3      - multiply these pairs by [2,3]
              Ɗ  - last three links as a monad - f(those):
           Ḃ     -   mod two
            U    -   reverse each
             +   -   add
               Ḥ - double

Then convert these locations to canvases of space characters

+8ŒṬ€Ịa⁶ - ...continued chain
+8       - add eight to every value
  ŒṬ€    - for each: matrix of zeros of that size with a 1 at the bottom right
     Ị   - insignificant? (convert all zeros to ones)
      a⁶ - logical AND space character

Now make a single hexagon (with rows in reverse):

“...’ṃ“ \/_”¤s8¤
               ¤ - nilad followed by link(s) as a nilad:
            ¤    -   nilad followed by link(s) as a nilad:
“...’            -     529086439416644320972805
     ṃ“ \/_”     -     convert to base 4 with digits 0-4 as "_ \/"
             s8  -   split into chunks of length eight -> ReversedHexagon

Now format the hexagon into each canvas and merge them together:

» ^^^ UṚ€»/
» ^^^       - {canvases} maximum {ReversedHexagon} (vectorises)
               (making the top-left of each canvas a ReversedHexagon)
      U     - reverse each row of each
               (placing the ReversedHexagon on the right)
       Ṛ€   - reverse each
               (placing it at the bottom-right, with rows correctly ordered)
         »/ - reduce by maximum
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 64 bytes

NθW‹Lυθ«→F⁶F⁻ⅈ⁼¹κ⊞υ⊕⁺κ÷κ³»F…υθ«\¶ \×⁴_↗²←↖²←×⁴_↓↙²→M⁴✳⁻⁸ιM⊗﹪ι²✳ι

Try it online! Link is to verbose version of code. Explanation:

Nθ

Input N.

W‹Lυθ«

Until it is large enough...

→F⁶F⁻ⅈ⁼¹κ⊞υ⊕⁺κ÷κ³

... calculate the path to trace out an ever increasingly large hexagon.

»F…υθ«

Taking only the first N steps:

\¶ \×⁴_↗²←↖²←×⁴_↓↙²→

Draw a hexagon without disturbing any existing hexagons, leaving the cursor in its original position.

M⁴✳⁻⁸ιM⊗﹪ι²✳ι

Take the next step on the path.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.