Reading Code in 8 Ways to Output 8 Numbers

21

3

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.

Calvin's Hobbies

Posted 2015-02-21T10:46:53.680

Reputation: 84 000

6Why the "only printable ascii" rule? It means APL can't compete in this challenge. :( – Moris Zucca – 2015-02-21T10:53:12.380

3

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.

– PurkkaKoodari – 2015-02-21T12:34:02.830

3

@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

– PurkkaKoodari – 2015-02-21T16:47:02.153

@MorisZucca All characters besides line terminators are now allowed. – Calvin's Hobbies – 2015-02-21T22:17:13.150

@Pietu1998 Thanks for the Fiddle, I've added it as a snippet. – Calvin's Hobbies – 2015-02-21T22:17:32.127

I am wondering if it might be possible to solve this using a solution of the form: a+b (line break) +c+ (line break) d+e where any or all of the +'es can be changed to any of the fundamental arithmetic operations. If so, that solution would be valid in many languages. But I guess that's more a mathematical problem than a codegolf :-) – Dan – 2015-02-23T11:11:39.877

I'm curious why "1" isn't a valid answer, (score 1), given that your language always evaluates and prints the result to stdout? I'm thinking of programs like "pyth" or "bc"? If there are some minimum number of rows and columns, then wouldn't that result in an automatic win with no thinking? – swstephe – 2015-02-25T23:31:40.137

@swstephe It needs to output the numbers 1-8. – Calvin's Hobbies – 2015-02-25T23:34:52.313

Answers

21

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.

randomra

Posted 2015-02-21T10:46:53.680

Reputation: 19 909

5This is really nice. Replace [ with ; and it should work in most languages' REPL environment. – Optimizer – 2015-02-21T17:04:11.183

18

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

PurkkaKoodari

Posted 2015-02-21T10:46:53.680

Reputation: 16 699

14

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

ProgramFOX

Posted 2015-02-21T10:46:53.680

Reputation: 8 017

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. – matega – 2015-02-22T15:31:04.063

I'd be ok with binary output. – captncraig – 2015-02-23T20:11:21.750

7

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

feersum

Posted 2015-02-21T10:46:53.680

Reputation: 29 566

7

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

captncraig

Posted 2015-02-21T10:46:53.680

Reputation: 4 373

6

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

Martin Ender

Posted 2015-02-21T10:46:53.680

Reputation: 184 808

5

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

HEGX64

Posted 2015-02-21T10:46:53.680

Reputation: 313

2It seems to me that this can be extended to most languages with inline comments – Sp3000 – 2015-02-22T05:48:10.193

4

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

Martin Ender

Posted 2015-02-21T10:46:53.680

Reputation: 184 808

2

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

aditsu quit because SE is EVIL

Posted 2015-02-21T10:46:53.680

Reputation: 22 326

1

Ruby - 7x7

Not impressive either, same tactic as the Haskell answer.

p 1#2 p

3     7
#     #
4     8

p 5#6 p

sj26

Posted 2015-02-21T10:46:53.680

Reputation: 111

1

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

freekvd

Posted 2015-02-21T10:46:53.680

Reputation: 909

1

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.

BobTheAwesome

Posted 2015-02-21T10:46:53.680

Reputation: 509