12 replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/

Notice: This answer has been solidly beaten with a side-length 4 solutionwith a side-length 4 solution by Etoplay.

The first ever non-trivial (i.e. non-linear) Hexagony program! It is based on the same squared-factorial approach as Sp3000's Labyrinth answerSp3000's Labyrinth answer. After starting out with a hexagon of size 10, I managed to compress it down to size 5. However, I was able to reuse some duplicate code and there are still quite a bunch of no-ops in the code, so size 4 might just be possible.

Notice: This answer has been solidly beaten with a side-length 4 solution by Etoplay.

The first ever non-trivial (i.e. non-linear) Hexagony program! It is based on the same squared-factorial approach as Sp3000's Labyrinth answer. After starting out with a hexagon of size 10, I managed to compress it down to size 5. However, I was able to reuse some duplicate code and there are still quite a bunch of no-ops in the code, so size 4 might just be possible.

Notice: This answer has been solidly beaten with a side-length 4 solution by Etoplay.

The first ever non-trivial (i.e. non-linear) Hexagony program! It is based on the same squared-factorial approach as Sp3000's Labyrinth answer. After starting out with a hexagon of size 10, I managed to compress it down to size 5. However, I was able to reuse some duplicate code and there are still quite a bunch of no-ops in the code, so size 4 might just be possible.

11 deleted 289 characters in body

Notice: I'll be giving a bounty of 500 rep to the first person who finds a valid solution in a hexagon of side-length 4 (or less?) or a provably optimal solution of side-length 5. If you can't find such a solution but manage to beat my score in a side-length 5 hexagon (by getting more no-ops at the end of the program, which can be omitted from the source code), I'm willing to give out a smaller bounty for that as well.

Notice: This answer has been solidly beaten with a side-length 4 solution by Etoplay.

Notice: I'll be giving a bounty of 500 rep to the first person who finds a valid solution in a hexagon of side-length 4 (or less?) or a provably optimal solution of side-length 5. If you can't find such a solution but manage to beat my score in a side-length 5 hexagon (by getting more no-ops at the end of the program, which can be omitted from the source code), I'm willing to give out a smaller bounty for that as well.

Notice: This answer has been solidly beaten with a side-length 4 solution by Etoplay.

10 added 43 characters in body

Hexagony, 218925858 55 bytes

)}?}.=(..]}]=}={='.=}.&~}~./=}%*..=&.=&{.<......*|>=|>(<..=%....}!\[=.&@..&@\[

     ) } ? } .
= ( . . ] }=
} = {' . =} . &}
~ . / =% }* . . =&
. = & { . < . . .
. . . *= | > ( <
. . =} %! .= . .
. }& !@ \ [ .
&. @. . . .

             . . . . . . . . . . . . .
. . . . . . . . . . @. . . .
. . . . . . . . . . !. . . . .
. . . . . . . . . . %@ . . . . .
. . . . . . . . . . =! . . . . . .
. . . . . . . . . . {% . . . . . . .
. . . . . . . . . . =' . . . . . . . .
. . . . . . . . . . & . . . . . . . . .
. . . . . . . . . . { . . . . . . . . . .
. . . . . . . . . . * . . . . . . . . . . .
. . . . . . . . . . = . . . . . . . . . . . .
. . . . . . . . . . } . . . . . . . . . . . . .
) } ? } = & { < . . & . . . . . . . . . . . . . .
. . . . . . . > ( < . . . . . . . . . . . . . .
. . . . . . = . . } . . . . . . . . . . . . .
. . . . . } . . . = . . . . . . . . . . . .
. . . . | . . . . | . . . . . . . . . . .
. . . . * . . . ) . . . . . . . . . . .
. . . . = . . & . . . . . . . . . . .
. . . . > } < . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . .

&}=*{&={=%&'%!@


Since C is zero, & copies the left neighbour, i.e. the factorial in A. }=* moves to B and stores the product of the two copies of the factorial (i.e. the square) in B. { moves back to C, but doesn't reverse the MP. We know that the current value is now positive, so & copies input from D into C. ={=' reverses the MP, moves backwards to the right, i.e. onto A and reverses the MP again. Remember, the square of the factorial is in B and the input is in C. So % computes (n-1)!^2 % n, exactly what we're looking for. ! prints the result as an integer (0 or 1) and @ terminates the program.

        ) . . . .
= . . . ] .
} = . . =} . .
~ . / . }* . . .
. . . . . . . . .
. . . *= . > ( <
. . =} . .= . .
. }& . \ [ .
&. . . . .


TheThis one now executes ~}=)&}=*}= which undoes the negation and then just runs the ungolfed loop body (minus the =). Finally it hits ] which hands control back to the original IP. (Note that next time, we execute it this IP, it will start from where it left off, so it will first hit the corner. We need the current value to be negative in order for the IP to jump back to the north west edge instead of the south east one.)

Once the original IP resumes control, it bounces off the \ and, executes the remaining = and then hits > to feed into the next loop iteration.

        ) . . . .
. ( . . ] }=
. . {' . =} . &}
. . . =% }* . . =&
. . . . . . . . .
. . . *= | . . <
. . =} %! . . .
. }& !@ . . .
&. @. . . .


The IP moves north east form the < and wraps around to the north east diagonal. So it ends up on the same execution path as the loop body (&}=*}=]]). Which is actually pretty cool, because that is exactly the code we want to execute at this point, at least if we add another =} (because }=} is equivalent to {). But how does this not actually enter the earlier loop again? Because ] changes to the next IP which is now the (so far unused) IP which starts in the top right corner, moving south west. From there, the IP continues along the edge, wraps to the top left corner, moves down the diagonal, bounces off the | and terminates at @ while executing the final bit of linear code:

=}&=&)({=%'%!@


Hexagony, 2189258 bytes

)}?}.=(..]}}={.=.&~./=}..=.=&{.<......*|>(<..=%....}!\[.&@

     ) } ? } .
= ( . . ] }
} = { . = . &
~ . / = } . . =
. = & { . < . . .
. . . * | > ( <
. . = % . . .
. } ! \ [ .
& @ . . .

             . . . . . . . . . . . . .
. . . . . . . . . . @ . . .
. . . . . . . . . . ! . . . .
. . . . . . . . . . % . . . . .
. . . . . . . . . . = . . . . . .
. . . . . . . . . . { . . . . . . .
. . . . . . . . . . = . . . . . . . .
. . . . . . . . . . & . . . . . . . . .
. . . . . . . . . . { . . . . . . . . . .
. . . . . . . . . . * . . . . . . . . . . .
. . . . . . . . . . = . . . . . . . . . . . .
. . . . . . . . . . } . . . . . . . . . . . . .
) } ? } = & { < . . & . . . . . . . . . . . . . .
. . . . . . . > ( < . . . . . . . . . . . . . .
. . . . . . = . . } . . . . . . . . . . . . .
. . . . . } . . . = . . . . . . . . . . . .
. . . . | . . . . | . . . . . . . . . . .
. . . . * . . . ) . . . . . . . . . . .
. . . . = . . & . . . . . . . . . . .
. . . . > } < . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . .

&}=*{&={=%!@


Since C is zero, & copies the left neighbour, i.e. the factorial in A. }=* moves to B and stores the product of the two copies of the factorial (i.e. the square) in B. { moves back to C, but doesn't reverse the MP. We know that the current value is now positive, so & copies input from D into C. ={= reverses the MP, moves to A and reverses the MP again. Remember, the square of the factorial is in B and the input is in C. So % computes (n-1)!^2 % n, exactly what we're looking for. ! prints the result as an integer (0 or 1) and @ terminates the program.

        ) . . . .
= . . . ] .
} = . . = . .
~ . / . } . . .
. . . . . . . . .
. . . * . > ( <
. . = . . . .
. } . \ [ .
& . . . .


The one now executes ~}=)&}=*}= which undoes the negation and then just runs the ungolfed loop body. Finally it hits ] which hands control back to the original IP. (Note that next time, we execute it this IP, it will start from where it left off, so it will first hit the corner. We need the current value to be negative in order for the IP to jump back to the north west edge instead of the south east one.)

Once the original IP resumes control, it bounces off the \ and the > to feed into the next loop iteration.

        ) . . . .
. ( . . ] }
. . { . = . &
. . . = } . . =
. . . . . . . . .
. . . * | . . <
. . = % . . .
. } ! . . .
& @ . . .


The IP moves north east form the < and wraps around to the north east diagonal. So it ends up on the same execution path as the loop body (&}=*}=]). Which is actually pretty cool, because that is exactly the code we want to execute at this point, at least if we add another } (because }=} is equivalent to {). But how does this not actually enter the earlier loop again? Because ] changes to the next IP which is now the (so far unused) IP which starts in the top right corner, moving south west. From there, the IP continues along the edge, wraps to the top left corner, moves down the diagonal, bounces off the | and terminates at @ while executing the final bit of linear code:

}&=)({=%!@


Hexagony, 2189258 55 bytes

)}?}.=(..]=}='.}.}~./%*..&.=&{.<......=|>(<..}!=...&@\[

     ) } ? } .
= ( . . ] =
} = ' . } . }
~ . / % * . . &
. = & { . < . . .
. . . = | > ( <
. . } ! = . .
. & @ \ [ .
. . . . .

             . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . @ . . . . .
. . . . . . . . . . ! . . . . . .
. . . . . . . . . . % . . . . . . .
. . . . . . . . . . ' . . . . . . . .
. . . . . . . . . . & . . . . . . . . .
. . . . . . . . . . { . . . . . . . . . .
. . . . . . . . . . * . . . . . . . . . . .
. . . . . . . . . . = . . . . . . . . . . . .
. . . . . . . . . . } . . . . . . . . . . . . .
) } ? } = & { < . . & . . . . . . . . . . . . . .
. . . . . . . > ( < . . . . . . . . . . . . . .
. . . . . . = . . } . . . . . . . . . . . . .
. . . . . } . . . = . . . . . . . . . . . .
. . . . | . . . . | . . . . . . . . . . .
. . . . * . . . ) . . . . . . . . . . .
. . . . = . . & . . . . . . . . . . .
. . . . > } < . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . .

&}=*{&'%!@


Since C is zero, & copies the left neighbour, i.e. the factorial in A. }=* moves to B and stores the product of the two copies of the factorial (i.e. the square) in B. { moves back to C, but doesn't reverse the MP. We know that the current value is now positive, so & copies input from D into C. ' the MP backwards to the right, i.e. onto A. Remember, the square of the factorial is in B and the input is in C. So % computes (n-1)!^2 % n, exactly what we're looking for. ! prints the result as an integer (0 or 1) and @ terminates the program.

        ) . . . .
= . . . ] .
} = . . } . .
~ . / . * . . .
. . . . . . . . .
. . . = . > ( <
. . } . = . .
. & . \ [ .
. . . . .


This one now executes ~}=)&}=*} which undoes the negation and then just runs the ungolfed loop body (minus the =). Finally it hits ] which hands control back to the original IP. (Note that next time, we execute it this IP, it will start from where it left off, so it will first hit the corner. We need the current value to be negative in order for the IP to jump back to the north west edge instead of the south east one.)

Once the original IP resumes control, it bounces off the \, executes the remaining = and then hits > to feed into the next loop iteration.

        ) . . . .
. ( . . ] =
. . ' . } . }
. . . % * . . &
. . . . . . . . .
. . . = | . . <
. . } ! . . .
. & @ . . .
. . . . .


The IP moves north east form the < and wraps around to the north east diagonal. So it ends up on the same execution path as the loop body (&}=*}]). Which is actually pretty cool, because that is exactly the code we want to execute at this point, at least if we add another =} (because }=} is equivalent to {). But how does this not actually enter the earlier loop again? Because ] changes to the next IP which is now the (so far unused) IP which starts in the top right corner, moving south west. From there, the IP continues along the edge, wraps to the top left corner, moves down the diagonal, bounces off the | and terminates at @ while executing the final bit of linear code:

=}&)('%!@

9 added 48 characters in body
8 added 384 characters in body
7 edited body
6 deleted 1 character in body
5 added 5229 characters in body
4 added 5229 characters in body
3 deleted 178 characters in body
2 deleted 468 characters in body
1