Apply rector, phpstan and php-cs-fixer
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -7,19 +7,14 @@ use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class ExecuteDay extends Command
|
||||
final class ExecuteDay extends Command
|
||||
{
|
||||
protected int $day;
|
||||
private int $day;
|
||||
|
||||
private int $year;
|
||||
|
||||
protected int $year;
|
||||
/**
|
||||
* @var string The title.
|
||||
*/
|
||||
private string $title;
|
||||
|
||||
/**
|
||||
* Configure the command.
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
@ -29,10 +24,6 @@ class ExecuteDay extends Command
|
||||
->addArgument('year', InputArgument::OPTIONAL, 'The year', date('y'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the command after the input has been bound and before the input
|
||||
* is validated.
|
||||
*/
|
||||
protected function initialize(InputInterface $input, OutputInterface $output): void
|
||||
{
|
||||
$this->day = $input->getArgument('day');
|
||||
@ -45,14 +36,10 @@ class ExecuteDay extends Command
|
||||
$output->writeln(str_repeat('-', strlen($this->title)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the current command.
|
||||
*
|
||||
* @return int 0 if everything went fine, or an exit code
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$className = sprintf('%s\\Y%d\\Day%d', __NAMESPACE__, $this->year, $this->day);
|
||||
|
||||
/** @var Solution $class */
|
||||
$class = new $className();
|
||||
$class->loadData();
|
||||
|
@ -2,25 +2,23 @@
|
||||
|
||||
namespace trizz\AdventOfCode;
|
||||
|
||||
use PhpPkg\CliMarkdown\CliMarkdown;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use trizz\AdventOfCode\Utils\SymfonyConsoleMarkdown;
|
||||
|
||||
class Puzzle extends Command
|
||||
final class Puzzle extends Command
|
||||
{
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setName('puzzle')
|
||||
->setDescription('Show the puzzle description.')
|
||||
->addArgument('day', InputArgument::REQUIRED, 'The day number.')
|
||||
->addArgument('year', InputArgument::OPTIONAL, 'The year', date('y'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$contents = file_get_contents(
|
||||
@ -31,7 +29,14 @@ class Puzzle extends Command
|
||||
(int) $input->getArgument('day')
|
||||
)
|
||||
);
|
||||
$rendered = (new SymfonyConsoleMarkdown())->render($contents);
|
||||
|
||||
if (!$contents) {
|
||||
$output->writeln('Can not read puzzle.');
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$rendered = (new CliMarkdown())->render($contents);
|
||||
|
||||
$output->writeln($rendered);
|
||||
|
||||
|
@ -16,12 +16,14 @@ abstract class Solution
|
||||
|
||||
/**
|
||||
* @var string[] The data to use.
|
||||
*
|
||||
* @psalm-suppress PropertyNotSetInConstructor
|
||||
*/
|
||||
public ?array $data = null;
|
||||
|
||||
/**
|
||||
* @var string[] The example data.
|
||||
*
|
||||
* @psalm-suppress PropertyNotSetInConstructor
|
||||
*/
|
||||
public ?array $exampleData = null;
|
||||
@ -50,7 +52,7 @@ abstract class Solution
|
||||
return 'n/a';
|
||||
}
|
||||
|
||||
public function loadData()
|
||||
public function loadData(): void
|
||||
{
|
||||
$dataFile = sprintf('%s/../data/Y%d/day%d/data.txt', __DIR__, $this->year(), $this->day());
|
||||
$dataExampleFile = sprintf('%s/../data/Y%d/day%d/example.txt', __DIR__, $this->year(), $this->day());
|
||||
@ -80,16 +82,19 @@ abstract class Solution
|
||||
return (int) substr(explode('\\', static::class)[3], 3);
|
||||
}
|
||||
|
||||
public function hasData()
|
||||
public function hasData(): bool
|
||||
{
|
||||
return !empty($this->data);
|
||||
}
|
||||
|
||||
public function hasExampleData()
|
||||
public function hasExampleData(): bool
|
||||
{
|
||||
return !empty($this->exampleData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{part1: int|string, part2: int|string}
|
||||
*/
|
||||
#[ArrayShape(['part1' => 'int|string', 'part2' => 'int|string'])]
|
||||
public function results(bool $useExampleData = true): array
|
||||
{
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace trizz\AdventOfCode\Utils;
|
||||
|
||||
class Arr
|
||||
final class Arr
|
||||
{
|
||||
/**
|
||||
* Flatten a multi-dimensional array into a single level.
|
||||
@ -11,12 +11,11 @@ class Arr
|
||||
*
|
||||
* @see https://github.com/laravel/framework/blob/c16367a1af68d8f3a1addc1a819f9864334e2c66/src/Illuminate/Collections/Arr.php#L221-L249
|
||||
*
|
||||
* @param iterable $array
|
||||
* @param float|int $depth
|
||||
* @param array<mixed> $array
|
||||
*
|
||||
* @return array
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public static function flatten(iterable $array, float|int $depth = INF)
|
||||
public static function flatten(iterable $array, float|int $depth = INF): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace trizz\AdventOfCode\Utils;
|
||||
|
||||
class Str
|
||||
final class Str
|
||||
{
|
||||
/**
|
||||
* Check if the entirety of string two matches string one.
|
||||
|
@ -4,12 +4,14 @@ namespace trizz\AdventOfCode\Y21;
|
||||
|
||||
use trizz\AdventOfCode\Solution;
|
||||
|
||||
class Day1 extends Solution
|
||||
final class Day1 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 7;
|
||||
|
||||
public static int|string|null $part1Result = 1688;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 5;
|
||||
|
||||
public static int|string|null $part2Result = 1728;
|
||||
|
||||
/**
|
||||
|
@ -4,13 +4,15 @@ namespace trizz\AdventOfCode\Y21;
|
||||
|
||||
use trizz\AdventOfCode\Solution;
|
||||
|
||||
class Day2 extends Solution
|
||||
final class Day2 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 150;
|
||||
public static int|string|null $part1Result = 1654760;
|
||||
|
||||
public static int|string|null $part1Result = 1_654_760;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 900;
|
||||
public static int|string|null $part2Result = 1956047400;
|
||||
|
||||
public static int|string|null $part2Result = 1_956_047_400;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -4,10 +4,11 @@ namespace trizz\AdventOfCode\Y21;
|
||||
|
||||
use trizz\AdventOfCode\Solution;
|
||||
|
||||
class Day3 extends Solution
|
||||
final class Day3 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 198;
|
||||
public static int|string|null $part1Result = 3309596;
|
||||
|
||||
public static int|string|null $part1Result = 3_309_596;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@ -27,9 +28,9 @@ class Day3 extends Solution
|
||||
$gammaRate = '';
|
||||
$epsilonRate = '';
|
||||
|
||||
foreach ($bits as $values) {
|
||||
$zeros = array_filter($values, static fn ($value) => $value === '0');
|
||||
$ones = array_filter($values, static fn ($value) => $value === '1');
|
||||
foreach ($bits as $bit) {
|
||||
$zeros = array_filter($bit, static fn ($value) => $value === '0');
|
||||
$ones = array_filter($bit, static fn ($value) => $value === '1');
|
||||
|
||||
$gammaRate .= ($ones > $zeros) ? '1' : '0';
|
||||
$epsilonRate .= ($ones < $zeros) ? '1' : '0';
|
||||
|
@ -4,12 +4,14 @@ namespace trizz\AdventOfCode\Y21;
|
||||
|
||||
use trizz\AdventOfCode\Solution;
|
||||
|
||||
class Day4 extends Solution
|
||||
final class Day4 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 4512;
|
||||
|
||||
public static int|string|null $part1Result = 60368;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 1924;
|
||||
|
||||
public static int|string|null $part2Result = 17435;
|
||||
|
||||
/**
|
||||
@ -30,15 +32,13 @@ class Day4 extends Solution
|
||||
|
||||
/**
|
||||
* @param int[] $winningCard
|
||||
* @param int $number
|
||||
* @psalm-param array<int, array<array-key, bool|int>|string> $winningCard
|
||||
*
|
||||
* @return int
|
||||
* @psalm-param array<int, array<array-key, bool|int>|string> $winningCard
|
||||
*/
|
||||
protected function calculateScore(array $winningCard, int $number): int
|
||||
private function calculateScore(array $winningCard, int $number): int
|
||||
{
|
||||
$return = [];
|
||||
array_walk_recursive($winningCard, static function (bool $value, int $key) use (&$return) {
|
||||
array_walk_recursive($winningCard, static function (bool $value, int $key) use (&$return): void {
|
||||
$return[$key] = $value;
|
||||
});
|
||||
$unusedNumbers = array_keys(array_filter($return, static fn (bool $value) => !$value));
|
||||
@ -47,14 +47,13 @@ class Day4 extends Solution
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $numberList
|
||||
* @param string $separator
|
||||
* @param non-empty-string $separator
|
||||
*
|
||||
* @return int[]
|
||||
*
|
||||
* @psalm-return array<int, int>
|
||||
*/
|
||||
protected function explodeNumbers(string $numberList, string $separator): array
|
||||
private function explodeNumbers(string $numberList, string $separator): array
|
||||
{
|
||||
return array_map(
|
||||
static fn ($value) => (int) $value,
|
||||
@ -67,13 +66,10 @@ class Day4 extends Solution
|
||||
|
||||
/**
|
||||
* @param string[] $data
|
||||
* @param bool $firstWins
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
protected function playBingo(array $data, bool $firstWins = true): int|string
|
||||
private function playBingo(array $data, bool $firstWins = true): int|string
|
||||
{
|
||||
$numbers = $this->explodeNumbers(array_shift($data), ',');
|
||||
$numbers = $this->explodeNumbers(array_shift($data) ?? '', ',');
|
||||
$cards = $this->setupCards($data);
|
||||
$finishedCards = [];
|
||||
|
||||
@ -129,7 +125,7 @@ class Day4 extends Solution
|
||||
*
|
||||
* @psalm-return array<int, array<int, array<false|int>|string>>
|
||||
*/
|
||||
protected function setupCards(array $data): array
|
||||
private function setupCards(array $data): array
|
||||
{
|
||||
$cards = array_chunk($data, 5);
|
||||
foreach ($cards as $card => $rows) {
|
||||
@ -148,7 +144,7 @@ class Day4 extends Solution
|
||||
*
|
||||
* @psalm-return list<int>
|
||||
*/
|
||||
protected function checkCards(array $cards, array $finishedCards): array
|
||||
private function checkCards(array $cards, array $finishedCards): array
|
||||
{
|
||||
$winningCards = [];
|
||||
// Check rows
|
||||
@ -191,7 +187,7 @@ class Day4 extends Solution
|
||||
return $winningCards;
|
||||
}
|
||||
|
||||
protected function arrayHasSingleValue(array $array, bool $value): bool
|
||||
private function arrayHasSingleValue(array $array, bool $value): bool
|
||||
{
|
||||
return count(array_unique($array)) === 1 && end($array) === $value;
|
||||
}
|
||||
|
@ -5,20 +5,22 @@ namespace trizz\AdventOfCode\Y21;
|
||||
use JetBrains\PhpStorm\Immutable;
|
||||
use trizz\AdventOfCode\Solution;
|
||||
|
||||
class Day6 extends Solution
|
||||
final class Day6 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 5934;
|
||||
|
||||
public static int|string|null $part1Result = 350917;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 26984457539;
|
||||
public static int|string|null $part2Result = 1592918715629;
|
||||
public static int|string|null $part2ExampleResult = 26_984_457_539;
|
||||
|
||||
public static int|string|null $part2Result = 1_592_918_715_629;
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
*
|
||||
* @psalm-param array{int: int}
|
||||
*/
|
||||
#[Immutable]
|
||||
protected array $startState = [
|
||||
#[Immutable] private array $startState = [
|
||||
8 => 0,
|
||||
7 => 0,
|
||||
6 => 0,
|
||||
@ -48,11 +50,10 @@ class Day6 extends Solution
|
||||
|
||||
/**
|
||||
* @param int[] $state
|
||||
* @psalm-param array{int: int} $state
|
||||
*
|
||||
* @return array
|
||||
* @psalm-param array{int: int} $state
|
||||
*/
|
||||
protected function processDay(array $state): array
|
||||
private function processDay(array $state): array
|
||||
{
|
||||
$newState = $state;
|
||||
|
||||
@ -75,11 +76,11 @@ class Day6 extends Solution
|
||||
return $newState;
|
||||
}
|
||||
|
||||
protected function processPuzzle(int $numberOfDays, string $data): int
|
||||
private function processPuzzle(int $numberOfDays, string $data): int
|
||||
{
|
||||
$state = $this->startState;
|
||||
|
||||
array_map(static function (string $stateValue) use (&$state) {
|
||||
array_map(static function (string $stateValue) use (&$state): void {
|
||||
++$state[(int) $stateValue];
|
||||
}, explode(',', $data));
|
||||
|
||||
|
@ -4,13 +4,15 @@ namespace trizz\AdventOfCode\Y21;
|
||||
|
||||
use trizz\AdventOfCode\Solution;
|
||||
|
||||
class Day7 extends Solution
|
||||
final class Day7 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 37;
|
||||
|
||||
public static int|string|null $part1Result = 344297;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 168;
|
||||
public static int|string|null $part2Result = 97164301;
|
||||
|
||||
public static int|string|null $part2Result = 97_164_301;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@ -28,15 +30,10 @@ class Day7 extends Solution
|
||||
return $this->calculateFuel($data[0], forPart2: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data
|
||||
* @param bool $forPart2
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function calculateFuel(string $data, bool $forPart2 = false): int
|
||||
private function calculateFuel(string $data, bool $forPart2 = false): int
|
||||
{
|
||||
$crabs = array_map(static fn (string $crab) => (int) $crab, explode(',', $data));
|
||||
|
||||
/** @psalm-param array{int: int} $fuelPerPosition */
|
||||
$fuelPerPosition = [];
|
||||
|
||||
|
@ -6,7 +6,7 @@ use trizz\AdventOfCode\Solution;
|
||||
use trizz\AdventOfCode\Utils\Arr;
|
||||
use trizz\AdventOfCode\Utils\Str;
|
||||
|
||||
class Day8 extends Solution
|
||||
final class Day8 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 26;
|
||||
|
||||
@ -14,7 +14,7 @@ class Day8 extends Solution
|
||||
|
||||
public static int|string|null $part2ExampleResult = 61229;
|
||||
|
||||
public static int|string|null $part2Result = 1027422;
|
||||
public static int|string|null $part2Result = 1_027_422;
|
||||
|
||||
private array $digitPatterns;
|
||||
|
||||
@ -26,7 +26,7 @@ class Day8 extends Solution
|
||||
public function part1(array $data): int
|
||||
{
|
||||
$values = array_map(
|
||||
static fn ($item) => strlen($item),
|
||||
static fn ($item) => strlen((string) $item),
|
||||
Arr::flatten(
|
||||
array_map(
|
||||
static fn ($item) => explode(' ', $item),
|
||||
@ -48,8 +48,8 @@ class Day8 extends Solution
|
||||
foreach ($data as $line) {
|
||||
$item = explode(' | ', $line);
|
||||
$sequences[] = [
|
||||
'patterns' => array_map([Str::class, 'sort'], explode(' ', $item[0])),
|
||||
'shown' => array_map([Str::class, 'sort'], explode(' ', $item[1])),
|
||||
'patterns' => array_map(Str::sort(...), explode(' ', $item[0])),
|
||||
'shown' => array_map(Str::sort(...), explode(' ', $item[1])),
|
||||
];
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user