✨ Add day 12 part 1
This commit is contained in:
66
day_12/functions.php
Normal file
66
day_12/functions.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @param Moon[] $moons
|
||||
*/
|
||||
function calculateVelocity(array $moons)
|
||||
{
|
||||
foreach ($moons as $i => $base_moon) {
|
||||
foreach ($moons as $j => $target_moon) {
|
||||
if ($i === $j) {
|
||||
// same planet
|
||||
continue;
|
||||
}
|
||||
|
||||
$velocity_x = $base_moon->getVelocity()->getX();
|
||||
$velocity_y = $base_moon->getVelocity()->getY();
|
||||
$velocity_z = $base_moon->getVelocity()->getZ();
|
||||
if ($base_moon->getX() > $target_moon->getX()) {
|
||||
--$velocity_x;
|
||||
} elseif ($base_moon->getX() < $target_moon->getX()) {
|
||||
++$velocity_x;
|
||||
}
|
||||
if ($base_moon->getY() > $target_moon->getY()) {
|
||||
--$velocity_y;
|
||||
} elseif ($base_moon->getY() < $target_moon->getY()) {
|
||||
++$velocity_y;
|
||||
}
|
||||
if ($base_moon->getZ() > $target_moon->getZ()) {
|
||||
--$velocity_z;
|
||||
} elseif ($base_moon->getZ() < $target_moon->getZ()) {
|
||||
++$velocity_z;
|
||||
}
|
||||
$base_moon->getVelocity()->setX($velocity_x)->setY($velocity_y)->setZ($velocity_z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Moon[] $moons
|
||||
*/
|
||||
function updateCoordinates(array $moons)
|
||||
{
|
||||
foreach ($moons as $moon) {
|
||||
$moon->setX($moon->getX() + $moon->getVelocity()->getX());
|
||||
$moon->setY($moon->getY() + $moon->getVelocity()->getY());
|
||||
$moon->setZ($moon->getZ() + $moon->getVelocity()->getZ());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Moon[] $moons
|
||||
* @return float|int
|
||||
*/
|
||||
function calculateEnergy(array $moons)
|
||||
{
|
||||
$energy = 0;
|
||||
foreach ($moons as $moon) {
|
||||
$pot = abs($moon->getX()) + abs($moon->getY()) + abs($moon->getZ());
|
||||
$velocity = $moon->getVelocity();
|
||||
$kin = abs($velocity->getX()) + abs($velocity->getY()) + abs($velocity->getZ());
|
||||
$energy += $pot * $kin;
|
||||
}
|
||||
|
||||
return $energy;
|
||||
}
|
||||
Reference in New Issue
Block a user