15
\$\begingroup\$

Challenge

Given input in the form <n1>, <n2> where number can be -1, 0, or 1, return the corresponding cardinal direction. Positive numbers move East in the x-axis and South in the y-axis, Negative numbers move West in the x-axis and North in the y-axis.

Output must be in the form South East, North East, North. It is case-sensitive.

If the input is 0, 0, your program must return That goes nowhere, silly!.

Sample Input/Outpot:

1, 1 -> South East

0, 1 -> South

1, -1 -> North East

0, 0 -> That goes nowhere, silly!

This is , the shortest answer in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ Loosely related \$\endgroup\$ – HyperNeutrino Mar 23 '17 at 21:49
  • 1
    \$\begingroup\$ Some examples with W, NW and SW are needed. \$\endgroup\$ – seshoumara Mar 23 '17 at 22:10
  • \$\begingroup\$ @seshoumara I'm on mobile, so no backticks, but NW would be -1, -1 \$\endgroup\$ – Matias K Mar 23 '17 at 23:41
  • 1
    \$\begingroup\$ Are trailing Spaces allowed? \$\endgroup\$ – Arjun Mar 23 '17 at 23:53
  • \$\begingroup\$ Uhh... Sure, I guess. As long as it looks the same. \$\endgroup\$ – Matias K Mar 24 '17 at 0:45

20 Answers 20

12
\$\begingroup\$

Japt, 55 51 bytes

`
SÆ 
NÆ° `·gV +`
E†t
Wƒt`·gU ª`T•t goƒ Í2€e, Ðéy!

Explanation

                      // Implicit: U, V = inputs
`\nSÆ \nNÆ° `       // Take the string "\nSouth \nNorth ".
·                     // Split it at newlines, giving ["", "South ", "North "].
gV                    // Get the item at index V. -1 corresponds to the last item.
+                     // Concatenate this with
`\nE†t\nWƒt`·gU       // the item at index U in ["", "East", "West"].
ª`T•t goƒ Í2€e, Ðéy!  // If the result is empty, instead take "That goes nowhere, silly!".
                      // Implicit: output result of last expression

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Um... I... ??? How on earth does this work? Does Japt have like some fancy things that replace common character pairs? \$\endgroup\$ – HyperNeutrino Mar 23 '17 at 21:44
  • \$\begingroup\$ @HyperNeutrino Yes, Japt uses the shoco compression library which replaces common pairs of lowercase characters with a single byte. \$\endgroup\$ – ETHproductions Mar 23 '17 at 21:46
  • \$\begingroup\$ Okay, that's really cool! I'll look into that, see if I can make any use out of it. \$\endgroup\$ – HyperNeutrino Mar 23 '17 at 21:47
9
\$\begingroup\$

Python, 101 87 bytes

Really naive solution.

lambda x,y:['','South ','North '][y]+['','West','East'][x]or'That goes nowhere, silly!'

Thanks to @Lynn for saving 14 bytes! Changes: Using the string.split method actually makes it longer ;_; And also, negative indexes exist in python.

\$\endgroup\$
  • 5
    \$\begingroup\$ You can cut it down to 87 like this: lambda x,y:('','South ','North ')[y]+('','East','West')[x]or'That goes nowhere, silly!' \$\endgroup\$ – Lynn Mar 24 '17 at 0:35
  • 2
    \$\begingroup\$ I found a neat way to get some directions, but unfortunately it doesn't seem like it will work for this challenge. Figured I'd share it anyways (perhaps someone craftier than I can figure out how to deal with its problems, like when x or y = 0): lambda x,y:'North htuoS'[::x][:6]+'EastseW'[::y][:4] Edit: it likely will now be too long, but you can make the second slicing [:6*x**2], likewise for the East/West string, if you can circumvent the error on the first slicing. \$\endgroup\$ – cole Mar 24 '17 at 6:48
  • \$\begingroup\$ @Lynn lambda x,y:('North ','South ')[y+1]+('West','East')[x+1]or'That goes nowhere, silly!' is shorter by 2 bytes \$\endgroup\$ – Dead Possum Mar 24 '17 at 9:22
  • \$\begingroup\$ @Lynn Oh, thanks! (I forgot about negative indexes!) \$\endgroup\$ – HyperNeutrino Mar 24 '17 at 12:15
  • \$\begingroup\$ @DeadPossum That won't work because it will return South East for (0, 0). Thank you though! \$\endgroup\$ – HyperNeutrino Mar 24 '17 at 12:17
6
\$\begingroup\$

PHP, 101 Bytes

[,$b,$a]=$argv;echo$a|$b?[North,"",South][1+$a]." ".[West,"",East][1+$b]:"That goes nowhere, silly!";
\$\endgroup\$
  • \$\begingroup\$ It's a long time since I programmed in PHP, but how does it know North, South, West and East are strings without double-quotes around them? Is this because of the empty String that shares the same array? If yes, does this also mean you cannot have an array with different types at once (like an array with both a string and an integer)? \$\endgroup\$ – Kevin Cruijssen Mar 24 '17 at 12:32
  • 1
    \$\begingroup\$ @KevinCruijssen North is a constant php.net/manual/en/language.constants.php If the constant not exists it will be interpreted as string. An array in PHP can contain different types. strings can be specified in four ways php.net/manual/en/language.types.string.php \$\endgroup\$ – Jörg Hülsermann Mar 24 '17 at 12:56
6
\$\begingroup\$

Perl 6, 79 bytes

{<<'' East South North West>>[$^y*2%5,$^x%5].trim||'That goes nowhere, silly!'}

Try it

Expanded:

{ # bare block lambda with placeholder parameters 「$x」 and 「$y」

  << '' East South North West >>\ # list of 5 strings
  [                               # index into that with:

    # use a calculation so that the results only match on 0
    $^y * 2 % 5, # (-1,0,1) => (3,0,2) # second parameter
    $^x % 5      # (-1,0,1) => (4,0,1) # first parameter

  ]
  .trim  # turn that list into a space separated string implicitly
         # and remove leading and trailing whitespace

  ||     # if that string is empty, use this instead
  'That goes nowhere, silly!'
}
\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6), 106 100 97 93 bytes

It's a very simple approach. It consists of a few ternary operators nested together -

f=a=>b=>a|b?(a?a>0?"South ":"North ":"")+(b?b>0?"East":"West":""):"That goes nowhere, silly!"

Test Cases

f=a=>b=>a|b?(a?a>0?"South ":"North ":"")+(b?b>0?"East":"West":""):"That goes nowhere, silly!"

console.log(f(1729)(1458));
console.log(f(1729)(-1458));
console.log(f(-1729)(1458));
console.log(f(-1729)(-1458));
console.log(f(0)(1729));
console.log(f(0)(-1729));
console.log(f(1729)(0));
console.log(f(-1729)(0));

\$\endgroup\$
  • \$\begingroup\$ a!=0 can be replaced by just a, since 0 is falsy and all other values are truthy. Also, taking input in currying syntax is shorter, and the array appoach is also shorter. \$\endgroup\$ – Luke Mar 24 '17 at 6:27
  • \$\begingroup\$ @Luke Thanks for the suggestion! I have edited the answer. Now, I am beating the PHP and Python solutions! All because of you!!! Thanks! \$\endgroup\$ – Arjun Mar 24 '17 at 6:30
  • \$\begingroup\$ Save another byte by doing f=a=>b=> and calling the function like f(1729)(1458); which is the currying syntax that @Luke mentioned. \$\endgroup\$ – Tom Mar 24 '17 at 7:35
  • \$\begingroup\$ You can safely use a|b instead of a||b. Assuming that the input only consists of -1, 0 or 1 (which is unclear to me), you could replace a>0 and b>0 with ~a and ~b. \$\endgroup\$ – Arnauld Mar 24 '17 at 7:42
  • \$\begingroup\$ Also, you don't need these parentheses: a?(...):"" / b?(...):"" \$\endgroup\$ – Arnauld Mar 24 '17 at 7:48
4
\$\begingroup\$

Batch, 156 bytes

@set s=
@for %%w in (North.%2 South.-%2 West.%1 East.-%1)do @if %%~xw==.-1 call set s=%%s%% %%~nw
@if "%s%"=="" set s= That goes nowhere, silly!
@echo%s%

The for loop acts as a lookup table to filter when the (possibly negated) parameter equals -1, and concatenating the matching words. If nothing is selected then the silly message is printed instead.

\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 86 bytes

a=>b=>["North ","","South "][b+1]+["West","","East"][a+1]||"That goes nowhere, silly!"

Explanation

Call it with currying syntax (f(a)(b)). This uses array indices. If both a and b are 0, the result is a falsy empty string. In that case, the string after the || is returned.

Try it

Try all test cases here:

let f=
a=>b=>["North ","","South "][b+1]+["West","","East"][a+1]||"That goes nowhere, silly!"

for (let i = -1; i < 2; i++) {
    for (let j = -1; j < 2; j++) {
        console.log(`${i}, ${j}: ${f(i)(j)}`);
    }
}

\$\endgroup\$
3
\$\begingroup\$

GNU sed, 100 + 1(r flag) = 101 bytes

s:^-1:We:
s:^1:Ea:
s:-1:Nor:
s:1:Sou:
s:(.*),(.*):\2th \1st:
s:0...?::
/0/cThat goes nowhere, silly!

By design, sed executes the script as many times as there are input lines, so one can do all the test cases in one run, if needed. The TIO link below does just that.

Try it online!

Explanation:

s:^-1:We:                         # replace '-1' (n1) to 'We'
s:^1:Ea:                          # replace '1' (n1) to 'Ea'
s:-1:Nor:                         # replace '-1' (n2) to 'Nor'
s:1:Sou:                          # replace '1' (n2) to 'Sou'
s:(.*),(.*):\2th \1st:            # swap the two fields, add corresponding suffixes
s:0...?::                         # delete first field found that starts with '0'
/0/cThat goes nowhere, silly!     # if another field is found starting with '0',
                                  #print that text, delete pattern, end cycle now

The remaining pattern space at the end of a cycle is printed implicitly.

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

05AB1E, 48 45 43 bytes

õ'†Ô'…´)èUõ„ƒÞ „„¡ )èXJ™Dg_i“§µ—±æÙ,Ú¿!“'Tì

Try it online!

Explanation

õ'†Ô'…´)                                       # push the list ['','east','west']
        èU                                     # index into this with first input
                                               # and store the result in X
          õ„ƒÞ „„¡ )                           # push the list ['','south ','north ']
                    èXJ                        # index into this with 2nd input
                                               # and join with the content of X
                       ™                       # convert to title-case
                        Dg_i                   # if the length is 0
                            “§µ—±æÙ,Ú¿!“       # push the string "hat goes nowhere, silly!"
                                        'Tì    # prepend "T"
\$\endgroup\$
2
\$\begingroup\$

Jelly, 40 bytes

Ṛị"“¡ƘƓ“¡+9“»,“wµ“¡ḳ]“»Kt⁶ȯ“¬ɼ¬<¬O÷ƝḤẎ6»

Try it online!

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

Japt, 56 bytes

N¬¥0?`T•t goƒ Í2€e, Ðéy!`:` SÆ NÆ°`¸gV +S+` E†t Wƒt`¸gU

Try it online! | Test Suite

Explanation:

N¬¥0?`Tt go Í2e, Ðéy!`:` SÆ NÆ°`¸gV +S+` Et Wt`¸gU
Implicit U = First input
         V = Second input

N´0?`...`:` ...`qS gV +S+` ...`qS gU
N¬                                                     Join the input (0,0 → "00")
  ¥0                                                   check if input is roughly equal to 0. In JS, "00" == 0
    ?                                                  If yes:
      ...                                               Output "That goes nowhere, silly!". This is a compressed string
     `   `                                              Backticks are used to decompress strings
          :                                            Else:
           ` ...`                                       " South North" compressed
                 qS                                     Split on " " (" South North" → ["","South","North"])
                   gV                                   Return the string at index V
                     +S+                                +" "+ 
                        ` ...`                          " East West" compressed
                              qS gU                     Split on spaces and yield string at index U
\$\endgroup\$
  • \$\begingroup\$ Hint: 00 is exactly the same as 0, as the extra digit gets removed ;) \$\endgroup\$ – ETHproductions Mar 24 '17 at 11:17
  • 1
    \$\begingroup\$ The second-best solution yet no upvote. I upvote for you. \$\endgroup\$ – Arjun Mar 25 '17 at 15:22
1
\$\begingroup\$

Retina, 84 82 81 bytes

1 byte saved thanks to @seshoumara for suggesting 0...? instead of 0\w* ?

(.+) (.+)
$2th $1st
^-1
Nor
^1
Sou
-1
We
1
Ea
0...?

^$
That goes nowhere, silly!

Try it online!

\$\endgroup\$
  • \$\begingroup\$ The output is wrong. OP wants positive numbers to move S in the y-axis and negative numbers to move N. \$\endgroup\$ – seshoumara Mar 24 '17 at 10:08
  • \$\begingroup\$ @seshoumara Right, fixed it for same bytecount (just had to swap Nor and Sou) \$\endgroup\$ – Cows quack Mar 24 '17 at 10:14
  • \$\begingroup\$ Ok. Also, you can shave 1 byte by using 0...?. \$\endgroup\$ – seshoumara Mar 24 '17 at 10:18
  • \$\begingroup\$ @seshoumara Thanks for the tip :) \$\endgroup\$ – Cows quack Mar 24 '17 at 10:21
1
\$\begingroup\$

Swift 151 bytes

func d(x:Int,y:Int){x==0&&y==0 ? print("That goes nowhere, silly!") : print((y<0 ? "North " : y>0 ? "South " : "")+(x<0 ? "West" : x>0 ? "East" : ""))}
\$\endgroup\$
1
\$\begingroup\$

PHP, 95 bytes.

This simply displays the element of the array, and if there's nothing, just displays the "default" message.

echo['North ','','South '][$argv[1]+1].[East,'',West][$argv[2]+1]?:'That goes nowhere, silly!';

This is meant to run with the -r flag, receiving the coordenates as the 1st and 2nd arguments.

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

C#, 95 102 bytes


Golfed

(a,b)=>(a|b)==0?"That goes nowhere, silly!":(b<0?"North ":b>0?"South ":"")+(a<0?"West":a>0?"East":"");

Ungolfed

( a, b ) => ( a | b ) == 0
    ? "That goes nowhere, silly!"
    : ( b < 0 ? "North " : b > 0 ? "South " : "" ) +
      ( a < 0 ? "West" : a > 0 ? "East" : "" );

Ungolfed readable

// A bitwise OR is perfomed
( a, b ) => ( a | b ) == 0

    // If the result is 0, then the 0,0 text is returned
    ? "That goes nowhere, silly!"

    // Otherwise, checks against 'a' and 'b' to decide the cardinal direction.
    : ( b < 0 ? "North " : b > 0 ? "South " : "" ) +
      ( a < 0 ? "West" : a > 0 ? "East" : "" );

Full code

using System;

namespace Namespace {
    class Program {
        static void Main( string[] args ) {
            Func<Int32, Int32, String> f = ( a, b ) =>
                ( a | b ) == 0
                    ? "That goes nowhere, silly!"
                    : ( b < 0 ? "North " : b > 0 ? "South " : "" ) +
                      ( a < 0 ? "West" : a > 0 ? "East" : "" );

            for( Int32 a = -1; a <= 1; a++ ) {
                for( Int32 b = -1; b <= 1; b++ ) {
                    Console.WriteLine( $"{a}, {b} = {f( a, b )}" );
                }
            }

            Console.ReadLine();
        }
    }
}

Releases

  • v1.1 - + 7 bytes - Wrapped snippet into a function.
  • v1.0 -  95 bytes - Initial solution.

Notes

I'm a ghost, boo!

\$\endgroup\$
  • 1
    \$\begingroup\$ That's a code snippet you need to wrap it in a function i.e. add the (a,b)=>{...} bit \$\endgroup\$ – TheLethalCoder Mar 24 '17 at 16:23
  • \$\begingroup\$ You can use currying to save a byte a=>b=>, might not need the () around the a|b, you might be able to use interpolated strings to get the string built up nicer as well \$\endgroup\$ – TheLethalCoder Mar 24 '17 at 16:26
  • \$\begingroup\$ Completely forgot to wrap into a function :S. For the () around the a|b, I do need it, otherwise Operator '|' cannot be applied to operands of type 'int' and 'bool'. I've also tried the interpolated strings, but didn't give much though due to the "" giving me errors. \$\endgroup\$ – auhmaan Mar 24 '17 at 16:35
1
\$\begingroup\$

Scala, 107 bytes

a=>b=>if((a|b)==0)"That goes nowhere, silly!"else Seq("North ","","South ")(b+1)+Seq("West","","East")(a+1)

Try it online

To use this, declare this as a function and call it:

val f:(Int=>Int=>String)=...
println(f(0)(0))

How it works

a =>                                // create an lambda with a parameter a that returns
  b =>                              // a lambda with a parameter b
    if ( (a | b) == 0)                // if a and b are both 0
      "That goes nowhere, silly!"       // return this string
    else                              // else return
      Seq("North ","","South ")(b+1)    // index into this sequence
      +                                 // concat
      Seq("West","","East")(a+1)        // index into this sequence
\$\endgroup\$
1
\$\begingroup\$

C, 103 bytes

f(a,b){printf("%s%s",b?b+1?"South ":"North ":"",a?a+1?"East":"West":b?"":"That goes nowhere, silly!");}
\$\endgroup\$
0
\$\begingroup\$

Java 7, 130 bytes

String c(int x,int y){return x==0&y==0?"That goes nowhere, silly!":"North xxSouth ".split("x")[y+1]+"WestxxEast".split("x")[x+1];}

Explanation:

String c(int x, int y){               // Method with x and y integer parameters and String return-type
  return x==0&y==0 ?                  //  If both x and y are 0:
     "That goes nowhere, silly!"      //   Return "That goes nowhere, silly!"
    :                                 //  Else:
     "North xxSouth ".split("x"[y+1]  //   Get index y+1 from array ["North ","","South "] (0-indexed)
     + "WestxxEast".split("x")[x+1];  //   Plus index x+1 from array ["West","","East"] (0-indexed)
}                                     // End of method

Test code:

Try it here.

class M{
  static String c(int x,int y){return x==0&y==0?"That goes nowhere, silly!":"North xxSouth ".split("x")[y+1]+"WestxxEast".split("x")[x+1];}

  public static void main(String[] a){
    System.out.println(c(1, 1));
    System.out.println(c(0, 1));
    System.out.println(c(1, -1));
    System.out.println(c(0, 0));
  }
}

Output:

South East
South 
North East
That goes nowhere, silly!
\$\endgroup\$
0
\$\begingroup\$

CJam, 68 bytes

"
South 
North 

East
West"N/3/l~W%.=s_Q"That goes nowhere, silly!"?

Try it online! or verify all test cases

Prints one trailing space on [0 -1] or [0 1] (North or South).

Explanation

"\nSouth \nNorth \n\nEast\nWest"  e# Push this string
N/                                e# Split it by newlines
3/                                e# Split the result into 3-length subarrays,
                                  e#  gives [["" "South " "North "]["" "East" "West"]]
l~                                e# Read and eval a line of input
W%                                e# Reverse the co-ordinates
.=                                e# Vectorized get-element-at-index: accesses the element
                                  e#  from the first array at the index given by the 
                                  e#  y co-ordinate. Arrays are modular, so -1 is the last
                                  e#  element. Does the same with x on the other array.
s                                 e# Cast to string (joins the array with no separator)
_                                 e# Duplicate the string
Q"That goes nowhere, silly!"?     e# If it's non-empty, push an empty string. If its empty, 
                                  e#  push "That goes nowhere, silly!"
\$\endgroup\$
0
\$\begingroup\$

Röda, 100 bytes

f a,b{["That goes nowhere, silly!"]if[a=b,a=0]else[["","South ","North "][b],["","East","West"][a]]}

Try it online!

This is a trivial solution, similar to some other answers.

\$\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.