# Chasing the car [closed]

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.

• And yes, I would highly recommend you start posting your challenge ideas to the Sandbox first to get feedback Jul 5 at 9:33
• Is the angle in radians also ok?
– G B
Jul 5 at 9:36
• Where is the angle measured from? From north, like a bearing? From east, like in most maths? Can we choose? Jul 5 at 9:41
• @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 Jul 5 at 9:43
• Can we take the input as single complex numbers, e.g. [40+0i, 0+40i, -30+0i, 0-30i]?
Jul 5 at 10:19

# 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
• Since "just 2 digits after decimal is fine", you can probably get away with changing Math::PI to just 3.142 or similar Jul 5 at 9:47

# 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

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

• Oh nice ATO in action Jul 5 at 9:42
• cmath has polar which gives (abs(v), phase(v)), though you'd have to find a way to convert the angle to degrees
– xnor
Jul 5 at 10:05
• @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) Jul 5 at 10:31
• @pxeger For the python 2, it looks like you can save a few bytes with a program taking input.
– xnor
Jul 5 at 10:45

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

• Imagine having a functional atan... Jul 5 at 9:43
• @AUsername feels nice to have mathematical functions direct in programming Jul 5 at 9:45
• Sadly mine'll be non-competing :p Jul 5 at 9:46