Make a ASCII Hexagon Ring Tiling
Lua, 174 176 156 bytes
The code would print height-amount of lines, not height-amount of hexagons. Added *4
, that fixed it, but added 2 extra bytes.
Saved some bytes by changing the counter from if to modulo, and by putting two io.read()
s into one.
Uses io.read()
as input.
a,w,h,n,d={[[ /\__/\ \__/]],[[/_/ \_\/__\\]],[[\ \__/ /\__/]],[[_\/__\/_/ \\]]},io.read(,),"",0 for i=h*4,0,-1 do d=d+1 d=5%d end n=a[d]print(n:rep(w))end
Replicates the strings width-amount of times via string:rep(width)
, then iterates height-amount of times with a for-loop. Needed [[]]
(literal strings) because the backslashes really screwed stuff up.
Old Version:
a,w,h,n,d={[[ /\__/\ \__/]],[[/_/ \_\/__\\]],[[\ \__/ /\__/]],[[_\/__\/_/ \\]]},io.read(),io.read(),"",0 for i=h*4,0,-1 do d=d+1 d=5%d end n=a[d]print(n:rep(w))end
Befunge, 137 bytes
I seem to have created some kind of scifi hand gun.
&>4*>~>$&>\>1-:4%3v
>00gg,\1-:v^_@#:\<>+00p\::6*\00g2/+2%+1+66+:
^%+66<:+1\_$$55+,^
_\/__\/_/ \
\ \__/ /\__/\
/_/ \_\/__\/
/\__/\ \__/
Try it online!
Explanation
&>4*> Read the height and multiply by 4.
~>$ Drop the 'x' separator.
&>\ Read the width and swap below the height
> Start of the outer loop.
1- Decrement the height.
:4%3+00p Calculate the pattern y offset: height%4+3
\ Swap the width back to the top.
::6*\00g2/+2%+1+ Calculate the line length: w*6+(w+y/2)%2+1
66+: Initialise the pattern x offset to 12, and duplicate it.
> Start of the inner loop.
00gg Get a pattern char using the x on the stack and the saved y.
, Output the char.
\1- Swap the line length to the top of the stack and decrement it.
:v Check if it's zero, and if so...
_ ...break out of the loop to the right.
+1\ Otherwise swap the x offset back to the top and increment it.
%+66<: Mod it with 12 to make sure it's in range.
^ Then return to the beginning of the inner loop.
$$ Drop the x offset and line length.
55+, Output a newline.
\< Swap the height back to the top of the stack.
_@#: Check if it's zero, and exit if that's the case.
^ Otherwise return to the start of the outer loop.