Completed day 6

master
Eirik Svagård 2019-12-23 04:00:56 +01:00
parent 7dc1e65291
commit 198aeba7b4
2 changed files with 65 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
.gitignore
*.txt
header.php
.ht*
*.code-workspace

View File

@ -1,5 +1,68 @@
<?php
$input = "COM)B\nB)C\nC)D\nD)E\nE)F\nB)G\nG)H\nD)I\nE)J\nJ)K\nK)L\nK)YOU\nI)SAN";
$input = file_get_contents("day6.txt");
$inputArray = explode("\n", $input);
foreach($inputArray as $key => $value){
list($orb, $obj) = explode(')', $value);
// echo $orb.")".$obj."<br>";
$objects[$orb][] = $obj;
$flippedObjects[$obj][] = $orb;
}
// echo "<br>";
//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 "<h1>Number of orbits: $orbits</h1>";
echo "<h1>Jumps from YOU to SAN: $jumps</h1>";
die();
echo "<br/><pre style='float:left;'>";
print_r($path1);
echo "</pre>";
echo "<pre style='float:left;'>";
print_r($flippedObjects);
echo "</pre>";
?>