68 lines
1.4 KiB
PHP
68 lines
1.4 KiB
PHP
<?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>";
|
|
?>
|