1
\$\begingroup\$

Imagine you are sitting at a park. Suddenly your car gets hijacked and being driven in different directions. Here are the directions, arrows show which direction goes towards where:

     South
       |
       V
East->   <-West
       ^
       | 
     North

After being driven, the car stops. Now you have to find out how far the car is away from you and angle of you and your car in degrees. It will be given how many units have been driven and in which direction. Input/Output format is flexible, you can take a 2D list/1D list/string etc and return the displacement and angle.

Test Cases

[[40,East],[40,North],[30,West],[30,South]] -> 14.1421356237 45.0
[[45,North],[200,East]] -> 205.0 75.9637565321

No need to output exact angle and distance, just 2 digits after decimal is fine, but more than 2 is fine too.

Only given directions are North, east, south, west.

The value of movement in a direction is always positive integer.

Trailing Whitespace allowed. Code golf, so shortest code wins.

My English is bad, sorry. Please ask for clarification if you have trouble understand ing

\$\endgroup\$
11
  • 3
    \$\begingroup\$ And yes, I would highly recommend you start posting your challenge ideas to the Sandbox first to get feedback \$\endgroup\$
    – pxeger
    Jul 5 at 9:33
  • 3
    \$\begingroup\$ Is the angle in radians also ok? \$\endgroup\$
    – G B
    Jul 5 at 9:36
  • 4
    \$\begingroup\$ Where is the angle measured from? From north, like a bearing? From east, like in most maths? Can we choose? \$\endgroup\$
    – pxeger
    Jul 5 at 9:41
  • 2
    \$\begingroup\$ @BadCoder requiring an output format (degrees) like that is really not a good idea and strongly discouraged on this site. It adds significant byte overhead on boring I/O boilerplate instead of solving the challenge \$\endgroup\$
    – pxeger
    Jul 5 at 9:43
  • 6
    \$\begingroup\$ Can we take the input as single complex numbers, e.g. [40+0i, 0+40i, -30+0i, 0-30i]? \$\endgroup\$
    – Adám
    Jul 5 at 10:19
3
\$\begingroup\$

Ruby, 44 bytes

->l{z=l.sum{|a,b|a*b};[z.abs,z.arg*57.2958]}

Try it online!

Input direction as:

  • 1i East
  • 1 North
  • -1i West
  • -1 South
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Since "just 2 digits after decimal is fine", you can probably get away with changing Math::PI to just 3.142 or similar \$\endgroup\$
    – pxeger
    Jul 5 at 9:47
3
\$\begingroup\$

APL (Dyalog Unicode), 18 bytes

Anonymous tacit infix function, taking a list of complex units as one argument and the magnitudes as another argument.

(|,{180÷○÷12○⍵})+.×

Try it online!

+.× dot product, i.e. sum of products of directions and magnitudes

() apply the following tacit function to that:

{} apply the following lambda to that; is the argument:

  12○⍵ the phase (i.e. angle)

  ÷ reciprocal of that

  π times that

  180÷ 180 divided by that

|, prepend the absolute value


If we were allowed to answer in radians, the solution would be just:

10 12○+.×

Try it online!

+.× dot product, i.e. sum of products of directions and magnitudes

10 12○ magnitude and phase of that

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

Python 3, 77 bytes

lambda a:(abs(v:=(sum(x*d for x,d in a))),phase(v)*180/pi)
from cmath import*

Attempt This Online!

Takes the direction as a complex number: 1, -1, 1j, or -1j for east, west, north, and south respectively.


Python 2, 74 bytes

Thanks to @xnor

from cmath import*
m,t=polar(sum(x*d for x,d in input()))
print m,t*180/pi

Attempt This Online!

Full program taking input as a list from STDIN with directions as above.

\$\endgroup\$
4
  • \$\begingroup\$ Oh nice ATO in action \$\endgroup\$
    – wasif
    Jul 5 at 9:42
  • \$\begingroup\$ cmath has polar which gives (abs(v), phase(v)), though you'd have to find a way to convert the angle to degrees \$\endgroup\$
    – xnor
    Jul 5 at 10:05
  • \$\begingroup\$ @xnor check back in the revision history: I used that until OP mentioned that degrees were required, but I couldn't find a shorter way of doing it with polar (although there's this in Python 2 for =77) \$\endgroup\$
    – pxeger
    Jul 5 at 10:31
  • \$\begingroup\$ @pxeger For the python 2, it looks like you can save a few bytes with a program taking input. \$\endgroup\$
    – xnor
    Jul 5 at 10:45
2
\$\begingroup\$

Python 2, 115 bytes

x=y=0;from math import*
for u,d in input():exec('x+=u y+=u x-=u y-=u'.split()[d])
print hypot(x,y),atan(x/y)*180/pi

Try it online!

Straightforward approach. Full program. Takes input as a 2D list, 0 for East, 1 for North, 2 for West, 3 for South.

\$\endgroup\$
3
  • \$\begingroup\$ Imagine having a functional atan... \$\endgroup\$
    – emanresu A
    Jul 5 at 9:43
  • \$\begingroup\$ @AUsername feels nice to have mathematical functions direct in programming \$\endgroup\$
    – wasif
    Jul 5 at 9:45
  • \$\begingroup\$ Sadly mine'll be non-competing :p \$\endgroup\$
    – emanresu A
    Jul 5 at 9:46

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