17
\$\begingroup\$

There are many magic squares, but there is just one non-trivial magic hexagon, as Dr. James Grime explained, which is the following:

  18 17  3
 11  1  7 19
9  6  5  2 16
 14  8  4 12
  15 13 10

As it is done in Hexagony this is easiest written as just one line, by just reading it row by row:

18 17 3 11 1 7 19 9 6 5 2 16 14 8 4 12 15 13 10

Of course there are twelve such list representations of this magic hexagon in total, if you count rotations and reflections. For instance a clockwise 1/6 rotation of the above hexagon would result in

9 11 18 14 6 1 17 15 8 5 7 3 13 4 2 19 10 12 16

@Okx asked to list the remaining variants. The remaining lists are:

15 14 9 13 8 6 11 10 4 5 1 18 12 2 7 17 16 19 3
3 17 18 19 7 1 11 16 2 5 6 9 12 4 8 14 10 13 15
18 11 9 17 1 6 14 3 7 5 8 15 19 2 4 13 16 12 10
9 14 15 11 6 8 13 18 1 5 4 10 17 7 2 12 3 19 16

plus all the mentioned lists reversed.

Challenge

Write a program that outputs the magic hexagon as a list. You can choose any of the 12 reflections/rotations of the hexagon.

Please add a few words on how your solution works.

\$\endgroup\$
  • 2
    \$\begingroup\$ Can this be done in hexagony? If so, I will put a bounty to reward that answer. \$\endgroup\$ – Mr. Xcoder Jun 4 '17 at 18:49
  • 1
    \$\begingroup\$ @Mr.Xcoder Anything can be done in Hexagony. It probably just won't be very interesting, because I doubt that you'll be able to save bytes over just printing one of the lists literally. \$\endgroup\$ – Martin Ender Jun 28 '17 at 16:25

10 Answers 10

9
\$\begingroup\$

Octave, 24 bytes

'IKRNFAQOHEGCMDBSJLP'-64

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Jelly, 11 bytes

“JɼQⱮȦ>Ȯ’Œ?

A niladic link returning the list of the given orientation reflected left-right.

Try it online!

How?

Just the kind of thing for which I made Œ?

“JɼQⱮȦ>Ȯ’Œ? - Niladic link: no arguments
“JɼQⱮȦ>Ȯ’   - base 250 number, 18473955480703453
         Œ? - shortest permutation of some set of natural numbers one through to some N
            -   inclusive which would lie at that index in a list of all permutations of
            -   those same natural numbers when sorted lexicographically.
            -
            -   - for example 7Œ?:
            -   - since 7 is greater than 3! and less than 4!+1, it references four items
            -   - the sorted order of permutations of 4 items is:
            -   - [[1,2,3,4],[1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[1,4,3,2],[2,1,3,4], ...]
            -   - so 7Œ? yields [2,1,3,4]
\$\endgroup\$
4
\$\begingroup\$

Pyth, 15 bytes

.PC"A¡öò\x06\x11Ý"S19

(Control characters replaced with \x06 and \x11 for your viewing convenience.)

Try it online

How it works

   "A¡öò\x06\x11Ý"      magic string
  C                     convert to number n using codepoints as base-256 digits
.P                S19   nth lexicographic permutation of [1, …, 19]
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 14 bytes

Both solutions generate the list [3,17,18,19,7,1,11,16,2,5,6,9,12,4,8,14,10,13,15]

19Lœ•δn2мׄÁ•è

Generates a list of all (sorted) permutations of the range [1...19] and indexes into that list with a base 255 compressed base 10 number.

Or 15 bytes runnable online

•áRвºñ*$vn+•20в

Decompresses a base 255 string to a base 10 number and converts to a list of base 20 digits.

Try it online!

\$\endgroup\$
3
\$\begingroup\$

SOGL, 15 bytes

³←@uΙΒQH√y׀“L«─

Explanation:

...“     push the number 4121998669867569415662783
    L«   push 20
      ─  convert 4121998669867569415662783 from base 10 to a base 20 number aka base 10 array 
\$\endgroup\$
3
\$\begingroup\$

Jelly, 21 bytes

18473955480703453œ?19

I really want to compress that big number, but I'm not sure how.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 18473955480703453 is 1 bytes shorter. \$\endgroup\$ – Emigna Jun 4 '17 at 10:40
  • \$\begingroup\$ Correct me if I'm wrong, but wouldn't a list of code page indices be shorter? \$\endgroup\$ – Nick Clifford Jun 4 '17 at 11:26
  • \$\begingroup\$ RE compression: It's the same number I used in mine. All you have to do is convert the number to bijective base 250 using ḃ250 and index into the code page (which is now easier as there is a niladic atom for it, ØJ). \$\endgroup\$ – Jonathan Allan Jun 4 '17 at 11:42
  • \$\begingroup\$ @NickClifford it would be 21 bytes too (19 indexes, an open quote and a close quote). \$\endgroup\$ – Jonathan Allan Jun 4 '17 at 11:48
  • \$\begingroup\$ @JonathanAllan Ah, got it. \$\endgroup\$ – Nick Clifford Jun 4 '17 at 11:49
2
\$\begingroup\$

APL, 24 bytes

⎕A⍳'RQCKAGSIFEBPNHDLOMJ'

Try it online!

How?

⎕A                        ⍝ 'ABC...
   ⍳                       ⍝ indices of
    'RQCKAGSIFEBPNHDLOMJ'  ⍝ ← this vector
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 49 bytes

[...'ih3b17j9652ge84cfda'].map(n=>parseInt(n,26))

console.log(
[...'ih3b17j9652ge84cfda'].map(n=>parseInt(n,26))
)

\$\endgroup\$
0
\$\begingroup\$

Mathematica, 37 bytes

36^^md1o3apsqxqkfhq6~IntegerDigits~20

Explanation (that may be already obvious as Mathematica is not a codegolf language, but according to the OP requirement):

36  : Number base
^^  : Input a number in arbitrary base. See BaseForm documentation
md1o3apsqxqkfhq6 : the number in base 36
~IntegerDigits~20 : convert to base 20 as list of digits

Output:

{18,17,3,11,1,7,19,9,6,5,2,16,14,8,4,12,15,13,10}
\$\endgroup\$
  • 1
    \$\begingroup\$ Note that kolmogorv-complexity actually is about data compression. \$\endgroup\$ – flawr Jun 4 '17 at 10:49
0
\$\begingroup\$

Japt, 27 bytes

Inspired by rahnema1's solution.

`ikrnfaqogcmdbsjlp`¨c -96

Try it online

\$\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.