diff --git a/build-container.php b/build-container.php new file mode 100755 index 0000000..2bf4617 --- /dev/null +++ b/build-container.php @@ -0,0 +1,53 @@ +#!/usr/bin/env php + __DIR__ . '/config/cache/inputParsers.php', + Solution::class => __DIR__ . '/config/cache/solutions.php', +]; + +foreach ($map as $type => $target) { + is_dir($dir = dirname($target)) || mkdir($dir, recursive: true); + $implementations = Collection::fromIterable($implementationsDiscovery->findImplementations($type)) + ->sort( + callback: static fn (ReflectionClass $alpha, ReflectionClass $bravo) => $alpha->getName() <=> $bravo->getName(), + ); + + $uses = $implementations + ->map(static fn (ReflectionClass $class) => sprintf('use %s;', $class->getName())) + ->implode("\n"); + + $contents = $implementations + ->map(static fn (ReflectionClass $class) => sprintf(' %s::class,', $class->getShortName())) + ->implode("\n"); + file_put_contents( + $target, + sprintf( + <<classes ->filter(static fn (ReflectionClass $ref) => $ref->implementsInterface($interface)) - ->map(static fn (ReflectionClass $ref) => $ref->newInstance()) ->all(); } diff --git a/src/Aoc/SolutionFactory.php b/src/Aoc/SolutionFactory.php index 9ad7c45..1092d3c 100644 --- a/src/Aoc/SolutionFactory.php +++ b/src/Aoc/SolutionFactory.php @@ -5,6 +5,7 @@ namespace App\Aoc; use App\Aoc\Discovery\ImplementationsDiscovery; +use loophp\collection\Collection; use RuntimeException; final readonly class SolutionFactory @@ -15,12 +16,11 @@ public function __construct(private ImplementationsDiscovery $implementations) public function make(Challenge $challenge): Solution { - $supports = static fn (Solution $solution) => collect($solution->challenges())->contains( - static fn (Challenge $supports) => $supports->equals($challenge), - ); + $supports = static fn (Solution $solution) => null !== Collection::fromIterable($solution->challenges()) + ->find(callbacks: static fn (Challenge $supports) => $supports->equals($challenge)); return $this->getSolutions() - ->first(static fn (Solution $solution) => $supports($solution)) + ->find(callbacks: static fn (Solution $solution) => $supports($solution)) ?? throw new RuntimeException(sprintf('No solution for %s', $challenge)); } @@ -35,8 +35,8 @@ public function mostRecentChallenge(): Challenge ?? throw new RuntimeException('No implementations yet'); } - public function getSolutions(): \Illuminate\Support\Collection + public function getSolutions(): Collection { - return collect($this->implementations->findImplementations(Solution::class)); + return Collection::fromIterable($this->implementations->findImplementations(Solution::class)); } }