Refactor framework to support more years

This commit is contained in:
Tristan 2021-12-09 22:41:22 +01:00
parent 8f6de7b627
commit 55e4e62d97
Signed by: trizz
GPG Key ID: 0A93DEC67165EB47
6 changed files with 37 additions and 23 deletions

View File

@ -9,14 +9,15 @@ use Symfony\Component\Console\Output\OutputInterface;
class ExecuteDay extends Command class ExecuteDay extends Command
{ {
protected int $day;
protected int $year;
/** /**
* @var string The title. * @var string The title.
*/ */
private string $title; private string $title;
protected int $day;
protected int $year;
/** /**
* Configure the command. * Configure the command.
*/ */

View File

@ -2,11 +2,11 @@
namespace trizz\AdventOfCode; namespace trizz\AdventOfCode;
use trizz\AdventOfCode\Utils\SymfonyConsoleMarkdown;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use trizz\AdventOfCode\Utils\SymfonyConsoleMarkdown;
class Puzzle extends Command class Puzzle extends Command
{ {
@ -14,7 +14,8 @@ class Puzzle extends Command
{ {
$this $this
->setName('puzzle') ->setName('puzzle')
->addArgument('day', InputArgument::REQUIRED, 'The day number.'); ->addArgument('day', InputArgument::REQUIRED, 'The day number.')
->addArgument('year', InputArgument::OPTIONAL, 'The year', date('y'));
} }
/** /**
@ -22,7 +23,14 @@ class Puzzle extends Command
*/ */
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$contents = file_get_contents(sprintf('%s/../data/day%s/puzzle.md', __DIR__, (int) $input->getArgument('day'))); $contents = file_get_contents(
sprintf(
'%s/../data/Y%d/day%s/puzzle.md',
__DIR__,
$input->getArgument('year'),
(int) $input->getArgument('day')
)
);
$rendered = (new SymfonyConsoleMarkdown())->render($contents); $rendered = (new SymfonyConsoleMarkdown())->render($contents);
$output->writeln($rendered); $output->writeln($rendered);

View File

@ -3,6 +3,9 @@
namespace trizz\AdventOfCode; namespace trizz\AdventOfCode;
use JetBrains\PhpStorm\ArrayShape; use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
abstract class Solution abstract class Solution
{ {
@ -90,7 +93,7 @@ abstract class Solution
return !empty($this->exampleData); return !empty($this->exampleData);
} }
#[ArrayShape(['part1' => 'int|string', 'part2' => 'int|string'])] #[ArrayShape(['part1' => "int|string", 'part2' => "int|string"])]
public function results(bool $useExampleData = true): array public function results(bool $useExampleData = true): array
{ {
$data = $useExampleData ? $this->exampleData : $this->data; $data = $useExampleData ? $this->exampleData : $this->data;

View File

@ -4,4 +4,5 @@ namespace AdventOfCode21\Utils;
class Arr class Arr
{ {
} }

View File

@ -12,22 +12,6 @@ class Day4 extends Solution
public static int|string|null $part2ExampleResult = 1924; public static int|string|null $part2ExampleResult = 1924;
public static int|string|null $part2Result = 17435; public static int|string|null $part2Result = 17435;
/**
* {@inheritdoc}
*/
public function part1(array $data): int|string
{
return $this->playBingo($data, firstWins: true);
}
/**
* {@inheritdoc}
*/
public function part2(array $data): int|string
{
return $this->playBingo($data, firstWins: false);
}
/** /**
* @param int[] $winningCard * @param int[] $winningCard
* @param int $number * @param int $number
@ -46,6 +30,22 @@ class Day4 extends Solution
return (int) array_sum($unusedNumbers) * $number; return (int) array_sum($unusedNumbers) * $number;
} }
/**
* {@inheritdoc}
*/
public function part1(array $data): int|string
{
return $this->playBingo($data, firstWins: true);
}
/**
* {@inheritdoc}
*/
public function part2(array $data): int|string
{
return $this->playBingo($data, firstWins: false);
}
/** /**
* @param string $numberList * @param string $numberList
* @param string $separator * @param string $separator

View File

@ -2,6 +2,7 @@
namespace trizz\AdventOfCode\Y21; namespace trizz\AdventOfCode\Y21;
use trizz\AdventOfCode\Solution;
use JetBrains\PhpStorm\Immutable; use JetBrains\PhpStorm\Immutable;
use trizz\AdventOfCode\Solution; use trizz\AdventOfCode\Solution;