DEV Community

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

Collapse
 
celyes profile image
Ilyes Chouia • Edited

this is my solution in PHP, although the description is quite ambiguous...

$friends = [["A1", "X1"], ["A2", "X2"], ["A3", "X3"], ["A4", "X4"]];
$distances = [["X1", 100], ["X2", 200], ["X3", 350], ["X4", 420]];

$distances = array_reduce($distances, function($x, $y){
    $x[$y[0]] = $y[1]; 
    return $x;
});

function getTotalDistance($friends, $distances){
    $total = 0;
    for($x = 0; $x < count($friends); $x++){
        if(count($friends[$x]) === 2){
            $total += ($distances[$friends[$x][1]] * 2);
        }
    }
    return $total;
}
echo getTotalDistance($friends, $distances);