Cardinal Code Challenge

24

1

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!

CzarMatt

Posted 2017-08-04T16:38:46.027

Reputation: 1 769

May we output more than 1 character? – Mr. Xcoder – 2017-08-04T16:52:55.627

Does a newline count as a printable character? (I don't think so, just asking for clarification.) – ETHproductions – 2017-08-04T16:55:40.373

1

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 – 2017-08-04T16:58:27.043

2

Apart from the output spec, I think this is a duplicate

– Digital Trauma – 2017-08-04T16:58:56.977

@Mr.Xcoder Output should be a single character. I originally wanted only N, E, S, W, as output but thought perhaps that was too difficult or unnecessarily constraining. – CzarMatt – 2017-08-04T16:59:06.457

1@BusinessCat You do not have to pad your code to make a rectangle - that example you provided would be valid. – CzarMatt – 2017-08-04T17:02:38.700

@ETHproductions New lines aren't printable ASCII to my knowledge. – CzarMatt – 2017-08-04T17:05:21.520

@CzarMatt What if our source is 0 or 1 byte(s) long? Is a program like Z acceptable? – Mr. Xcoder – 2017-08-04T17:08:39.320

4@Mr.Xcoder Um, how could 4 identical programs print 4 different ASCII chars? – ETHproductions – 2017-08-04T17:09:19.810

@DigitalTrauma Darn, and here I was searching for examples with "compass" in the q/a. :) Also, I was hoping putting this in the sandbox would surface any potential dupes. – CzarMatt – 2017-08-04T17:11:15.100

@DigitalTrauma The comment rules make a big difference in the two. – Post Rock Garf Hunter – 2017-08-04T17:58:58.093

What about trailing whitespace in output, is that ok? Specifically I'm wondering about a single trailing space. – algmyr – 2017-08-04T22:22:17.480

@CzarMatt Would the score of your example be 13 or 14 bytes if the space on South can be omitted? (i.e. do we count the smallest or the largest rotation?) – Martin Ender – 2017-08-21T13:51:23.783

Answers

20

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

Jonathan Allan

Posted 2017-08-04T16:38:46.027

Reputation: 67 804

1This is the final answer. Well done. – Erik the Outgolfer – 2017-08-04T17:22:17.983

19

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.

ETHproductions

Posted 2017-08-04T16:38:46.027

Reputation: 47 880

8

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##

Rod

Posted 2017-08-04T16:38:46.027

Reputation: 17 588

Great general appraoch, now I just need to figure out how to do it! :) – flawr – 2017-08-04T19:08:41.530

You can use interface M{static void main(String[]a){System.out.println(0);}} instead to save some bytes. – Leaky Nun – 2017-08-04T19:11:44.447

Would this be work? Its way shorter – Post Rock Garf Hunter – 2017-08-04T19:58:57.910

"straightforward", he says. I'd hate to do that by hand. – Magic Octopus Urn – 2017-08-11T19:07:17.990

1@MagicOctopusUrn it wasn't done by hand c; – Rod – 2017-08-11T19:10:54.363

1@MagicOctopusUrn nah, I made this answer to show this algorithm, the language is irrelevant :3 – Rod – 2017-08-11T19:38:59.987

Ahhh... after sorting oldest you WERE the first to do this, nice. – Magic Octopus Urn – 2017-08-11T19:40:02.350

7

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!

Post Rock Garf Hunter

Posted 2017-08-04T16:38:46.027

Reputation: 55 382

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 – 2017-08-04T17:12:48.943

1This is very cool! – CzarMatt – 2017-08-04T17:14:35.350

Trying to golf you :P so far have 3 of them working – Christopher – 2017-09-10T23:43:28.473

6

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!

Nitrodon

Posted 2017-08-04T16:38:46.027

Reputation: 9 181

5

05AB1E, 5 3 bytes

Y'X

North, East, South, West

Riley

Posted 2017-08-04T16:38:46.027

Reputation: 11 345

Impressive, that was quick! – CzarMatt – 2017-08-04T16:44:29.803

@MagicOctopusUrn I think Y'X will work, but I have to try it still. – Riley – 2017-08-11T18:24:43.100

@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 – 2017-08-11T18:29:53.057

@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 – 2017-08-11T18:40:13.107

10 would've worked if they allowed multiple characters for an output ;P.* – Magic Octopus Urn – 2017-08-11T19:12:29.233

@MagicOctopusUrn That was my first thought too. – Riley – 2017-08-11T19:15:09.357

Brute-Forcer v0.3 – Magic Octopus Urn – 2017-08-11T19:26:43.063

5

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

algmyr

Posted 2017-08-04T16:38:46.027

Reputation: 858

4

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 ;)

Felix Palmen

Posted 2017-08-04T16:38:46.027

Reputation: 3 866

Do you need any of the four slashes on the right edge right above the gap? – ETHproductions – 2017-08-04T23:39:54.780

Hey .. uhm ... I guess, actually ... no. Good catch, thanks :) – Felix Palmen – 2017-08-04T23:46:27.653

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 – 2017-08-05T00:14:20.907

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 – 2017-08-05T00:17:19.377

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 – 2017-08-05T00:21:39.290

@ETHproductions I fixed/improved it using your very nice idea (don't know why I didn't see it as I did experiment with line breaks ...) If you want to post it yourself, that would be fair and I'll delete it. – Felix Palmen – 2017-08-05T00:34:09.993

It's your answer, I'm just helping you golf it ;-) You can shave off a few more bytes by making the actual code a 3x6 rectangle: Try it online!

– ETHproductions – 2017-08-05T01:03:48.180

Just a nitpick but it looks like you've got two "W"s – boboquack – 2017-08-05T03:38:19.917

4

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.

Martin Ender

Posted 2017-08-04T16:38:46.027

Reputation: 184 808

4

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.

Martin Ender

Posted 2017-08-04T16:38:46.027

Reputation: 184 808

Super cool, great diagrams, too. – CzarMatt – 2018-02-18T17:51:09.290

3

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!

AdmBorkBork

Posted 2017-08-04T16:38:46.027

Reputation: 41 581

Don't know powershell, but wouldn't this work?

– Post Rock Garf Hunter – 2017-08-04T19:34:50.787

@WheatWizard Yes, indeed. Thanks! – AdmBorkBork – 2017-08-04T19:37:00.537

3

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

Not a tree

Posted 2017-08-04T16:38:46.027

Reputation: 3 106

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 – 2017-08-06T01:45:35.837

@CzarMatt, thanks for the clarification! I've updated the post. – Not a tree – 2017-08-06T01:54:27.013

3

dc, 9 bytes

 1
2p3
 4

Try it online!

I think its obviousness is part of the charm.

Sophia Lechner

Posted 2017-08-04T16:38:46.027

Reputation: 1 200

2

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.

Neil

Posted 2017-08-04T16:38:46.027

Reputation: 95 035

2

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.

Tom Carpenter

Posted 2017-08-04T16:38:46.027

Reputation: 3 990

Come to think of it, this would work in MATL as well. Same byte count as mine, though. – Sanchises – 2017-08-05T19:59:15.553

2

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.

Jo King

Posted 2017-08-04T16:38:46.027

Reputation: 38 234

1

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!

dzaima

Posted 2017-08-04T16:38:46.027

Reputation: 19 048

If it contains a space, it is 5 bytes. – Mr. Xcoder – 2017-08-04T17:14:11.420

1@Mr.Xcoder OP in comments said that you don't need to pad it – dzaima – 2017-08-04T17:15:09.917

1

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>

kamoroso94

Posted 2017-08-04T16:38:46.027

Reputation: 739

1

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)

gfdfgdds

Posted 2017-08-04T16:38:46.027

Reputation: 11

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ý – 2017-08-05T17:51:32.280

(Just add REPL after JS, then I think it's fine) – Zacharý – 2017-08-05T18:19:06.633

1Output should be a single printable ASCII character, so like my MATLAB one, this is invalid. – Tom Carpenter – 2017-08-05T19:27:22.633

1

Sorry, I meant:

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

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

gdsgsgsgsgsgsgdsg

Posted 2017-08-04T16:38:46.027

Reputation: 11

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ý – 2017-08-05T17:53:28.807

@Zacharý I think https://codegolf.meta.stackexchange.com/questions/7842/when-is-code-that-requires-a-repl-acceptable says that REPLs are allowed

– SuperStormer – 2017-08-05T18:03:38.383

Either way, they should specify that it is a JS REPL. – Zacharý – 2017-08-05T18:18:49.310

2Output should be a single printable ASCII character, so like my MATLAB one, this is invalid. – Tom Carpenter – 2017-08-05T19:26:03.050

1

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.

Sanchises

Posted 2017-08-04T16:38:46.027

Reputation: 8 530

1

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!

Dom Hastings

Posted 2017-08-04T16:38:46.027

Reputation: 16 415

1

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

gastropner

Posted 2017-08-04T16:38:46.027

Reputation: 3 264