38
\$\begingroup\$

A shape is chiral if no amount of rotation can make it look like it's mirror image. In this puzzle we will be writing chiral computer programs.

For this puzzle we will think of a program as a rectangular matrix of characters. As such all solutions to this challenge must be rectangular (that is all lines must be of the same length). We can rotate these programs in quarter turn increments. For example the program

The quickish fish
    lept deftly  
  rightwards     

When rotated a quarter turn clockwise looks like

  T
  h
r e
i  
glq
heu
tpi
wtc
a k
rdi
des
sfh
 t 
 lf
 yi
  s
  h

We can also reflect these programs. Here is the same program reflected over a vertical axis:

hsif hsikciuq ehT
  yltfed tpel    
    sdrawthgir   

A chiral program is a program that when rotated any number of times will always output "left". However when reflected produces a program that outputs "right" no matter how many times it is rotated.

Your task is to write a chiral program in as few bytes as possible.

Additional Rules

  • Output is not-case sensitive but should be consistent. (e.g. You may output "LEFT" and "rIgHt" but this casing must be consistent under rotations)

  • Lines are to be split either on a newline or a newline and a linefeed.

  • Your program must be a rectangle, you can pad it with spaces or comments but each line must have the same length.

  • You may optionally have a trailing newline (or newline and linefeed) on all of your programs if you wish.

\$\endgroup\$
  • \$\begingroup\$ I've been working on this for hours, and I don't think it's possible in Clean if the module header is required. May I interpret "chiral program" as "code, which after being rotated and placed in a source file with the appropriate filename-dependent module header (ie:module filename), forms a full program which produces the correct output"? \$\endgroup\$ – Οurous Sep 13 '18 at 1:26
  • \$\begingroup\$ @Οurous Can't Clean have comments before the header? \$\endgroup\$ – Ørjan Johansen Sep 13 '18 at 1:35
  • \$\begingroup\$ @ØrjanJohansen it can, but I wasn't able to find a way to hide it with comments in a way that worked for all 8 orientations. It can even have any amount of whitespace between module and modulename - if you have any general comment structure suggestions that'd be welcome. \$\endgroup\$ – Οurous Sep 13 '18 at 1:49
  • \$\begingroup\$ Oh dear I think I missed a really obvious solution to that problem. \$\endgroup\$ – Οurous Sep 13 '18 at 1:50

17 Answers 17

16
\$\begingroup\$

Pascal (FPC), 2161 755 349 bytes

///////bw(,,),(wb///////
///////er'''e''re///////
begin//girgtnflig//nigeb
write//itih'dteti//etirw
('le'//ne'' .''en//'ir'(
,'ft'//////////////'hg',
)end.////////////// 't',
,'t' //////////////.dne)
,'gh'//////////////'tf',
('ri'//ne''. ''en//'el'(
write//itetd'hiti//etirw
begin//gilfntgrig//nigeb
///////er''e'''re///////
///////bw(,),,(wb///////

Try left

Try right

@tsh motivated me to try again when I saw his program (in comment) and here it is!


Previous one with 755 bytes:

 begin write('left')end.// 
/e .dne)'thgir'(etirw nigeb
/g                        e
.i                       .g
dn                       di
n                        nn
ew                       e 
)r                       )w
'i                       'r
tt                       ti
fe                       ht
e(                       ge
l'                       i(
'r                       r'
(i                       'l
eg                       (e
th                       ef
it                       tt
r'                       i'
w)                       r)
 e                       we
nn                        n
id                       nd
g.                       i.
e                        g/
begin write('right')end. e/
 //.dne)'tfel'(etirw nigeb 

Try left

Try right

If you saw my previous submission, just forget it :)

All rotations for both left and right programs are the same.

\$\endgroup\$
  • 1
    \$\begingroup\$ 450 bytes \$\endgroup\$ – tsh Sep 13 '18 at 3:23
  • 1
    \$\begingroup\$ I know those mirrored brackets are correct, but they still upset me. \$\endgroup\$ – Sellyme Sep 13 '18 at 5:54
  • 1
    \$\begingroup\$ It looks like a framed mirror! \$\endgroup\$ – Frédéric Grosshans Sep 14 '18 at 7:36
  • \$\begingroup\$ @tsh Excellent idea! You motivated me to find better layout. \$\endgroup\$ – AlexRacer Sep 16 '18 at 18:48
  • \$\begingroup\$ 299 bytes \$\endgroup\$ – Ørjan Johansen Sep 17 '18 at 4:57
11
\$\begingroup\$

Klein (000), 109 87 bytes

."left"@\.
\"right"..
@"thgir">.
..@"tfel"\
\"left"@..
.>"right"@
.."thgir"\
.\@"tfel".

Try it online!

Should be possible to get a shorter version of this answer so good luck!

This answer is inspired by spirals. In particular the idea is two interlocked spirals, one for left and one for right. It has two fold symmetry, So we can check the following programs to know it works:

Quarter turn

...\.@\.
\.>"."""
@""l@trl
"tre"hie
thiftggf
fggtfiht
eih"ert"
lrt@l""@
""".">.\
.\@.\...

Try it online!

Mirrored

.\@.\...
""".">.\
lrt@l""@
eih"ert"
fggtfiht
thiftggf
"tre"hie
@""l@trl
\.>"."""
...\.@\.

Try it online!

Mirrored and Quarter turn

.\@"tfel".
.."thgir"\
.>"right"@
\"left"@..
..@"tfel"\
@"thgir">.
\"right"..
."left"@\.

Try it online!

\$\endgroup\$
10
\$\begingroup\$

Klein (000), 27 25 bytes

\(("right"@/
\(("left"@!\

Try it online! Rotated once! Rotated twice! Rotated three times!

And flipped: Try it online! Rotated once! Rotated twice! Rotated thrice!

The only one that gave me a bit of trouble was the flipped and rotated once, which looks like:

\/
!@
@"
"t
th
fg
ei
lr
""
((
((
\\

This is the only reason for the two sets of ((.

\$\endgroup\$
9
\$\begingroup\$

Klein (211), 37 bytes

!\"left"@"thgir"\!
!/............../!

This has a different program for each rotation.

Explanation

Note that each of these programs is padded to a square with no-ops before execution

No change

!\"left"@"thgir"\!
!/............../!

Try it online!

! makes execution jump over the \ and "left"@ loads the string "left" into the stack and terminates the program printing the stack

Quarter turn

!!...IP->
\/
".
r.
i.
g.
h.
t.
".
@.
".
t.
f.
e.
l.
".
\/
!!
^
|
P
I

Try it online!

Execution runs off the top right corner of the square, continues in the bottom left corner and once again !\"left"@ prints "left.

Half turn

                ^
                |
                P
                I
!/............../!
!\"right"@"tfel"\!
..................
..................
..................
..................
..................
..................
..................
..................
..................
..................
..................
..................
................^.
................|.
IP->............P.
................I.

Try it online!

Here, the execution path exits from the North side, re-enters through the West side, exits again from the East Before entering in the south. \ bounces the path into "left"@ to print it.

Three-quarter turn

!!..IP->
/\
."
.l
.e
.f
.t
."
.@
."
.t
.h
.g
.i
.r
."
/\
!!
^
|
I
P

Try it online

Execution exits from the top right corner then re-enters in the bottom left. Once again the mirrors /\ redirect the path into "left"@ to print it.

Reflection

!\"right"@"tfel"\!
!/............../!

This is essentialy the same as for left in all rotations.

\$\endgroup\$
9
\$\begingroup\$

JavaScript (Node.js), 1481 599 505 461 341 305 271 bytes

/g+///g/g"c.c"g/
g=//////=rolol=g
"left"///inone/+
console//gsgsf//
.log(g)//ho(ot//
console//tlgl"//
"right"//"e)e//g
g=//////////////
//////////////=g
g//e)e"//"thgir"
//"lglt//elosnoc
//to(oh//)g(gol.
//fsgsg//elosnoc
+/enoni///"tfel"
g=lolor=//////=g
/g"c.c"g/g///+g/

Mirrored

/g"c.c"g/g///+g/
g=lolor=//////=g
+/enoni///"tfel"
//fsgsg//elosnoc
//to(oh//)g(gol.
//"lglt//elosnoc
g//e)e"//"thgir"
//////////////=g
g=//////////////
"right"//"e)e//g
console//tlgl"//
.log(g)//ho(ot//
console//gsgsf//
"left"///inone/+
g=//////=rolol=g
/g+///g/g"c.c"g/

Try it online!

(Please see the TIO Link for all test cases, including mirrored version)

Although further golfed, size 22x22 -> 21x21 on credit of @JoKing!

Size 38x38 24x24 22x22 21x21 18x18 17x17 16x16, 4-fold symmetry.

Here comes a question -- is it possible to have 15x15 or smaller for JS? It seems that what I need at least is the comment separator between two sub-blocks and at the edge, so at least 7+2+2+5=16 lines?

\$\endgroup\$
  • \$\begingroup\$ @JoKing Oh that seems practicable. I'd have a try ;) \$\endgroup\$ – Shieru Asakoto Sep 12 '18 at 9:41
  • \$\begingroup\$ @JoKing Got down to 21x21, thanks ;) \$\endgroup\$ – Shieru Asakoto Sep 12 '18 at 10:11
8
\$\begingroup\$

Canvas, 109 89 71 bytes

(tfel⁸((
(h(right
⁸g   q(f
li    re
er    il
f(    g⁸
thgir(h(
((⁸left(

Try it here | Try reversed! Those are the only two possible programs, as the program is rotationally symmetric.

Made in this.

\$\endgroup\$
  • 4
    \$\begingroup\$ What an empty canvas over there... :/ \$\endgroup\$ – Erik the Outgolfer Sep 11 '18 at 17:02
7
\$\begingroup\$

Alice, 25 bytes

}/"regttoo {
{/"lifh""@@{

Left: Normal, 1/4 turn clockwise, 1/2 turn, 1/4 turn counterclockwise

Right: reflect across horizontal axis, down-right diagonal, vertical axis, up-right diagonal

Explanation

This program has three parts. The first part is the three {s and one } on the corners. The { turn the IP left until it reaches the }, at which point it turns right. In the reflected version, the IP goes along the top row in the original program. In the original version, turning right will immediately hit the adjacent {, pointing the IP along the bottom row in the original program.

Next are the two slashes in the second column. If the program is horizontally oriented, either type of slash would simply change to ordinal mode, sending the IP in the same left/right direction it was already going. If the program is vertically oriented, the IP bounces around quite a bit more, but making them both forward slashes gives the same end result. (Two backslashes would give the opposite result, and one of each would create an infinite loop.)

The rest of the program is straightforward. The IP in ordinal mode bounces diagonally, so either "left"o@ or "right"o@ will be run depending on how this section was entered.

\$\endgroup\$
6
\$\begingroup\$

Gol><>, 23 bytes

\"thgir"H /
\"tfel"H.9\

Try it online!

This is the same format as my Klein answer, but 2 bytes shorter. Maybe there's another 2D language out there that can golf those last 2 bytes off... But for now, this is as short as it gets.

\$\endgroup\$
6
\$\begingroup\$

Hexagony, 98 bytes

\.@.t;./r.
.)@.;...;.
ll..)gt.;$
;.i<;;;i);
e;@.)@e;.l
.;e;d.g|.|
rt/;t.;)g\
@f.#l.;..r
\.;\.;i\.i

Try it online! | Rotated 90° | Rotated 180° | Rotated 270°
Reversed | Rotated 90° & Reversed | Rotated 180° & Reversed | Rotated 270° & Reversed

Hexagony was kind of fun for this challenge, because a rotation or a reversal can drastically change the actual program. Each Rotation/Reversal is its own unique program. Some of the program execution paths are much more interesting than others.

I will admit that coming up with this took longer than it probably should have. I am pretty sure a shorter answer can be done in Hexagony, so good luck!

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

Ruby, 131 bytes

#######:$:#
:right#l>r#
$><<###e<i#
:left##f<g#
####   t#h#
#t##   ##t#
#h#t   ####
#g<f##tfel:
#i<e###<<>$
#r>l#thgir:
#:$:#######

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Prolog (SWI), 649 188 bytes

Unflipped, Unrotated

:-%    l :%r     %-:
write(%e%-(i %(etirw
left).%f.weg%.)right
%      t)rth.      %
%      )tiit)      %
%      .htr)t      %
right).%gew.f%.)left
write(% i(-%e%(etirw
:-%     r%: l    %-:

Try it online!

Unflipped, Rotated

:wr%%%lw:
-ri   er-
%ig   fi%
 th   tt 
 et   )e 
 ()   .( 
 %.   %% 
  %.)tfel
right).% 
%(etirw-:
:-write(%
 %.)thgir
left).%  
 %%   .% 
 (.   )( 
 e)   re 
 tl   it 
%ie   gi%
-rf   hr-
:wt%%%tw:

Try it online!

Flipped, Unrotated

:-%     r%: l    %-:
write(% i(-%e%(etirw
right).%gew.f%.)left
%      .htr)t      %
%      )tiit)      %
%      t)rth.      %
left).%f.weg%.)right
write(%e%-(i %(etirw
:-%    l :%r     %-:

Try it online!

Flipped, Rotated

:wl%%%rw:
-re   ir-
%if   gi%
 tt   ht 
 e)   te 
 (.   )( 
 %%   .% 
left).%  
 %.)thgir
:-write(%
%(etirw-:
right).% 
  %.)tfel
 %.   %% 
 ()   .( 
 er   )e 
 ti   lt 
%ig   ei%
-rh   fr-
:wt%%%tw:

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Prolog (SWI), 239 223 209 181 bytes

%%%%%%%  l:r%
right).% e-i%
:-write(%fwg%
left).%  trh%
  %      )it%
 %(      .t)%
%.e%     %e.%
%)t.      (%
%ti)      %
%hrt  %.)tfel
%gwf%(etirw-:
%i-e %.)thgir
%r:l  %%%%%%%

Try it online!

Since the program has 4 fold symmetry you only need to check the mirror:

%r:l  %%%%%%%
%i-e %.)thgir
%gwf%(etirw-:
%hrt  %.)tfel
%ti)      %  
%)t.      (% 
%.e%     %e.%
 %(      .t)%
  %      )it%
left).%  trh%
:-write(%fwg%
right).% e-i%
%%%%%%%  l:r%

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Haskell, 461 379 bytes

82 bytes saved by Ørjan Johansen

--_----------mppr--
-- ----------a  l--
rl=p  p--   niam= _
p m="left"-- n==p--
p a="right"++ "" --
main  =putStr rl --
--n +r       =iep--
-- -+t       pgf---
-- -"S       uht---
-- "tt       tt" --
---thu       S"- --
---fgp       t+- --
--pei=       r+ n--
-- lr rtStup=  niam
-- "" ++"thgir"=a p
--p==n --"tfel"=m p
_ =main   --p  p=lr
--l  a---------- --
--rppm----------_--

Try it online!

Since this one has 4-fold symmetry you only need to test the mirror:

--rppm----------_--
--l  a---------- --
_ =main   --p  p=lr
--p==n --"tfel"=m p
-- "" ++"thgir"=a p
-- lr rtStup=  niam
--pei=       r+ n--
---fgp       t+- --
---thu       S"- --
-- "tt       tt" --
-- -"S       uht---
-- -+t       pgf---
--n +r       =iep--
main  =putStr rl --
p a="right"++ "" --
p m="left"-- n==p--
rl=p  p--   niam= _
-- ----------a  l--
--_----------mppr--

Try it online!

This is a start. It is far from ideal, but there are a few interesting things going on here. Haskell is a certainly an interesting language for this challenge. I look forward to an answer that beats this be it mine or someone else's.

\$\endgroup\$
4
\$\begingroup\$

Python 2, 209 bytes (14 x 14)

Normal (left):

##########sps#
s="left"##=r=#
print s###"i"#
s="right"#rnl#
####      ite#
###"      g f#
#"#t      hst#
#tsh      t#"#
#f g      "###
#eti      ####
#lnr#"thgir"=s
#"i"###s tnirp
#=r=##"tfel"=s
#sps##########

Try it online!

Reflected (right):

#sps##########
#=r=##"tfel"=s
#"i"###s tnirp
#lnr#"thgir"=s
#eti      ####
#f g      "###
#tsh      t#"#
#"#t      hst#
###"      g f#
####      ite#
s="right"#rnl#
print s###"i"#
s="left"##=r=#
##########sps#

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Ruby, 181 bytes

#########tpt#
t=:left##=u=#
puts t###:t:#
t=:right#rsl#
####     i e#
###t     gtf#
#t#h     h#t#
#ftg     t###
#e i     ####
#lsr#thgir:=t
#:t:###t stup
#=u=##tfel:=t
#tpt#########

Try it online!

This is a port of Curtis Bechtel's Python answer to Ruby, golfed a little more.

Actually, his Python program is a polyglot and runs on Ruby too, so my first answer was a copy-paste of it, but it seemed unfair. So I wrote a script that generates a new answer starting from a base script. You can try it here:

Build your own

\$\endgroup\$
2
\$\begingroup\$

Batch, 438 321 bytes

:::::::::::::@@@:
@echo left&: eee:
@exit/b&:    cxc:
@echo right&:hih:
:  :         oto:
:: &          / :
:& t         rbl:
:t h         i&e:
:f:g         g:f:
:e&i         h t:
:lbr         t &:
: /          & ::
:oto         :  :
:hih:&thgir ohce@
:cxc    :&b/tixe@
:eee :&tfel ohce@
:@@@:::::::::::::

Explanation: A : is used to indicate a label, which is as good as a comment, so as the program has rotational symmetry the only difference between the two versions is which of the @ lines is first, in which case either left or right is output before the script exits.

I did try creatively overlapping the rotated lines with each other but my attempt actually ended up with a larger grid.

Edit: Saved 117 bytes thanks to @ØrjanJohansen.

\$\endgroup\$
  • \$\begingroup\$ I think you can replace &rem by &:. \$\endgroup\$ – Ørjan Johansen Sep 13 '18 at 1:31
  • \$\begingroup\$ @ØrjanJohansen I seem to remember trying that before on a similar challenge, but I don't think it worked well. \$\endgroup\$ – Neil Sep 13 '18 at 10:50
  • \$\begingroup\$ This (don't mind the Haskell, TIO doesn't seem to have Batch) worked for me as test.bat in the Win10 command prompt. \$\endgroup\$ – Ørjan Johansen Sep 14 '18 at 0:51
  • \$\begingroup\$ @ØrjanJohansen Fair enough, it might have been a bug in a previous version of CMD.EXE or something. \$\endgroup\$ – Neil Sep 14 '18 at 8:45
1
\$\begingroup\$

Runic Enchantments, 116 bytes

> \ !U\L
 ...@.. 
 \.R""" 
 @""trl 
 "lrhie 
 teiggf 
 ffgiht 
 ethrt" 
 l"t""@ 
 " "R.\ 
 .\@.@. 
DR\!!/R 
     !! 

Try it online!

And reversed

Not exactly a port, but utilized Post Left Garf Hunter's Klein 000 answer as a starting point, given the typical near-compatibility of the two languages (most commands are the same and the edge wrapping in Runic is identical to Klein 000). The only issue was that Klein IPs always start from the top left and Runic's do not. As such the . are all part of the original code and still treated as NOP by Runic, while the are NOPs I had to add as part of controlling the flow.

Prints "left" in all 4 rotations and "right" when mirrored (and in all four rotations thereof). Two middle columns were completely unused after my modifications, so I was able to remove them.

In theory a rotation with lots of trailing whitespace could be used for a lower byte count (eg. this variant), however the spaces are needed to correctly rotate the structure using external tools and so I included them.

Below variants contain the unused chunks that were removed, but are otherwise identical:

All four lefts. Outputs leftleftleftleft (the fact that all four can be run like this is coincidence).

Right 1, Right 2, Right 3, Right 4

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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