From 198aeba7b431c17828798b8ce8aadbed9d47b580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eirik=20Svag=C3=A5rd?= Date: Mon, 23 Dec 2019 04:00:56 +0100 Subject: [PATCH] Completed day 6 --- .gitignore | 2 ++ php/day6.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/.gitignore b/.gitignore index e604cc3..fa4cfa9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.gitignore *.txt header.php .ht* +*.code-workspace \ No newline at end of file diff --git a/php/day6.php b/php/day6.php index ba2e1f6..e1a9572 100644 --- a/php/day6.php +++ b/php/day6.php @@ -1,5 +1,68 @@ $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 "
"; ?> \ No newline at end of file