Add day 12 part 1

This commit is contained in:
Clement Desmidt 2019-12-12 14:09:52 +01:00
parent 1273f4388b
commit 1ac5b20e1e
4 changed files with 301 additions and 0 deletions

139
day_12/Moon.php Normal file
View File

@ -0,0 +1,139 @@
<?php
class Moon
{
/** @var int */
protected $x;
/** @var int */
protected $y;
/** @var int */
protected $z;
/** @var string[] */
protected $history = [];
/**
* @var Velocity
*/
protected $velocity;
/**
* Moon constructor.
* @param int $x
* @param int $y
* @param int $z
*/
public function __construct(int $x, int $y, int $z)
{
$this->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;
}
}

75
day_12/Velocity.php Normal file
View File

@ -0,0 +1,75 @@
<?php
class Velocity
{
protected $x;
protected $y;
protected $z;
/**
* Velocity constructor.
* @param $x
* @param $y
* @param $z
*/
public function __construct($x, $y, $z)
{
$this->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;
}
}

66
day_12/functions.php Normal file
View 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;
}

21
day_12/part_1.php Normal file
View File

@ -0,0 +1,21 @@
<?php
include 'Moon.php';
include 'Velocity.php';
include 'functions.php';
$moons = array_map(function ($moon) {
$moon_coordinates = explode(', ', substr($moon, 1, -1));
$x = (int) substr($moon_coordinates[0], 2);
$y = (int) substr($moon_coordinates[1], 2);
$z = (int) substr($moon_coordinates[2], 2);
return new Moon($x, $y, $z);
}, explode("\n", file_get_contents('input.txt'))
);
for ($i = 0; $i < 1000; $i++) {
calculateVelocity($moons);
updateCoordinates($moons);
}
var_dump(calculateEnergy($moons));