# Cardinal Code Challenge

You're in charge of making a compass, of sorts.

Imagine your source code as the compass "needle" where running at different orientations produces distinct output.

Supported source code orientations are North, East, South, and West.

## Example

Let's say you have source code:

ABCD
J K
WXYZ


We'll consider this the North orientation, rotating 90 degrees clockwise points us to the East:

W A
XJB
Y C
ZKD


rotating again points South:

ZYXW
K J
DCBA


and finally, the last rotation to the West:

DKZ
C Y
BJX
A W


When ran, each of the above code examples should output a single, distinct printable ASCII character of your choosing.

## Notes

Your code shall take no input.

Empty spaces or new lines do not collapse/disappear when rotating.

Leading/trailing new lines are okay in output.

Answers may be whole programs or functions, thus output to STDOUT or return the function result.

Standard rules apply; shortest answer in bytes wins!

• May we output more than 1 character? – Mr. Xcoder Aug 4 '17 at 16:52
• Do we have to pad the code with spaces so it's a perfect rectangle (and count those spaces in our score)? For example, would code shaped like this be valid, given the first one is the submission? – Business Cat Aug 4 '17 at 16:58
• Apart from the output spec, I think this is a duplicate – Digital Trauma Aug 4 '17 at 16:58
• @BusinessCat You do not have to pad your code to make a rectangle - that example you provided would be valid. – CzarMatt Aug 4 '17 at 17:02
• @Mr.Xcoder Um, how could 4 identical programs print 4 different ASCII chars? – ETHproductions Aug 4 '17 at 17:09

# Jelly, 2 bytes

*2


Try it online!

Note that the main entry for a Jelly program is its last link, where any newline character will split links), neither of the two-line programs actually access their top link.

The four full programs, all of which implicitly print their result, are:

*2   -> (implicit) zero raised to the power of 2 = 0


East:

*
2    -> literal 2 = 2

2*   -> two raised to the power of (implicit) 2 = 4


West:

2
*    -> (implicit) zero raised to the power of (implicit) zero = 1

• This is the final answer. Well done. – Erik the Outgolfer Aug 4 '17 at 17:22

# Japt, 3 2 bytes

gy


Somehow, somehow, I found an extremely hacky 2-byte solution...

North outputs 0:

gy


Since there's no implicit input, it defaults to 0. g on a number returns the sign of the number regardless of its arguments ("y" in this case).

East outputs 2:

g
y


In a multi-line program, the first line sets the input to its result. This is basically a no-op, since g on 0 is 0. Then y returns the GCD of 0 and... since it's missing an argument, it defaults to 2 (thanks, @Oliver!). This gives 2 as the output.

South outputs g:

yg


y, as before, is GCD. Since gcd(0, x) is x for any value, y on 0 takes the liberty of just returning its argument. In this case, the argument is "g", which is the result.

West outputs 1:

y
g


y on 0, as before, returns 2. This is then passed to g, which (as already discussed) is the sign function on numbers. Therefore, the result is 1.

# Java (OpenJDK 8), 73094421 855 bytes

-2888 bytes thanks to Leaky Nun
-3566 bytes thanks to Wheat Wizard

//i/////////////////////////////////////////////////////////////
//n//////////////////////////////////////////////////////////////////
interface M{static void main(String[]a){System.out.println(0);}}/////
//e//}};)2(nltnirp.tuo.metsyS{)a][gnirtS(niam diov citats{M ecafretni
//r//////////////////////////////////////////////////////////////////
//f}/////////////////////////////////////////////////////////////////
//a}//
//c;//
//e)//
// 3//
//M(//
//{n//
//sl//
//tt//
//an//
//ti//
//ir//
//cp//
// .//
//vt//
//ou//
//io//
//d.//
// m//
//me//
//at//
//is//
//ny//
//(S//
//S{//
//t)//
//ra//
//i]//
//n[//
//gg//
//[n//
//]i//
//ar//
//)t//
//{S//
//S(//
//yn//
//si//
//ta//
//em//
//m //
//.d//
//oi//
//uo//
//tv//
//. //
//pc//
//ri//
//it//
//na//
//tt//
//ls//
//n{//
//(M//
//1 //
//)e//
//;c//
//}a//
//}f//
///r//
///e//
//t//
//n//
//i//


Try it online!

Old version

Straightforward aproach with comments wrapping the code^2 square, this can be done in pretty much any language.
a (more readable) example in python

##p#####
# r  2 #
print 1#
# n  t #
# t  n #
#4 tnirp
# 3  r #
#####p##

• Great general appraoch, now I just need to figure out how to do it! :) – flawr Aug 4 '17 at 19:08
• You can use interface M{static void main(String[]a){System.out.println(0);}} instead to save some bytes. – Leaky Nun Aug 4 '17 at 19:11
• Would this be work? Its way shorter – Ad Hoc Garf Hunter Aug 4 '17 at 19:58
• @MagicOctopusUrn it wasn't done by hand c; – Rod Aug 11 '17 at 19:10
• @MagicOctopusUrn nah, I made this answer to show this algorithm, the language is irrelevant :3 – Rod Aug 11 '17 at 19:38

# Brain-Flak, 33 bytes

##)(##))()()  ((
((  ))##    ()##


Try it online!

# Brain-Flak, 33 bytes

##)     ## #
(( ))#)())()
# ( (


Try it online!

# Brain-Flak, 36 bytes

#)##     ## #
(())#)())()
#   ( (


Try it online!

# Brain-Flak, 38 bytes

######  (#
(()()())#))((
#(  ##


Try it online!

# Brain-Flak, 41 bytes

##(#####(#
(()()())#
##))()((
####((#)#)#


Try it online!

• Holy crap! I was working on one, but I couldn't get anywhere. I thought it would take atleast 30-40 minutes of work to figure something out. This is amazing! – James Aug 4 '17 at 17:12
• This is very cool! – CzarMatt Aug 4 '17 at 17:14
• Trying to golf you :P so far have 3 of them working – Christopher Sep 10 '17 at 23:43

# Alice, 17 bytes

1/ 3<
vPo</
5} @<


Outputs 1. Try it online!

5v1
}P/
o
@<3
</<


Outputs x. Try it online!

<@ }5
/<oPv
<3 /1


Outputs 5. Try it online!

</<
3<@
o
/P}
1v5


Outputs 3. Try it online!

# Befunge, 17 13 bytes

I thought Befunge would be fun for a geometrical problem. There is a trivial 4x4 solution akin to others here (I need 3 commands) but I managed a bit better.

Edit 2: realized I could create a cat

Edit 3: the cat is dead

2v3
@.v
.
1@.


RIP kitty :<

1.@ 2
^._.^
3 @.4


# 05AB1E, 5 3 bytes

Y'X

• Impressive, that was quick! – CzarMatt Aug 4 '17 at 16:44
• @MagicOctopusUrn I think Y'X will work, but I have to try it still. – Riley Aug 11 '17 at 18:24
• @Riley it does, also it works forward and backwards, Y'X is also valid. Still looking for a 2-byte though; none of the 'dot commands' work for it, so I'm doubtful it exists. – Magic Octopus Urn Aug 11 '17 at 18:29
• @MagicOctopusUrn I don't think there is a 2 byte solution with the way newlines work. It wouldn't be hard to brute Force though. – Riley Aug 11 '17 at 18:40
• 10 would've worked if they allowed multiple characters for an output ;P.* – Magic Octopus Urn Aug 11 '17 at 19:12

# C (gcc), 283279 209 bytes

/////////)pm//
/////////;ua//
main(){//}ti//
puts("N"//sn//
);}///////((//
//////////")//
///"//////W{//
///E//////"///
//)"//////////
//((///////};)
//ns//"S"(stup
//it}//{)(niam
//au;/////////
//mp)/////////


Try it online!

Same old comment trick here, but at least, in C this doesn't get huuuge ;)

• Do you need any of the four slashes on the right edge right above the gap? – ETHproductions Aug 4 '17 at 23:39
• Hey .. uhm ... I guess, actually ... no. Good catch, thanks :) – Felix Palmen Aug 4 '17 at 23:46
• I think you can pack it together quite a bit more tightly by moving each );} to the line below, like so (I haven't tested the rotations though) – ETHproductions Aug 5 '17 at 0:14
• Oh, the W program in your current setup currently fails because there's an extra sn after the actual code. Apparently you can fix this by changing the slash right before the pm on the top line to a semicolon. – ETHproductions Aug 5 '17 at 0:17
• uhh ... probably time to delete this and start over :o (first version was a square of slashes, but I thought I'd do a "clever" thing saving some bytes ... dammit) – Felix Palmen Aug 5 '17 at 0:21

## Labyrinth, 9 bytes

!
2@2
!)


Prints 0. Try it online!

 2)
@!
!2


Prints 3. Try it online!

)!
2@2
!


Prints 1. Try it online!

 2!
!@
)2


Prints 2. Try it online!

### Explanation

Each program starts at the first non-space in reading order (i.e. either the top left or top centre character), moving east. For the first program:

!   Print an implicit zero.
The IP can't move east, so it moves south instead.
2   Push a 2.
The IP can't keep going south, so it turns east instead.
@   Terminate the program.


For the second program:

2   Push a 2.
)   Increment it to 3.
The IP can't keep going east, so it turns south instead.
!   Print the 3.
The IP can't keep going south, so it turns west instead.
@   Terminate the program.


For the third program:

)   Increment an implicit zero to 1.
!   Print the 1.
The IP can't keep going east, so it turns south instead.
@   Terminate the program.


For the fourth program:

2   Push a 2.
!   Print the 2.
The IP can't keep going east, so it turns back around to move west.
2   Push another 2.
The IP can't keep going west, so it turns south instead.
@   Terminate the program.


## Wumpus, 7 bytes

O@$)))  Prints 0. Try it online! )O )@ )$


Prints 1. Try it online!

)))
$@O  Prints 3. Try it online! $)
@)
O)


Prints 2. Try it online!

### Explanation

The first program is easy enough: O prints an implicit zero and @ terminates the program.

Starting at the second program, we need to look at the triangular grid layout to understand the control flow:

)   Increment an implicit zero to 1.
O   Print the 1.
))  Two irrelevant increments.
@   Terminate the program.


For the third program:

))) Increment an implicit zero to 3.
O   Print the 3.
@   Terminate the program.


The fourth one is where it gets really funky. Dashed lines indicate cells that aren't executed because they are skipped by the $: $   Skip the ).
$Skip the @. )) Increment an implicit zero to 2. O Print the 2. )) Two irrelevant increments. @ Terminate the program.  • Super cool, great diagrams, too. – CzarMatt Feb 18 '18 at 17:51 # PowerShell, 20 11 bytes #4# 1#3 #2#  Abuses comments (#) like crazy, and the fact that a single number placed onto the pipeline gets output as-is. The above prints 1. Try it online! From here, you can easily see that each rotation yields only one number that's on the "left" of the comments, and so there's only one number that will be output per rotation. Saved 9 bytes thanks to Wheat Wizard! • Don't know powershell, but wouldn't this work? – Ad Hoc Garf Hunter Aug 4 '17 at 19:34 • @WheatWizard Yes, indeed. Thanks! – AdmBorkBork Aug 4 '17 at 19:37 # Starry, 34 bytes  zz + . +  Or with spaces shown as hyphens so you can see them: --zz-- --+--- ---.-- ----+- ------  Try it online! The commands in Starry are +, . and some other things, and what they do is determined by how many spaces there are before them: a + with n spaces pushes n−5 to the stack, and . with an even number of spaces prints it. The zs and newlines are ignored entirely. There are 6 spaces before the first + so it pushes 6−5 = 1, and the . prints it. And the rotations: ----- ----- ---+z --.-z -+--- -----  Try it online! This prints "8". ------ -+---- --.--- ---+-- --zz--  Try it online! This prints "2". ----- ---+- z-.-- z+--- ----- -----  Try it online! And this prints "3". • What a neat language. Also, I never said you have to pad with whitespace to form a rectangle. But if your source relies on the spaces then I suppose you have to count them. – CzarMatt Aug 6 '17 at 1:45 • @CzarMatt, thanks for the clarification! I've updated the post. – Not a tree Aug 6 '17 at 1:54 # dc, 9 bytes  1 2p3 4  Try it online! I think its obviousness is part of the charm. ## Batch, 90 bytes  :: :::@: :&s ohce@ :e : c: :h: :o o: :h: :c : w: @echo n&: :@::: ::  Batch doesn't really have a comment character. For whole-line comments, : works, as it introduces a label, but I still need something to terminate the echo command while being a no-op when reversed. &: seems to work, which is all I need here, but it really confuses Batch, erroring out if I don't put a : before the @ on the next line, and also somehow forgetting to print a newline. # MATLAB, 29 17 5 11 bytes Having realised that the question called for single ASCII characters not just a distinct output, here is a MATLAB approach which will do just that: %4% 1%3 %2%  This will implicitly print 1, 2, 3, or 4 depending on the rotation. • Come to think of it, this would work in MATL as well. Same byte count as mine, though. – Sanchises Aug 5 '17 at 19:59 # Cardinal, 20 17 12 11 bytes .$
Z%"
"%Z
$.  Try it online! Outputs a space. ### West  "v$%$.Z+  Try it online! Outputs v. # SOGL V0.12, 4 bytes 5H I  Outputs 1, Try it Here! I5 H  Outputs H, Try it Here!  I H5  Outputs 5, Try it Here! H 5I  Outputs 6, Try it Here! • If it contains a space, it is 5 bytes. – Mr. Xcoder Aug 4 '17 at 17:14 • @Mr.Xcoder OP in comments said that you don't need to pad it – dzaima Aug 4 '17 at 17:15 # JS, 17 B //1// 2///4 //3//  Outputs: North: 2, East: 3, South: 4, West: 0.33333333333…. (as in: 2, 3/1, 4, 1/3) • Welcome to PPCG! I don't think this is valid, as in a non-REPL environment, the number won't be displayed. (I might be wrong there) – Zacharý Aug 5 '17 at 17:51 • (Just add REPL after JS, then I think it's fine) – Zacharý Aug 5 '17 at 18:19 • Output should be a single printable ASCII character, so like my MATLAB one, this is invalid. – Tom Carpenter Aug 5 '17 at 19:27 Sorry, I meant: //0// //// 1/2 ///// //3//  and 28B. And outputs as 0.5, 3, 2, 0. • Welcome to PPCG! You should put the language name and byte count in a header. And this is a snippet, not a full program or a function, which isn't valid. (I might be wrong) – Zacharý Aug 5 '17 at 17:53 • @Zacharý I think codegolf.meta.stackexchange.com/questions/7842/… says that REPLs are allowed – SuperStormer Aug 5 '17 at 18:03 • Either way, they should specify that it is a JS REPL. – Zacharý Aug 5 '17 at 18:18 • Output should be a single printable ASCII character, so like my MATLAB one, this is invalid. – Tom Carpenter Aug 5 '17 at 19:26 # JavaScript (ES6), 86 bytes Outputs 0 for North, 1 for East, 2 for South, and 3 for West. //// _// //// =// _=>0//>// ////1// // // //3//// //>//2>=_ //= //// //_ ////  const source = //// _// //// =// _=>0//>// ////3// // // //1//// //>//2>=_ //= //// //_ ////; function rotateTextCW(text) { const lines = text.split("\n"); const maxLineLength = lines .map(line => line.length) .reduce((max, len) => Math.max(len, max), 0); const paddedLines = lines.map(line => line.padEnd(maxLineLength)); const charGrid = paddedLines.map(line => line.split("")); const rotatedGrid = [...new Array(maxLineLength)] .map(v => new Array(charGrid.length)); for (let i = 0; i < charGrid.length; i++) { for (let j = 0; j < charGrid[i].length; j++) { rotatedGrid[j][charGrid.length - i - 1] = charGrid[i][j]; } } return rotatedGrid .map(chars => chars.join("").trimRight()) .join("\n"); } const orientations = ["North", "East", "South", "West"]; let rotatedSource = source; let markup = ""; orientations.forEach(orientation => { const code = <pre>${rotatedSource}</pre>;
const result = <code>${eval(rotatedSource)()}</code>; const row =  <td>${orientation}</td>
<td>${code}</td> <td>${result}</td>;
markup += <tr>${row}</tr>; rotatedSource = rotateTextCW(rotatedSource); }); document.querySelector("table tr:first-child") .parentNode .innerHTML += markup; <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="container"> <table class="table table-striped table-responsive"> <tr> <th>Orientation</th> <th>Source</th> <th>Result</th> </tr> </table> </div> # MATL, 11 bytes HxI xFx TxK  Try it online! Let's get this started in MATL. The main challenge is that MATL just fails if a function requires input if the stack is empty. Perhaps something clever with modifiers like X, Y, Z and & could make for something shorter, but I couldn't find a suitable combination. Explanation: all characters push a single integer on the stack, and x removes all of them but the last. ## Perl, 49 bytes 48 bytes code + 1 for -p. Assumes empty input which TIO doesn't support, so a newline is added in its place and not used. Prints N, E, S, W. # #### #S= _$
#; W#
$_=N# #_ _# #=$#
#E#
## #


Try it online!

# C (gcc), 120 bytes

I had hoped for more synergy between the variants.

//}=)f8 ///
// c{(7 ///
;rr; //}
;c=
78; /// r{)
f(r /// r(f
){r /// ;38
=c;
}// ;rr;
/// 9({c //
/// 6f)=}//


North

East

South

West