# Hexagonify™ a String Block

Sandbox

### What is Hexagonification™?

Hexagonification™ is a transformation that creates a hexagon with 3 copies of a rectangle block, each skewed to the right by 30 degrees and then rotated by 0, 120 and 240 degrees respectively, as shown in the following image. A triangle hole may appear in the middle, but that isn't a great deal.

### Challenge

Write a program or function that receives a block of string as an input and outputs the hexagonified™ input. You may assume that the input consists of only ASCII characters and new lines, and each line of the input has been padded so that all lines are of equal length. Empty spaces should be filled with space characters.

The input and output formats are flexible. You may receive a single string, a list of lines or a 2D array as input, and output a single string or a list of lines. You may have leading/trailing spaces on each line and leading/trailing newlines, provided that the hexagon is properly formed. See Sample IO for how the blocks should be positioned.

### Sample IO

Input:

3x3
squ
are


Output:

   3 x 3
e s q u
u r a r e
3 q a a s 3
x s r q x
3 e u 3


Input:

longer.....
rectangular
block......


Output:

   l o n g e r . . . . .
. r e c t a n g u l a r
r . b l o c k . . . . . .
. a .                 b r l
. l .               l e o
. u .             o c n
. g .           c t g
. n k         k a e
r a c       . n r
e t o     . g .
g c l   . u .
n e b . l .
o r . a .
l . r .


Input:

vb
el
ro
tc
ik
c2
ax
l8


Output:

        v b
8 e l
x l r o
2 a   t c
k c     i k
c i       c 2
o t         a x
l r           l 8
b e l a c i t r e v
v 8 x 2 k c o l b


Input:

1.line


Output:

 1 . l i n e
e           1
n         .
i       l
l     i
.   n
1 e


Input:

1
.
l
i
n
e


Output:

      1
e .
n   l
i     i
l       n
.         e
1 e n i l . 1


### Scoring

Your score will be the area of the hexagonified™ source code (including the triangle hole). Assuming your code can just fit inside a rectangle of width $$\w\$$ and height $$\h\$$, the score will be:

$$3wh+\frac{(w-h)(w-h+1)}{2}$$

### Winning Criteria

The submission with the lowest score in each language wins. No standard loopholes.

• Isn't there a language, HexaGolf with a built-in for this? – 640KB Dec 15 '19 at 23:57
• @640KB I'm not sure about that, but if there is such language, feel free to use it ;) – Shieru Asakoto Dec 16 '19 at 0:18
• ...waits with baited breath for a Hexagony answer. – Jonathan Allan Dec 16 '19 at 12:06
• May we accept a list of lines? May we return a list of lines? – Jonathan Allan Dec 16 '19 at 21:12
• @JonathanAllan Yes of course. Edited to reflect this. – Shieru Asakoto Dec 16 '19 at 22:11

# Charcoal, score 235 168

→ＷＳ⊞υιＦυＦ
«Ｐ↙ιＭ²←»Ｆ
Ｍ⊖⌈ＥυＬκ↙Ｆ
Ｆυ«Ｐ↖κ↗»Ｆ
Ｍ⊖⌈ＥυＬκ↖Ｆ
Ｆυ«Ｐ⪫κ ↘»


Try it online! Takes input as a newline-terminated list. Charcoal's problem here is that a newline is a command, so I can't just throw newlines in arbitrarily. Instead, I'm passing them as arguments to the For command, which loops over each character in the string, which here is just the one character, therefore not modifying the behaviour of the program (except to change the implicit loop variables). As such, these dummy For instructions are not included in the explanation. Explanation:

→


Another no-op, just to make the code exactly rectangular.

ＷＳ⊞υι


Input the list.

Ｆυ«Ｐ↙ιＭ²←»


Print the lower right diagonal side.

Ｍ⊖⌈ＥυＬκ↙


Move to the bottom.

Ｆυ«Ｐ↖κ↗»


Print the lower left diagonal side.

Ｍ⊖⌈ＥυＬκ↖


Move to the top left.

Ｆυ«Ｐ⪫κ ↘»


Print the top side.

# Python 2, score 918

m=input()
d={};h=len(m)
w=len(m[0])
r=range
for i in r(h*w):
j,i=divmod(i,h)
d[(i,i+j*2)]=d[
(h+j,h+2*w-2*i-j
-2)]=d[(w-j+h+~i
,w+i-j+~h)]=m[i
][j]
y,x=zip(
*d.keys())
for i in r(min
(y),max(y)+1):
print''.join(d
.get((i,j),' ')
for j in r(min
(x),max(x)+1))


Try it online!

# Jelly, Score (8 by  9  8) =  216  192

¹¹ẈṀ_$⁶ẋ ŒdṙLC$UŻ
ÇżUṚżZṚ$ÇṚUF€ḟ€0 0ẋṂ;r/_Ṃ Z,$ẈÇṖ⁶ẋ
4ĿŒHj¥"Ç
ỴÇµÑżK€Y


A full-program which prints the result.

Try it online! Or see the test-suite.

### How?

Starts at the Main (unnumbered) Link, which is the bottom line of code.

¹¹ẈṀ_$⁶ẋ - Link 1: the output lines without leading spaces, A ¹ - no-op -> A ¹ - no-op -> A Ẉ - length of each (A)$   - last two links as a monad - f(Ẉ(A))
Ṁ     -   maximum
_    -   subtract (vectorises)
⁶  - literal space character
ẋ - repeat (vectorises)

ŒdṙLC$UŻ - Link 2: lines Œd - anti-diagonals (e.g. ["abcd","1234"] -> ["d3","c2","b1","a4"])$   - last two links as a monad - f(lines)
L     -   length
C    -   complement (1 - that)
ṙ      - rotate (anti-diagonals) left by (1 - length(lines))
i.e. rotate right by (#lines - 1)
U  - reverse each
Ż - prepend a zero (allows us to start the join one further
down - removed in Link 4 with ḟ€0)

ÇżUṚżZṚ$- Link 3: lines Ç - call the last Link (2) as a monad = Link_2(lines) U - reverse each (lines) ż - zip together$ - last two links as a monad - f(lines)
Z   -   transpose (lines)
Ṛ  -   reverse
ż    - zip together

Ṛ       - reverse
U      - reverse each
F€    - flatten each
ḟ€0 - remove zeros from each

0ẋṂ;r/_Ṃ - Link 5: [w,h]       e.g. [8,3]
0        - literal zero             0
Ṃ      - minimum (of w and h)     3
ẋ       - repeat                   [0,0,0]
/   - reduce ([w,h]) by:
r    -   inclusive range        [8,7,6,5,4,3]
;     - concatenate              [0,0,0,8,7,6,5,4,3]
Ṃ - minimum (of w and h)     3
_  - subtract                 [-3,-3,-3,5,4,3,2,1,0]

Z,$ẈÇṖ⁶ẋ - Link 6: lines$      - last two links as a monad:
Z        -   transpose
,       -   pair
Ẉ     - length of each - i.e. [w=width(S), h=height(S)]
Ç    - call the last Link (5) as a monad = f([w,h])
Ṗ   - pop (remove rightmost)
⁶  - literal space character
ẋ - repeat (vectorises) - Note: z ẋ -n = []

"  - zip with - i.e. for i in [1..] f(left[i],right[i]):
ŒH     -     split (Link_4(lines)[i]) into two halves

ỴÇµÑżK€Y - Main Link: List of characters, S
Ỵ        - split (S) at newline characters
K€  - join each (line) with space characters
ż    - zip these together
Y - join with newline characters
- implicit print

• Nice. And unlike mine, almost no wasted space! – Nick Kennedy Dec 16 '19 at 23:25
• Was quite an effort. I imagine it's still beatable (in Jelly) too! – Jonathan Allan Dec 16 '19 at 23:27
• With some challenges, it seems everyone in a given language tends to convene on a single similar solution. I quite like the fact that ours are pretty different here. – Nick Kennedy Dec 16 '19 at 23:28

# Jelly, width × height = 9 × 9 9 × 6, score = 243 168

ṚZÑṚŻL}¡
J’0ẋ;"K€
ṚÇḟ€⁶ZUÇŻ
Z,ṖẈ+L0ẋ
Ṫ;Ɱ";⁶o/
U¹ƭṛĿ¥Ɱ4Ç


Try it online!

A set of links that takes a list of Jelly strings as its input and returns a list of Jelly strings.

# An alternative 8 × 7 with score of 169:

ṚZÑṚŻL}¡
J’0ẋ;"K€
ṚÇḟ€⁶ZUÇ
Z,ṖẈ+L0ẋ
Ṫ;Ɱ";⁶o/
ṛĿ"JŻ€3¦
U3Ð¡ṚÇ5Ŀ


Try it online!

• +1 (but I've also enquired regarding lines as IO) – Jonathan Allan Dec 16 '19 at 21:38
• @JonathanAllan modified taking advantage of laxer IO rules. – Nick Kennedy Dec 17 '19 at 21:05