DEV Community

Discussion on: Daily Challenge #6 - Grandma and her friends

Collapse
 
stevemoon profile image
Steve Moon • Edited

Erlang:
note: I followed the link to the codewars page and am using their list of distances. I also assume Granny is coming home after visiting her friends. A different list of distances can be passed in, without limit on length. But if the correlation between friend1 is in town1 breaks down then I'd need to add a map/hashtable for that lookup.

-module(devto6).
-export([find_distance/1]).

find_distance([B, C | Rest]) ->
    TotalDist = B,
    A = math:sqrt(C * C - B * B),
    find_distance(TotalDist + A, C, Rest).

find_distance(TotalDist, B, [C | Rest]) ->
    A = math:sqrt(C * C - B * B),
    find_distance(TotalDist + A, C, Rest);
find_distance(TotalDist, C, []) ->
    TotalDist + C.

devto6:find_distance([100, 200, 250, 300]).
889.0363202746577