14
\$\begingroup\$

See also: Wordagons

Challenge

Given a hexagon and a number n as input, output the same hexagon rotated n times.

Hexagon

The hexagon will be a string like so:

  a b c
 d d e e
f f o g g
 h h i i
  j k l

The hexagon will always be regular and will only ever contain the printable ASCII characters:

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Rotation

To find how much to rotate the hexagon by, multiply n by 60 degrees to get the angle of rotation. For example, using the above hexagon, if n is 2, we multiply that by 60 to get 120 degrees. Then, we rotate the hexagon by 120 degrees:

  j h f
 k h f d
l i o d a
 i g e b
  g e c

Note that each side has moved two sides clockwise.

If n is positive, you should rotate the hexagon clockwise. If n is negative, you should rotate the hexagon anticlockwise.

n will always be an integer in the range -6 to 6 inclusive.

Examples

Input:

  . u .
 .  |  .
l - + - r
 .  |  .
  . d .

n = 3

  . d .
 .  |  .
r - + - l
 .  |  .
  . u .

Input:

  - - -
 /     \
<       >
 \     /
  - - -

n = -1

Output:

  - \ >
 -     /
-       -
 /     -
  < \ -

Input:

   h e x a
  g o n s a
 n d t r i a
n g l e s m a
 k e m e s a
  d l y m a
   d d e r

n = 6

Output:

   h e x a
  g o n s a
 n d t r i a
n g l e s m a
 k e m e s a
  d l y m a
   d d e r

Winning

The shortest program in bytes wins.

\$\endgroup\$
  • 2
    \$\begingroup\$ What do you have against triangles? :P \$\endgroup\$ – Conor O'Brien Sep 6 '16 at 23:08
  • 4
    \$\begingroup\$ @ConorO'Brien Haven't you seen how dangerous their points are? ;) \$\endgroup\$ – Beta Decay Sep 6 '16 at 23:11
  • \$\begingroup\$ Can we take input as a list of lines? Can we assume input is space-padded? \$\endgroup\$ – Lynn Sep 6 '16 at 23:36
  • 1
    \$\begingroup\$ Very closely related: codegolf.stackexchange.com/q/51964/15599 \$\endgroup\$ – Level River St Sep 6 '16 at 23:43
  • 1
    \$\begingroup\$ I lost a few minutes trying to be a smartass with CSS' transform:rotate(x). Turns out that wasn't a bright idea, who would have known? \$\endgroup\$ – Aaron Sep 7 '16 at 14:28
1
\$\begingroup\$

Perl, 120 119 113 107 104 102 bytes

Includes +6 for -0pXi

Run with the hexagon on STDIN (padded with spaces so all lines have the same length) and the number of rotations after -i.

perl -0pXi1 rotagon.pl 
  a b c
 d d e e
f f o g g
 h h i i
  j k l
^D

rotagon.pl

s!(.)!$X=(3*($w=y/
//*2)/4+($-="@-"/$w)-"@+"%$w)/2*--$w+2*$--.25;/^\C{$X}(.)/;$1!eg while$^I--%6

Finally got the rotation matrix to be shorter than the old double loop

\$\endgroup\$
  • \$\begingroup\$ I had to add the a and n flags to get this to work. \$\endgroup\$ – Neil Sep 8 '16 at 19:02
  • 1
    \$\begingroup\$ @Neil That is only needed on old perl versions. In recent perls -F implies -a and -a in turn implies -n \$\endgroup\$ – Ton Hospel Sep 8 '16 at 20:29
14
\$\begingroup\$

Python 2, 160 158 156 148 124 bytes

Prepare to be amazed! The resulting text is drawn on a canvas... by a Turtle! :D

from turtle import*
h,n=input()
up()
rt(n*60)
l=0
for c in h:
 write(c);fd(9);l-=1
 if" ">c:fd(9*l);rt(90);fd(15);lt(90);l=0

Try it online - Supports decimal rotations as well!


If you would prefer the turtle to be shaped like one, you can add the line t.shape("turtle").

View the revision history to see past versions.

Aliases used:

up() - penup()

rt() - right()

lt() - left()

fd() - forward()

bk() - backward()

Thanks to Sherlock9 for the suggestions!

\$\endgroup\$
  • 2
    \$\begingroup\$ 7 more until you beat standard python! \$\endgroup\$ – Rɪᴋᴇʀ Sep 8 '16 at 20:40
  • \$\begingroup\$ @EasterlyIrk If only t.write('\n') worked... \$\endgroup\$ – mbomb007 Sep 9 '16 at 14:42
  • \$\begingroup\$ @EasterlyIrk If I could remove t.up(), I'd be there. \$\endgroup\$ – mbomb007 Sep 9 '16 at 15:15
  • \$\begingroup\$ Congrats! You are beating normal python! \$\endgroup\$ – Rɪᴋᴇʀ Sep 9 '16 at 22:17
6
\$\begingroup\$

Python 2.7, 151 bytes

h,n=input()
s=1-len(h)
e=enumerate
exec n%6*"h=[''.join(c>' 'and h[y-x-3*s/2>>1][x*2+y*6+s>>2]or c for x,c in e(l))for y,l in e(h)];"
print'\n'.join(h)

Example:

% python2.7 hexarot.py <<<'[["   h e x a   ","  g o n s a  "," n d t r i a ","n g l e s m a", " k e m e s a ","  d l y m a  ","   d d e r   "],-1]'
   a a a a   
  x s i m a  
 e n r s s a 
h o t e e m r
 g d l m y e 
  n g e l d  
   n k d d
\$\endgroup\$
  • \$\begingroup\$ @mbomb007 That won't work because of the order of operations, the subtractions will happen before the bitshift. \$\endgroup\$ – FryAmTheEggman Sep 8 '16 at 20:39
1
\$\begingroup\$

JavaScript (ES6), 130 127 bytes

f=(s,n)=>n%6?f(s.split`
`.map((s,i,a)=>s.replace(/./g,(c,j)=>(a[(i+z-j)/2+z]||c)[(i+j-z)/2+i]||c,z=a.length>>1)).join`
`,n-1):s

Originally based on my answer to Rotate a diamond tiling although I might now be able to rewrite that answer along the lines of this answer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.