16
\$\begingroup\$

Problem

Draw a hexagon chain x long, each with side of y length

Input

x - the length of the chain <= 50

y - the length of each side <= 50

Examples

x=1,y=1

 _
/ \
\_/

x=4,y=1

 _   _
/ \_/ \_
\_/ \_/ \
  \_/ \_/

x=3,y=2

  __      __
 /  \    /  \
/    \__/    \
\    /  \    /
 \__/    \__/
    \    /
     \__/

Rules

  • The shortest valid answer in bytes wins.

  • Leading and trailing newlines allowed.

  • Trailing white space allowed.

\$\endgroup\$
5
  • \$\begingroup\$ Related \$\endgroup\$
    – Arnauld
    Commented Dec 8, 2017 at 9:23
  • 2
    \$\begingroup\$ Waiting for obligatory Hexagony answer... \$\endgroup\$
    – LLlAMnYP
    Commented Dec 8, 2017 at 15:36
  • 2
    \$\begingroup\$ @LLlAMnYP Any minute now... \$\endgroup\$ Commented Dec 8, 2017 at 15:43
  • \$\begingroup\$ @user202729 If you write one now, we never have to find out what happens. \$\endgroup\$
    – LiefdeWen
    Commented Dec 27, 2017 at 17:00
  • \$\begingroup\$ First line in Hexagony. Obviously need to increase edge size for the rest. \$\endgroup\$
    – user202729
    Commented Dec 28, 2017 at 14:14

6 Answers 6

8
\$\begingroup\$

Charcoal, 34 bytes

NθFN«M∧﹪ι²⊗θ↓P×_θ←↖θ→↗θ×_θ↓↘θ←P↙θ↗

Try it online! Link is to verbose version of code. Takes the parameters in the order size, count. Explanation:

Nθ

Input the hexagon size.

FN«

Loop over the input number of hexagons.

M∧﹪ι²⊗θ↓

On alternate hexagons, move down a whole hexagon, so that the next hexagon is drawn to the lower right rather than the upper right.

P×_θ

Draw the bottom.

←↖θ

Draw the lower left side.

→↗θ

Draw the upper left side.

×_θ

Draw the top.

↓↘θ

Draw the upper right side.

←P↙θ

Draw the lower right side.

Assume the next hexagon is to the upper right.

\$\endgroup\$
4
\$\begingroup\$

Python 2, 254 224 bytes

def f(n,w):
 a=w*2
 for j in range(1+w*3):print''.join([[[' ',[' /'[i%w==-j%w],' \\'[i%w==~-j%w]][i/a+~-j/w&1]][(j>0)*(i/w>=(j>a))*((i/w/n<2)or(n%-2<~-j/w<3-n%2))],' _'[(j+i/w%4/2*w)%a<(i<n*a)]][i/w%2]for i in range(-~n*a)])

Try it online!


Python 2, 264 229 bytes

def f(n,w):
 c=2*w;r=[[' ']*(-~n*c)for _ in' '*(1+w*3)]
 for i in range(w):
  for j in range(n):a=i+j*2*w;b=j%2*w;r[b][w+a],r[c+b][w+i+j*c],r[b+w-i][a],r[b+c-i][a+c],r[b+w-~i][a],r[b-~i][a+c]=r'__//\\'
 for l in r:print''.join(l)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ @ovs Thanks a lot :) \$\endgroup\$
    – TFeld
    Commented Dec 8, 2017 at 14:39
  • \$\begingroup\$ 222 bytes \$\endgroup\$
    – FlipTack
    Commented Dec 25, 2017 at 10:08
3
\$\begingroup\$

SOGL V0.12, 32 31 bytes

ā.{e╚øΚe╔*ο⁴↔±┼┼╬±fe«*If2%e*I╬5

Try it Here!

Explanation:

ā                              push an empty array - the canvas
 .{                            repeat input times
   e╚                            push a diagonal the length of the variable E (by default: next input)
     øΚ                          prepend a line to it
       e╔*ο                      push ["_"*E]
           ⁴                     copy that diagonal
            ↔±                   and reverse it horizontally
              ┼┼                 add the 3 parts together
                ╬±               and palindromize vertically - one hexagon is finished
                  fe«*I          push counter*E*2 + 1 (the counter is 0-based)
                       f2%e*I    push counter%2 * E + 1
                             ╬5  at [counter*E*2+1; counter%2*E+1] insert the hexagon in the canvas
\$\endgroup\$
3
+50
\$\begingroup\$

Befunge, 230 228 225 bytes

+&#92#<*:0< vp93*p92+1*3:p91:&+1p
>1+:29g-!#@_>:1-19g+:19g/1-:2*49p2%!2*:1+59p19g*\19g%+69p01v
,>*19g3*79g-1-69g-!2*49g1+0g2%*79g69g-!49g0g2%*++4g,1+:39g-v
^^3*!-*g95g91+1g96!-g95%4p04p01:`\g90p05`0::/g91:p97%*4g91:_$55+
 \/_

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 215 bytes

Takes input in currying syntax (y)(x).

y=>(F=(x,c=!(p=y-1,w=x*y*2+y,a=[...(' '.repeat(w++)+`
`).repeat(3*y+1)],g=(d,k)=>k!=y?g(d,-~k,a[p+=d]='_\\/'[c%3]):c++))=>x--?F(x,g(1),g(w+1),p++,g(w-1),g(-1),p+=w,g(~w),p--,g(1-w),p+=(c/6&1?w:-w)*y-w+y*2):a.join``)

Demo

let f =

y=>(F=(x,c=!(p=y-1,w=x*y*2+y,a=[...(' '.repeat(w++)+`
`).repeat(3*y+1)],g=(d,k)=>k!=y?g(d,-~k,a[p+=d]='_\\/'[c%3]):c++))=>x--?F(x,g(1),g(w+1),p++,g(w-1),g(-1),p+=w,g(~w),p--,g(1-w),p+=(c/6&1?w:-w)*y-w+y*2):a.join``)

O.innerText = f(3)(7)
<pre id=O></pre>

\$\endgroup\$
1
\$\begingroup\$

Canvas, 25 bytes

ø╶{╷⁷«×¹2%⁷×╵⁷⇵_× ⁷/∔×╬│╋

Try it here!

Explanation (some characters have been changed to look monospace):

ø╶{╷⁷«×¹2%⁷×╵⁷⇵_× ⁷/∔×╬│╋
ø                          push an empty canvas
 ╶{                        for 1..input
   ╷                         decrease (so this starts with 0)
    ⁷«×                      multiply by X*2; X coordinate of new hexagon done
       ¹2%                   push 0-indexed counter%2
          ⁷×                 multiply by X
            ╵                and increment; Y coordinate done
             ⁷⇵              push ceil(X/2), saving the remainder
               _×            repeat "_" that many times
                  ⁷/         push " " and an ASCII diagonal with size X
                    ∔        prepend verticall the space before the diagonal
                              done so there's space for the underscores
                     ×       and append the underscores horizontally to the diagonal
                      ╬│     quad-palindromize with Y overlap of 1
                              and X overlap of the remainder taken before
                        ╋    and at the before defined coords ((I-1)*X*2; (i%2)*X + 1)
                              insert the hexagon in the canvas
\$\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.