ASCII Hexagon Chain

13

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.

LiefdeWen

Posted 2017-12-08T09:15:18.250

Reputation: 3 381

Related – Arnauld – 2017-12-08T09:23:27.237

2Waiting for obligatory Hexagony answer... – LLlAMnYP – 2017-12-08T15:36:59.633

2

@LLlAMnYP Any minute now...

– Martin Ender – 2017-12-08T15:43:27.023

@user202729 If you write one now, we never have to find out what happens. – LiefdeWen – 2017-12-27T17:00:19.760

First line in Hexagony. Obviously need to increase edge size for the rest. – user202729 – 2017-12-28T14:14:04.757

Answers

8

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.

Neil

Posted 2017-12-08T09:15:18.250

Reputation: 95 035

4

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!

TFeld

Posted 2017-12-08T09:15:18.250

Reputation: 19 246

@ovs Thanks a lot :) – TFeld – 2017-12-08T14:39:49.370

222 bytes – FlipTack – 2017-12-25T10:08:52.560

3

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

dzaima

Posted 2017-12-08T09:15:18.250

Reputation: 19 048

3

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!

James Holderness

Posted 2017-12-08T09:15:18.250

Reputation: 8 298

1

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>

Arnauld

Posted 2017-12-08T09:15:18.250

Reputation: 111 334

0

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

dzaima

Posted 2017-12-08T09:15:18.250

Reputation: 19 048