$value){ list($orb, $obj) = explode(')', $value); // echo $orb.")".$obj."
"; $objects[$orb][] = $obj; $flippedObjects[$obj][] = $orb; } // echo "
"; //PART 1 $orbits = 0; foreach($inputArray as $key => $value){ list($orb, $obj) = explode(')', $value); $orbits++; $newOrb = $orb; while($newOrb != "COM"){ if(isset($objects[$newOrb])){ $newOrb = $flippedObjects[$newOrb][0]; $orbits++; } } } //PART 2 $newOrbP2 = $flippedObjects["YOU"][0]; $orbits2 = 0; while($newOrbP2 != "COM"){ if(isset($objects[$newOrbP2])){ $path1[] = $newOrbP2; $newOrbP2 = $flippedObjects[$newOrbP2][0]; $orbits2++; } } $newOrbP2 = $flippedObjects["SAN"][0]; $orbits3 = 0; while($newOrbP2 != "COM"){ if(isset($objects[$newOrbP2])){ if($temp = array_search($newOrbP2, $path1)){ // echo "Meeting: ".$newOrbP2; break; } $newOrbP2 = $flippedObjects[$newOrbP2][0]; $orbits3++; } } $jumps = $orbits3; foreach($path1 as $orb){ if($orb == $newOrbP2){ break; } $jumps++; } echo "

Number of orbits: $orbits

"; echo "

Jumps from YOU to SAN: $jumps

"; die(); echo "
";
print_r($path1);
echo "
"; echo "
";
print_r($flippedObjects);
echo "
"; ?>