Hexagonify™ a String Block

20

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.

enter image description here

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.

Shieru Asakoto

Posted 2019-12-15T03:40:21.277

Reputation: 4 445

Isn't there a language, HexaGolf with a built-in for this? – 640KB – 2019-12-15T23:57:19.517

@640KB I'm not sure about that, but if there is such language, feel free to use it ;) – Shieru Asakoto – 2019-12-16T00:18:05.250

3

...waits with baited breath for a Hexagony answer.

– Jonathan Allan – 2019-12-16T12:06:09.840

1May we accept a list of lines? May we return a list of lines? – Jonathan Allan – 2019-12-16T21:12:25.760

1@JonathanAllan Yes of course. Edited to reflect this. – Shieru Asakoto – 2019-12-16T22:11:24.120

Answers

5

Charcoal, score 235 168

→WS⊞υιFυF
«P↙ιM²←»F
M⊖⌈EυLκ↙F
Fυ«P↖κ↗»F
M⊖⌈EυLκ↖F
Fυ«P⪫κ ↘»

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.

WS⊞υι

Input the list.

Fυ«P↙ιM²←»

Print the lower right diagonal side.

M⊖⌈EυLκ↙

Move to the bottom.

Fυ«P↖κ↗»

Print the lower left diagonal side.

M⊖⌈EυLκ↖

Move to the top left.

Fυ«P⪫κ ↘»

Print the top side.

Neil

Posted 2019-12-15T03:40:21.277

Reputation: 95 035

4

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!

TFeld

Posted 2019-12-15T03:40:21.277

Reputation: 19 246

3

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!

Nick Kennedy

Posted 2019-12-15T03:40:21.277

Reputation: 11 829

1+1 (but I've also enquired regarding lines as IO) – Jonathan Allan – 2019-12-16T21:38:58.583

@JonathanAllan modified taking advantage of laxer IO rules. – Nick Kennedy – 2019-12-17T21:05:56.970

3

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

ÇṚUF€ḟ€0 - Link 4: lines
Ç        - call the last Link (3) as a monad = Link_3(lines)
 Ṛ       - 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 = []

4ĿŒHj¥"Ç - Link 7: lines 
4Ŀ       - call Link 4 as a monad = Link_4(lines)
       Ç - call the last Link (6) as a monad = Link_6(lines)
      "  - zip with - i.e. for i in [1..] f(left[i],right[i]):
     ¥   -   last two links as a dyad:
  ŒH     -     split (Link_4(lines)[i]) into two halves
    j    -     join with (Link_6(lines)[i])

ỴÇµÑżK€Y - Main Link: List of characters, S
Ỵ        - split (S) at newline characters
 Ç       - call the last Link (7) as a monad = Link_7(lines)
  µ      - start a new monadic chain - i.e. f(Link_7(lines))
   Ñ     - call the next Link (1) as a monad = Link_1(Link_7(lines))
     K€  - join each (line) with space characters
    ż    - zip these together
       Y - join with newline characters
         - implicit print

Jonathan Allan

Posted 2019-12-15T03:40:21.277

Reputation: 67 804

Nice. And unlike mine, almost no wasted space! – Nick Kennedy – 2019-12-16T23:25:49.923

Was quite an effort. I imagine it's still beatable (in Jelly) too! – Jonathan Allan – 2019-12-16T23:27:06.793

1With 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 – 2019-12-16T23:28:19.917