From 1ac5b20e1edcaf6173f3b6ed1cef7a8e0a6beb2f Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Thu, 12 Dec 2019 14:09:52 +0100 Subject: [PATCH] :sparkles: Add day 12 part 1 --- day_12/Moon.php | 139 +++++++++++++++++++++++++++++++++++++++++++ day_12/Velocity.php | 75 +++++++++++++++++++++++ day_12/functions.php | 66 ++++++++++++++++++++ day_12/part_1.php | 21 +++++++ 4 files changed, 301 insertions(+) create mode 100644 day_12/Moon.php create mode 100644 day_12/Velocity.php create mode 100644 day_12/functions.php create mode 100644 day_12/part_1.php diff --git a/day_12/Moon.php b/day_12/Moon.php new file mode 100644 index 0000000..20b35f0 --- /dev/null +++ b/day_12/Moon.php @@ -0,0 +1,139 @@ +x = $x; + $this->y = $y; + $this->z = $z; + $this->addHistory(sprintf('%s,%s,%s,0,0,0', $this->getX(), $this->getY(), $this->getZ())); + $this->velocity = new Velocity(0,0,0); + } + + /** + * @return int + */ + public function getX(): int + { + return $this->x; + } + + /** + * @param int $x + * @return Moon + */ + public function setX(int $x): Moon + { + $this->x = $x; + return $this; + } + + /** + * @return int + */ + public function getY(): int + { + return $this->y; + } + + /** + * @param int $y + * @return Moon + */ + public function setY(int $y): Moon + { + $this->y = $y; + return $this; + } + + /** + * @return int + */ + public function getZ(): int + { + return $this->z; + } + + /** + * @param int $z + * @return Moon + */ + public function setZ(int $z): Moon + { + $this->z = $z; + return $this; + } + + /** + * @return Velocity + */ + public function getVelocity(): Velocity + { + return $this->velocity; + } + + /** + * @param Velocity $velocity + * @return Moon + */ + public function setVelocity(Velocity $velocity): Moon + { + $this->velocity = $velocity; + return $this; + } + + /** + * @return string[] + */ + public function getHistory(): array + { + return $this->history; + } + + /** + * @param string $history + * @return Moon + */ + public function addHistory($history): Moon + { + $this->history[] = $history; + return $this; + } + + public function saveHistory() + { + $velocity = $this->getVelocity(); + $this->history[] = sprintf( + '%s,%s,%s,%s,%s,%s', + $this->getX(), + $this->getY(), + $this->getZ(), + $velocity->getX(), + $velocity->getY(), + $velocity->getZ() + ); + return $this; + } +} diff --git a/day_12/Velocity.php b/day_12/Velocity.php new file mode 100644 index 0000000..f1a0b32 --- /dev/null +++ b/day_12/Velocity.php @@ -0,0 +1,75 @@ +x = $x; + $this->y = $y; + $this->z = $z; + } + + /** + * @return mixed + */ + public function getX() + { + return $this->x; + } + + /** + * @param mixed $x + * @return Velocity + */ + public function setX($x) + { + $this->x = $x; + return $this; + } + + /** + * @return mixed + */ + public function getY() + { + return $this->y; + } + + /** + * @param mixed $y + * @return Velocity + */ + public function setY($y) + { + $this->y = $y; + return $this; + } + + /** + * @return mixed + */ + public function getZ() + { + return $this->z; + } + + /** + * @param mixed $z + * @return Velocity + */ + public function setZ($z) + { + $this->z = $z; + return $this; + } +} diff --git a/day_12/functions.php b/day_12/functions.php new file mode 100644 index 0000000..82346a5 --- /dev/null +++ b/day_12/functions.php @@ -0,0 +1,66 @@ + $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; +} \ No newline at end of file diff --git a/day_12/part_1.php b/day_12/part_1.php new file mode 100644 index 0000000..8f87093 --- /dev/null +++ b/day_12/part_1.php @@ -0,0 +1,21 @@ +