21
\$\begingroup\$

The English language and most programming languages are written and read from left-to-right, top-to-bottom, but that needn't be the case.

In fact for the block of text

ABC
DEF

I can think of eight related ways it might be read:

  1. Left-to-right, top-to-bottom (LTR-TTB): ABCDEF
  2. Top-to-bottom, left-to-right (TTB-LTR): ADBECF
  3. Left-to-right, bottom-to-top (LTR-BTT): DEFABC
  4. Bottom-to-top, left-to-right (BTT-LTR): DAEBFC
  5. Right-to-left, top-to-bottom (RTL-TTB): CBAFED
  6. Top-to-bottom, right-to-left (TTB-RTL): CFBEAD
  7. Right-to-left, bottom-to-top (RTL-BTT): FEDCBA
  8. Bottom-to-top, right-to-left (BTT-RTL): FCEBDA

Challenge

Write a rectangular block of text that can be read in each of the eight ways above as eight single line programs in your language of choice. Each of these programs should output a different integer from one to eight.

It doesn't matter which reading direction outputs which number, they do not have to match the numbers above. For example, if your text block were still

ABC
DEF

then the program ABCDEF might output 5 and FEDCBA might output 2, and the other six programs would output 1, 3, 4, 6, 7, and 8 in some order.

The text block may contain any characters except line terminators.

Output should go to stdout or a similar alternative if your language doesn't have a proper stdout. There is no input. You may assume the programs are run in a REPL environment.

Pietu1998 charitably wrote a JSFiddle that gives the 8 different single line programs when given a block of text. I've made it into a stack snippet:

<script>function f(n){n=n.split("\n");var e=n.map(function(n){return n.length}).sort()[n.length-1];n=n.map(function(n){return(n+Array(e+1).join(" ")).substring(0,e)});var t=n[0].split("").map(function(e,t){return n.map(function(n){return n[t]}).join("")});n=[n.join(""),n.reverse().join(""),t.join(""),t.reverse().join("")],n=n.concat(n.map(function(n){return n.split("").reverse().join("")})),document.getElementById("a").innerHTML=n.map(function(n,e){return document.getElementById("b").checked?n+"   "+"LLTTRRBB"[e]+"T"+"RRBBLLTT"[e]+"-"+"TBLRBTRL"[e]+"T"+"BTRLTBLR"[e]:n}).join("\n")}</script><textarea onkeyup="f(this.value)" id="c" placeholder="Code"></textarea><br/><input type="checkbox" id="b" onchange="f(document.getElementById('c').value)" checked/>&nbsp;<label for="b">Show directions</label><br/><pre id="a"></pre>

You can still find Martin's CJam version here.

Scoring

Your score is the area of your block of text (the width times the height). The submission with the lowest score wins. (Essentially the smallest code wins, hence the tag.) Tiebreaker goes to the earlier posted submission.

The example is 2 by 3 so its score is 6. A score less than 4 (2 by 2) is impossible because then some of the 8 programs would be identical and couldn't output two different values.

\$\endgroup\$
8
  • 6
    \$\begingroup\$ Why the "only printable ascii" rule? It means APL can't compete in this challenge. :( \$\endgroup\$ Commented Feb 21, 2015 at 10:53
  • 3
    \$\begingroup\$ I made a fiddle that converts a code block to the 8 programs. Could be included as a snippet, want OP's opinion on this though. \$\endgroup\$ Commented Feb 21, 2015 at 12:34
  • 3
    \$\begingroup\$ @MartinBüttner All right, I fixed it. It seems String.prototype.repeat() is still kinda new. Also confirmed to work in IE now. new fiddle \$\endgroup\$ Commented Feb 21, 2015 at 16:47
  • \$\begingroup\$ @MorisZucca All characters besides line terminators are now allowed. \$\endgroup\$ Commented Feb 21, 2015 at 22:17
  • \$\begingroup\$ @Pietu1998 Thanks for the Fiddle, I've added it as a snippet. \$\endgroup\$ Commented Feb 21, 2015 at 22:17

12 Answers 12

21
\$\begingroup\$

J, 3 * 3 = 9

1[2
+2+
2+2

Executing all directions:

       ". '1[2+2+2+2','2+2+2+1[2','1+2[2+2+2','2+2[2+1+2','2+2+2+2[1','2[1+2+2+2','2+2+2[2+1',:'2+1+2[2+2'
1 7 3 4 8 2 6 5

Explanation:

  • In J execution goes from right to left and there is no operator precedence.
  • The [ (left) takes the left side of its two operand so it essentially cancels the whole right side of our expression e.g. 1+2[2+2+2 becomes 1+2[6 and then 1+2.
  • The left expressions are additions with a total of 1, 2, 3 and 4 operands. There are two of each n-operand expressions one with the number 1 and one with only 2s. The additions with 1 generate the odd numbers and the others generate the even ones.
\$\endgroup\$
1
  • 5
    \$\begingroup\$ This is really nice. Replace [ with ; and it should work in most languages' REPL environment. \$\endgroup\$
    – Optimizer
    Commented Feb 21, 2015 at 17:04
19
\$\begingroup\$

Befunge-98, 5x5 = 25 5x3 = 15

1+4+2
$.@.$
3+4+4

I wrote a little script that found the correct numbers for me. It took a while, but hey, I just beat GolfScript! :D

The scripts I used are here and here, but I don't suggest looking at them since the code style is extremely carcinogenic.

Subprograms

1+4+2$.@.$3+4+4   LTR-TTB   5
3+4+4$.@.$1+4+2   LTR-BTT   7
1$3+.+4@4+.+2$4   TTB-LTR   3
2$4+.+4@4+.+1$3   TTB-RTL   4
4+4+3$.@.$2+4+1   RTL-BTT   8
2+4+1$.@.$4+4+3   RTL-TTB   6
4$2+.+4@4+.+3$1   BTT-RTL   2
3$1+.+4@4+.+4$2   BTT-LTR   1

Old version

 1.6
3 @ 4
.@ @.
8 @ 7
 2.5

Subprograms

Output the numbers 1-8 respectively.

 1.6 3 @ 4.@ @.8 @ 7 2.5    LTR-TTB
 2.5 8 @ 7.@ @.3 @ 4 1.6    LTR-BTT
 3.8 1 @ 2.@ @.6 @ 5 4.7    TTB-LTR
 4.7 6 @ 5.@ @.1 @ 2 3.8    TTB-RTL
 5.2 7 @ 8.@ @.4 @ 3 6.1    RTL-BTT
 6.1 4 @ 3.@ @.7 @ 8 5.2    RTL-TTB
 7.4 5 @ 6.@ @.2 @ 1 8.3    BTT-RTL
 8.3 2 @ 1.@ @.5 @ 6 7.4    BTT-LTR
\$\endgroup\$
0
14
\$\begingroup\$

Brainfuck$, 4x3 = 12

Brainfuck$ is very similar to Brainfuck, but has some more commands, including a command to output the current cell value as numeric output, which was very useful for this challenge.

++:+
  ++
++++

One-line commands:

++:+  ++++++   LTR-TTB, outputs 2
++++  ++++:+   LTR-BTT, outputs 8
+ ++ +:+++++   TTB-LTR, outputs 4
+++:+++ ++ +   TTB-RTL, outputs 3
++++++  +:++   RTL-BTT, outputs 7
+:++++  ++++   RTL-TTB, outputs 1
+++++:+ ++ +   BTT-RTL, outputs 5
+ ++ +++:+++   BTT-LTR, outputs 6
\$\endgroup\$
2
  • \$\begingroup\$ I'd argue you don't need Brainfuck$'s ; command. I'd consider using simply . and outputting 0x01-0x08 perfectly valid. It's Brainfuck, for (brain)fuck's sake. +1. \$\endgroup\$
    – matega
    Commented Feb 22, 2015 at 15:31
  • \$\begingroup\$ I'd be ok with binary output. \$\endgroup\$
    – captncraig
    Commented Feb 23, 2015 at 20:11
7
\$\begingroup\$

TECO, 3*5 = 15

 +4 5
+2=4 
 +1 \

= prints the value of the last numerical expression. \ is used to read or write numbers from strings, but I use it only as a discard here.

  • +4 5+2=4 +1 \ 7
  • + +2+4=1 4 5 \ 6
  • + +2+1=4 4 \ 5 3
  • +1 \+2=4 +4 5 2
  • \ 1+ 4=2+5 4+ 5
  • \ 5 4 1=4+2+ + 1
  • 5 \ 4 4=1+2+ + 4
  • 5 4+ 4=2+\ 1+ 8
\$\endgroup\$
7
\$\begingroup\$

piet - 12x12 = 144

enter image description here

Since a one line program can never terminate, assume it terminates after first output.

8 subprograms in a single image:

enter image description here

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

GolfScript, 4x4 = 16

1})7
)  }
}  )
3)}5

Makes use of the good old "super comment": an unmatched } ignores the rest of the code (in fact, in this case a normal comment # would have worked just as well, since all the code is run as a single line). So from each corner there's either only a single number, or (in the other direction) that number incremented by 1, because execution terminates and the stack contents are printed. The 8 programs are

1})7)  }}  )3)}5  # LTR-TTB
1)}3}  ))  }7})5  # TTB-LTR
3})1)  }}  )5)}7  # BTT-LTR
3)}5}  ))  }1})7  # LTR-BTT
5})3)  }}  )7)}1  # RTL-BTT
5)}7}  ))  }3})1  # BTT-RTL
7})5)  }}  )1)}3  # TTB-RTL
7)}1}  ))  }5})3  # RTL-BTT
\$\endgroup\$
5
\$\begingroup\$

Haskell, 26x26 = 676

Uses comments (--) to hide the backwards and sideways bits. This makes it terribly long

main=print 1--2 tnirp=niam
a                        a
i                        i
n                        n
=                        =
p                        p
r                        r
i                        i
n                        n
t                        t

3                        4
-                        -
-                        -
5                        6

t                        t
n                        n
i                        i
r                        r
p                        p
=                        =
n                        n
i                        i
a                        a
main=print 7--8 tnirp=niam
\$\endgroup\$
1
  • 2
    \$\begingroup\$ It seems to me that this can be extended to most languages with inline comments \$\endgroup\$
    – Sp3000
    Commented Feb 22, 2015 at 5:48
4
\$\begingroup\$

Prelude, 5x3 = 15

12 34
  !  
56 78

This assumes the Python interpreter, which prints values as numbers instead of character codes.

This doesn't beat Befunge$, but it does beat my GolfScript submission, and I like its simplicity. Note also that it uses only 9 non-space characters, which is less than any other submission so far (J strikes again :)). In Prelude, each digit is pushed onto the stack individually, and depending on the path, there's a different digit right before the !, which just prints the top stack element. The 8 programs are:

12 34  !  56 78   LTR-TTB   4
56 78  !  12 34   LTR-BTT   8
1 52 6 ! 3 74 8   TTB-LTR   6
4 83 7 ! 2 61 5   TTB-RTL   7
87 65  !  43 21   RTL-BTT   5
43 21  !  87 65   RTL-TTB   1
8 47 3 ! 6 25 1   BTT-RTL   3
5 16 2 ! 7 38 4   BTT-LTR   2

Alternatively, there's also

 1 3 
1+!+1
 5 7 

Which pushes the odd numbers on the vertical paths, and increments them by 1 on the horizontal paths:

 1 3 1+!+1 5 7    LTR-TTB   4
 5 7 1+!+1 1 3    LTR-BTT   8
 1 1+5 ! 3+7 1    TTB-LTR   5
 1 3+7 ! 1+5 1    TTB-RTL   7
 7 5 1+!+1 3 1    RTL-BTT   6
 3 1 1+!+1 7 5    RTL-TTB   2
 1 7+3 ! 5+1 1    BTT-RTL   3
 1 5+1 ! 7+3 1    BTT-LTR   1
\$\endgroup\$
2
\$\begingroup\$

CJam - 7×7

Not impressive, but there was no CJam answer and I like the way it looks :)

 8;];1 
7     2
;  0  ;
] 0 0 ]
;  0  ;
6     3
 5;];4 

It mainly uses the fact that ]; clears the stack.

Try it online

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

R, 9x9

No white space, no comments.

75%%99299
99%%99699
%%299%%41
%%699%%06
995999997
11%%999%%
99%%799%%
39719%%99
59519%%16

I guess this could be expanded to whatever size you want. I thought the modulo operator was most the most flexible of available operators, since it prevents values from being too large regardless of data size, and there's no effect from having really large numbers in between.

75%%9929999%%99699%%299%%41%%699%%0699599999711%%999%%99%%799%%39719%%9959519%%16   LTR-TTB  2
59519%%1639719%%9999%%799%%11%%999%%995999997%%699%%06%%299%%4199%%9969975%%99299   LTR-BTT  3
79%%9193559%%91999%%265%%75%%999%%1199999979999%%999%%26%%999%%99409%%9199167%%96   TTB-LTR  4
99167%%9699409%%9126%%999%%99%%999%%999999799%%999%%11%%265%%7559%%9199979%%91935   TTB-RTL  1
61%%9159599%%91793%%997%%99%%999%%1179999959960%%996%%14%%992%%99699%%9999299%%57   RTL-BTT  5
99299%%5799699%%9914%%992%%60%%996%%799999599%%999%%11%%997%%9999%%9179361%%91595   RTL-TTB  6
69%%7619919%%90499%%999%%62%%999%%9999799999911%%999%%57%%562%%99919%%9553919%%97   BTT-RTL  7
53919%%9799919%%9557%%562%%11%%999%%997999999%%999%%99%%999%%6219%%9049969%%76199   BTT-LTR  8
\$\endgroup\$
1
\$\begingroup\$

Ruby - 7x7

Not impressive either, same tactic as the Haskell answer.

p 1#2 p

3     7
#     #
4     8

p 5#6 p
\$\endgroup\$
1
\$\begingroup\$

This Programming Language, 5 * 9 = 45

1i;i2
2   2
+   +
i   i
;   ;
i   i
+   +
2   2
5i;i6

Which translates into this:

1i;i22   2+   +i   i;   ;i   i+   +2   25i;i6
5i;i62   2+   +i   i;   ;i   i+   +2   21i;i2
12+i;i+25i       i;       ;i       i22+i;i+26
22+i;i+26i       i;       ;i       i12+i;i+25
6i;i52   2+   +i   i;   ;i   i+   +2   22i;i1
2i;i12   2+   +i   i;   ;i   i+   +2   26i;i5
62+i;i+22i       i;       ;i       i52+i;i+21
52+i;i+21i       i;       ;i       i62+i;i+22

Since ; terminates the program, the above translates into this:

1i;   - outputs 1
5i;   - outputs 5
12+i; - outputs 3
22+i; - outputs 4
6i;   - outputs 6
2i;   - outputs 2
62+i; - outputs 8
52+i; - outputs 7

Explanation: any number from 0 to 9 pushes the corresponding digit onto the stack. + pops the top two values x and y off the stack and pushes x + y onto the stack. i outputs the stack as an integer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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