1
\$\begingroup\$

Jill lives in the (magnetic) north pole. One day, Jill decided to go for a walk, travelling in the four directions (north, east, west, south) for some lengths, with the help of a compass. Your task is to find whether Jill ended up where Jill lives, i.e. the magnetic north pole.

Only south is defined at the north pole. Walking north or south changes the latitude, while walking east or west changes the longitude. The longitude is not defined at the north pole. As a result, Jill would have ended up at the north pole if and only if the distances Jill walked to the south sum up to be equal to the sum of the distances Jill walked to the north, and the distance Jill walked east and west do not matter.

Input

A list of pairs of (direction, amount). The input shall be valid. The amounts will be integers. Acceptable formats include:

  • "N200"
  • "200N"
  • 200i (complex number)
  • (0, 200) (0123 for NESW)
  • (200,0) (the above reversed)

Output

Two consistent values, one for the case that Jill did end up in the magnetic north pole, and one for the case that Jill did not.

Testcases

Truthy inputs:

S100, E50, N50, W50, N50
S10, E1000, S30, W400, S10, W10, N50
S30, N30
S10, E50, E50, N10

Falsey inputs:

S100
S10, E314, N5
S300, W135, S10, E35, N290

Scoring

This is . Shortest answer in bytes wins. Standard loopholes apply.

\$\endgroup\$
  • 1
    \$\begingroup\$ I have a feeling that whomever can get away with the worst abuse of the input format will win this one... \$\endgroup\$ – Dennis May 5 '17 at 2:32
  • 3
    \$\begingroup\$ @Dennis I have a feeling that regardless of the input format, you will win this one... \$\endgroup\$ – Leaky Nun May 5 '17 at 2:34
  • 3
    \$\begingroup\$ Why is Jill a he? \$\endgroup\$ – Stephen May 5 '17 at 2:45
  • 2
    \$\begingroup\$ @Dennis - If my suspicions are correct, the intent might be to include polar coordinates. So actually the only criteria to check is that distanceSouth - distanceNorth = 0 \$\endgroup\$ – user15259 May 5 '17 at 2:47
  • 2
    \$\begingroup\$ @LeakyNun I'd rather not try to figure out the spec from the test cases, but from what I gather, is this equivalent to the sums of Souths and Norths being 0, with the Easts and Wests ignored? \$\endgroup\$ – xnor May 5 '17 at 2:54
5
\$\begingroup\$

Jelly, 3 bytes

SĊṆ

Input is in form of complex numbers, which will be real or purely imaginary.

Try it online!

How it works

SĊṆ  Main link. Argument: A (array of directions)

S    Take the sum of A.
 Ċ   Get the imaginary part of the sum.
  Ṇ  Take the logical NOT of the imaginary part.
\$\endgroup\$
  • \$\begingroup\$ I never knew Jelly has a complex part built-in \$\endgroup\$ – Leaky Nun May 5 '17 at 3:54
  • 2
    \$\begingroup\$ Yes, it's one of the very few overloaded operators. , Ċ, and (floor, ceil, and sign) don't make sense for complex numbers, so I recycled them for real, imag, and conjugate. \$\endgroup\$ – Dennis May 5 '17 at 3:56
  • \$\begingroup\$ What are you going to do with < etc? \$\endgroup\$ – Leaky Nun May 5 '17 at 3:57
  • \$\begingroup\$ Not really sure yet. \$\endgroup\$ – Dennis May 5 '17 at 4:03
  • \$\begingroup\$ @Dennis Why didn't you overload more operators? \$\endgroup\$ – Esolanging Fruit May 5 '17 at 4:19
3
\$\begingroup\$

JavaScript (ES6), 36 bytes

Takes input as an array of [orientation, distance] arrays, with 0123 = NESW. Returns a boolean.

a=>!a.reduce((p,[o,d])=>p+--o%2*d,0)

The result of the modulo in JS has the same sign as the dividend:

  • (0 - 1) % 2 == -1 (north)
  • (2 - 1) % 2 == 1 (south)

.map() alternative, 36 bytes

a=>a.map(([o,d])=>a=~~a+--o%2*d)&&!a

Test cases

let f =

a=>!a.reduce((p,[o,d])=>p+--o%2*d,0)

// truthy
console.log(f([[2,100], [1,50], [0,50], [3,50], [0,50]]))
console.log(f([[2,10], [1,1000], [2,30], [3,400], [2,10], [0,50]]))
console.log(f([[2,30], [0,30]]))
console.log(f([[2,10], [1,50], [1,50], [0,10]]))

// falsy
console.log(f([[2,100]]))
console.log(f([[2,10], [1,314], [0,5]]))
console.log(f([[2,300], [3,135], [2,10], [1,35], [0,290]]))

\$\endgroup\$
  • \$\begingroup\$ +1 for reduce \$\endgroup\$ – Neil May 5 '17 at 9:09
  • \$\begingroup\$ Nice; blows the reduce solution I was working on out of the water. +1 \$\endgroup\$ – Shaggy May 5 '17 at 9:14
1
\$\begingroup\$

Batch, 78 bytes

@set l=0
@for %%s in (%*)do @set t=%%s&call set/al+=%%t:i=*0%%
@cmd/cset/a!l

Takes input as command-line arguments in complex format (e.g. -100 50i 50 -50i 50). Outputs 1 if Jill arrives back at the pole, 0 if not.

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

Pyth, 3 bytes

!es

Input as a list of complex numbers.

Try it online!

!es
  s  # Sum
 e   # Imaginary part
!    # Not
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 10 bytes

1>Im@Tr@#&

Pure function taking a list of complex numbers (same format, and implementation, as Dennis's Jelly answer). 1> saves a byte over 0==, and is valid thanks to the facts that distances will always be integers and you can't go north from the north pole.

\$\endgroup\$
0
\$\begingroup\$

Java 7, 135 bytes

boolean c(String[]a){int n=0,s=0,c,i;for(String x:a){c=x.charAt(0);i=new Short(x.substring(1));n+=c==78?i:0;s+=c==83?i:0;}return n==s;}

Those restricted input-format are pretty annoying, but I guess that was the main part of the challenge..

Explanation:

boolean c(String[]a){             // Method with String-array parameter and boolean return-type
  int n=0,                        //  North-sum
      s=0,                        //  South-sum
      c,i;                        //  Two temp integers to save bytes
  for(String x:a){                //  Loop over the input-array
    c=x.charAt(0);                //   Set `c` to the first character
    i=new Short(x.substring(1));  //   Set `i` to the actual number
    n+=c==78?i:0;                 //   If `c` == 'N': increase the North-sum with `i`
    s+=c==83?i:0;                 //   If `c` == 'S': increase the South-sum with `i`
  }                               //  End of loop
  return n==s;                    //  Return if the North- and South-sums are equal
}                                 // End of method

Test code:

Try it here.

class M{
  static boolean c(String[]a){int n=0,s=0,c,i;for(String x:a){c=x.charAt(0);i=new Short(x.substring(1));n+=c==78?i:0;s+=c==83?i:0;}return n==s;}

  public static void main(String[] a){
    System.out.println(c(new String[]{ "S100", "E50", "N50", "W50", "N50" }));
    System.out.println(c(new String[]{ "S10", "E1000", "S30", "W400", "S10", "W10", "N50" }));
    System.out.println(c(new String[]{ "S30", "N30" }));
    System.out.println(c(new String[]{ "S10", "E50", "E50", "N10" }));
    System.out.println(c(new String[]{ "S100" }));
    System.out.println(c(new String[]{ "S10", "E314", "N5" }));
    System.out.println(c(new String[]{ "S300", "W135", "S10", "E35", "N290" }));
  }
}

Output:

true
true
true
true
false
false
false
\$\endgroup\$
0
\$\begingroup\$

Microsoft Sql Server, 94 bytes

select sign(sum(iif(m like'[NS]%',stuff(m,1,1,iif(m like'S%','-','')),0)))+1 from a group by g

Check it.

\$\endgroup\$
0
\$\begingroup\$

Python, 23 bytes

lambda s:sum(s).imag==0

Input as a list of complex numbers.

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