24
\$\begingroup\$

Task

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!

\$\endgroup\$
  • \$\begingroup\$ May we output more than 1 character? \$\endgroup\$ – Mr. Xcoder Aug 4 '17 at 16:52
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – Business Cat Aug 4 '17 at 16:58
  • 2
    \$\begingroup\$ Apart from the output spec, I think this is a duplicate \$\endgroup\$ – Digital Trauma Aug 4 '17 at 16:58
  • 1
    \$\begingroup\$ @BusinessCat You do not have to pad your code to make a rectangle - that example you provided would be valid. \$\endgroup\$ – CzarMatt Aug 4 '17 at 17:02
  • 4
    \$\begingroup\$ @Mr.Xcoder Um, how could 4 identical programs print 4 different ASCII chars? \$\endgroup\$ – ETHproductions Aug 4 '17 at 17:09

23 Answers 23

20
\$\begingroup\$

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:

North:

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

East:

*
2    -> literal 2 = 2

South:

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

West:

2
*    -> (implicit) zero raised to the power of (implicit) zero = 1
\$\endgroup\$
  • 1
    \$\begingroup\$ This is the final answer. Well done. \$\endgroup\$ – Erik the Outgolfer Aug 4 '17 at 17:22
19
\$\begingroup\$

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.

\$\endgroup\$
8
\$\begingroup\$

Java (OpenJDK 8), 7309 4421 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##
\$\endgroup\$
  • \$\begingroup\$ Great general appraoch, now I just need to figure out how to do it! :) \$\endgroup\$ – flawr Aug 4 '17 at 19:08
  • \$\begingroup\$ You can use interface M{static void main(String[]a){System.out.println(0);}} instead to save some bytes. \$\endgroup\$ – Leaky Nun Aug 4 '17 at 19:11
  • \$\begingroup\$ Would this be work? Its way shorter \$\endgroup\$ – Sriotchilism O'Zaic Aug 4 '17 at 19:58
  • 1
    \$\begingroup\$ @MagicOctopusUrn it wasn't done by hand c; \$\endgroup\$ – Rod Aug 11 '17 at 19:10
  • 1
    \$\begingroup\$ @MagicOctopusUrn nah, I made this answer to show this algorithm, the language is irrelevant :3 \$\endgroup\$ – Rod Aug 11 '17 at 19:38
7
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ 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! \$\endgroup\$ – DJMcMayhem Aug 4 '17 at 17:12
  • 1
    \$\begingroup\$ This is very cool! \$\endgroup\$ – CzarMatt Aug 4 '17 at 17:14
  • \$\begingroup\$ Trying to golf you :P so far have 3 of them working \$\endgroup\$ – Christopher Sep 10 '17 at 23:43
6
\$\begingroup\$

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!

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

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: forgot about newlines

Edit 2: realized I could create a cat

Edit 3: the cat is dead

2v3
@.v
.  
1@.

RIP kitty :<

1.@ 2
^._.^
3 @.4
\$\endgroup\$
5
\$\begingroup\$

05AB1E, 5 3 bytes

Y'X

North, East, South, West

\$\endgroup\$
  • \$\begingroup\$ Impressive, that was quick! \$\endgroup\$ – CzarMatt Aug 4 '17 at 16:44
  • \$\begingroup\$ @MagicOctopusUrn I think Y'X will work, but I have to try it still. \$\endgroup\$ – Riley Aug 11 '17 at 18:24
  • \$\begingroup\$ @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. \$\endgroup\$ – Magic Octopus Urn Aug 11 '17 at 18:29
  • \$\begingroup\$ @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. \$\endgroup\$ – Riley Aug 11 '17 at 18:40
  • \$\begingroup\$ 10 would've worked if they allowed multiple characters for an output ;P.* \$\endgroup\$ – Magic Octopus Urn Aug 11 '17 at 19:12
4
\$\begingroup\$

C (gcc), 283 279 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 ;)

\$\endgroup\$
  • \$\begingroup\$ Do you need any of the four slashes on the right edge right above the gap? \$\endgroup\$ – ETHproductions Aug 4 '17 at 23:39
  • \$\begingroup\$ Hey .. uhm ... I guess, actually ... no. Good catch, thanks :) \$\endgroup\$ – Felix Palmen Aug 4 '17 at 23:46
  • \$\begingroup\$ 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) \$\endgroup\$ – ETHproductions Aug 5 '17 at 0:14
  • \$\begingroup\$ 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. \$\endgroup\$ – ETHproductions Aug 5 '17 at 0:17
  • \$\begingroup\$ 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) \$\endgroup\$ – Felix Palmen Aug 5 '17 at 0:21
4
\$\begingroup\$

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.
\$\endgroup\$
4
\$\begingroup\$

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:

enter image description here

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

For the third program:

enter image description here

))) 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 $:

enter image description here

$   Skip the ).
$   Skip the @.
))  Increment an implicit zero to 2.
O   Print the 2.
))  Two irrelevant increments.
@   Terminate the program.
\$\endgroup\$
  • \$\begingroup\$ Super cool, great diagrams, too. \$\endgroup\$ – CzarMatt Feb 18 '18 at 17:51
3
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ Don't know powershell, but wouldn't this work? \$\endgroup\$ – Sriotchilism O'Zaic Aug 4 '17 at 19:34
  • \$\begingroup\$ @WheatWizard Yes, indeed. Thanks! \$\endgroup\$ – AdmBorkBork Aug 4 '17 at 19:37
3
\$\begingroup\$

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".

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – CzarMatt Aug 6 '17 at 1:45
  • \$\begingroup\$ @CzarMatt, thanks for the clarification! I've updated the post. \$\endgroup\$ – Not a tree Aug 6 '17 at 1:54
3
\$\begingroup\$

dc, 9 bytes

 1
2p3
 4

Try it online!

I think its obviousness is part of the charm.

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

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.

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

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.

\$\endgroup\$
  • \$\begingroup\$ Come to think of it, this would work in MATL as well. Same byte count as mine, though. \$\endgroup\$ – Sanchises Aug 5 '17 at 19:59
2
\$\begingroup\$

Cardinal, 20 17 12 11 bytes

.$
Z%"
+$v

Try it online!

All this time and no-one answered in Cardinal? This first one outputs 0.

East

 +Z.
 $%$
 v"

Try it online!

Outputs 1.

South


v$+
"%Z
 $.

Try it online!

Outputs a space.

West

 "v
$%$
.Z+

Try it online!

Outputs v.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ If it contains a space, it is 5 bytes. \$\endgroup\$ – Mr. Xcoder Aug 4 '17 at 17:14
  • 1
    \$\begingroup\$ @Mr.Xcoder OP in comments said that you don't need to pad it \$\endgroup\$ – dzaima Aug 4 '17 at 17:15
1
\$\begingroup\$

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)

\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – Zacharý Aug 5 '17 at 17:51
  • \$\begingroup\$ (Just add REPL after JS, then I think it's fine) \$\endgroup\$ – Zacharý Aug 5 '17 at 18:19
  • 1
    \$\begingroup\$ Output should be a single printable ASCII character, so like my MATLAB one, this is invalid. \$\endgroup\$ – Tom Carpenter Aug 5 '17 at 19:27
1
\$\begingroup\$

Sorry, I meant:

//0//
////
 1/2
/////
//3//

and 28B. And outputs as 0.5, 3, 2, 0.

\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – Zacharý Aug 5 '17 at 17:53
  • \$\begingroup\$ @Zacharý I think codegolf.meta.stackexchange.com/questions/7842/… says that REPLs are allowed \$\endgroup\$ – SuperStormer Aug 5 '17 at 18:03
  • \$\begingroup\$ Either way, they should specify that it is a JS REPL. \$\endgroup\$ – Zacharý Aug 5 '17 at 18:18
  • 2
    \$\begingroup\$ Output should be a single printable ASCII character, so like my MATLAB one, this is invalid. \$\endgroup\$ – Tom Carpenter Aug 5 '17 at 19:26
1
\$\begingroup\$

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>

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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