@ 2023-05-31T04:28:53Z <?php
class Person
{
public readonly string $lastName;
public readonly string $firstName;
private readonly int $strength;
private readonly int $agility;
private readonly int $intelligence;
private readonly int $charisma;
private readonly int $luck;
/**
* @param non-empty-string|null $lastName
*/
public function __construct(?string $lastName = null)
{
$this->lastName = $lastName ?? $this->randomName();
$this->firstName = $firstName ?? $this->randomName($this->lastName[0]);
$this->strength = $this->randomFeatureValue();
$this->agility = $this->randomFeatureValue();
$this->intelligence = $this->randomFeatureValue();
$this->charisma = $this->randomFeatureValue();
$this->luck = $this->randomFeatureValue();
}
public function __clone(): void
{
$this->firstName = $this->randomName($this->lastName[0]);
$this->strength = $this->evolveFeatureValue($this->strength);
$this->agility = $this->evolveFeatureValue($this->agility);
$this->intelligence = $this->evolveFeatureValue($this->intelligence);
$this->charisma = $this->evolveFeatureValue($this->charisma);
$this->luck = $this->evolveFeatureValue($this->luck);
}
public function getName(): string
{
return $this->lastName . ' ' . $this->firstName;
}
public function getValue(): int
{
return $this->strength + $this->agility + $this->intelligence + $this->charisma + $this->luck;
}
public function getDetails(): string
{
return $this->getName() . ' ' . $this->getValue() . '(' . $this->strength . '+' . $this->agility . '+' . $this->intelligence . '+' . $this->charisma . '+' . $this->luck . ')';
}
/**
* @return int
* @throws Exception
*/
private function randomFeatureValue(): int
{
return random_int(0, 9);
}
private function evolveFeatureValue(int $value): int
{
return min(max($value + random_int(-1, 1), 0), 9);
}
public function randomName(?string $notStartingWith = null): string
{
$possibleNames = ['Alice', 'Bob', 'Charlie', 'Dave', 'Eve', 'Frank', 'Grace', 'Heidi', 'Ivan', 'Judy', 'Kevin', 'Linda', 'Mallory', 'Niaj', 'Olivia', 'Peggy', 'Quentin', 'Rupert', 'Sybil', 'Trent', 'Ursula', 'Victor', 'Walter', 'Xavier', 'Yvonne', 'Zoe'];
do {
$randomName = $possibleNames[array_rand($possibleNames)];
} while ($notStartingWith !== null && $randomName[0] === $notStartingWith);
return $randomName;
}
}
class Life
{
/** @var Person[] */
private array $population = [];
public function run(int $iterations)
{
while ($iterations-- > 0) {
$this->runIteration();
}
}
public function runIteration()
{
$this->new();
$this->reproduce();
}
public function geStatisticsOnMostValuable()
{
$topMostValuablePersons = $this->getTopMostValuablePersons(10);
$statistics = array_map(
fn(Person $person) => $person->getDetails(),
$topMostValuablePersons,
);
return implode("\n", $statistics);
}
private function new(): void
{
foreach (range(1, 3) as $_) {
$this->addPerson(new Person());
}
}
private function reproduce(): void
{
$topMostValuablePerson = $this->getTopMostValuablePersons(5);
// usual evolution
foreach (range(1, 3) as $_) {
$randomValuablePerson = $topMostValuablePerson[array_rand($topMostValuablePerson)];
$this->addPerson(clone $randomValuablePerson);
}
// tracked evolution
$randomValuablePerson = $topMostValuablePerson[array_rand($topMostValuablePerson)];
$this->addPerson(clone $randomValuablePerson with (lastName: '0000'));
}
private function addPerson(Person $person)
{
/** add a person, eventually killing one that already has the same name */
$this->population[$person->getName()] = $person;
}
/**
* @param int $int
* @return Person[]
*/
private function getTopMostValuablePersons(int $int): array
{
$population = $this->population;
uasort($population, function (Person $person1, Person $person2) {
return $person2->getValue() <=> $person1->getValue();
});
return array_slice($population, 0, $int);
}
}
$life = new Life();
$life->run(100);
echo $life->geStatisticsOnMostValuable();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.2.6 0.003 0.006 17.50 8.2.5 0.018 0.000 17.50 8.2.4 0.017 0.000 17.25 8.2.3 0.011 0.005 17.38 8.2.2 0.017 0.000 17.21 8.2.1 0.014 0.003 17.35 8.2.0 0.017 0.000 17.00 8.1.19 0.010 0.000 17.00 8.1.18 0.008 0.008 16.86 8.1.17 0.015 0.002 16.88 8.1.16 0.010 0.008 17.00 8.1.15 0.006 0.012 16.99 8.1.14 0.013 0.006 17.13 8.1.13 0.017 0.000 17.13 8.1.12 0.010 0.007 17.34 8.1.11 0.008 0.008 17.13 8.1.10 0.013 0.003 17.00 8.1.9 0.011 0.005 16.98 8.1.8 0.013 0.004 16.98 8.1.7 0.016 0.002 17.22 8.1.6 0.017 0.003 17.13 8.1.5 0.009 0.009 16.98 8.1.4 0.006 0.012 17.00 8.1.3 0.017 0.000 17.13 8.1.2 0.013 0.004 17.25 8.1.1 0.009 0.009 17.25 8.1.0 0.022 0.000 17.13 8.0.28 0.012 0.004 16.52 8.0.27 0.010 0.006 16.52 8.0.26 0.013 0.003 16.75 8.0.25 0.015 0.000 16.52 8.0.24 0.007 0.007 16.52 8.0.23 0.014 0.002 16.63 8.0.22 0.010 0.005 16.52 8.0.21 0.012 0.004 16.63 8.0.20 0.016 0.000 16.75 8.0.19 0.011 0.006 16.61 8.0.18 0.013 0.002 16.63 8.0.17 0.014 0.003 16.59 8.0.16 0.011 0.008 16.52 8.0.15 0.011 0.006 16.63 8.0.14 0.014 0.003 16.52 8.0.13 0.016 0.000 16.52 8.0.12 0.006 0.009 16.75 8.0.11 0.012 0.003 16.52 8.0.10 0.008 0.008 16.63 8.0.9 0.010 0.005 16.52 8.0.8 0.016 0.000 16.52 8.0.7 0.013 0.004 16.52 8.0.6 0.011 0.005 16.80 8.0.5 0.008 0.008 16.52 8.0.3 0.010 0.006 16.63 8.0.2 0.011 0.004 16.52 8.0.1 0.015 0.000 16.52
preferences:dark mode live preview ace vim emacs key bindings
138.01 ms | 993 KiB | 7 Q