adventofcode/src/ExecuteDay.php

73 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace trizz\AdventOfCode;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
2022-11-29 19:52:02 +01:00
final class ExecuteDay extends Command
{
2022-11-29 19:52:02 +01:00
private int $day;
private int $year;
2021-12-09 22:25:59 +00:00
private string $title;
protected function configure(): void
{
$this
->setName('day')
->setDescription('Run day')
->addArgument('day', InputArgument::REQUIRED, 'The day number')
->addArgument('year', InputArgument::OPTIONAL, 'The year', date('y'));
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->day = $input->getArgument('day');
$this->year = $input->getArgument('year');
$this->title = sprintf("Advent of Code '%d - Day %d", $this->year, $this->day);
$output->writeln('');
$output->writeln($this->title);
$output->writeln(str_repeat('-', strlen($this->title)));
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
2021-12-09 21:45:08 +00:00
$className = sprintf('%s\\Y%d\\Day%d', __NAMESPACE__, $this->year, $this->day);
2022-11-29 19:52:02 +01:00
/** @var Solution $class */
$class = new $className();
$class->loadData();
// Solve the examples if available.
$resultPart1Example = 'n/a';
$resultPart2Example = 'n/a';
if ($class->hasExampleData()) {
['part1' => $resultPart1Example, 'part2' => $resultPart2Example] = $class->results(useExampleData: true);
}
// Solve the real puzzle if available.
$resultPart1 = 'n/a';
$resultPart2 = 'n/a';
if ($class->hasData()) {
['part1' => $resultPart1, 'part2' => $resultPart2] = $class->results(useExampleData: false);
}
// Output all the results.
$output->writeln('<fg=bright-green>Part 1</>');
$output->writeln(sprintf('<fg=blue>Example:</> <comment>%s</comment>', $resultPart1Example));
$output->writeln(sprintf('<fg=blue>Result: </> <comment>%s</comment>', $resultPart1));
$output->writeln(str_repeat('-', strlen($this->title)));
$output->writeln('<fg=bright-green>Part 2</>');
$output->writeln(sprintf('<fg=blue>Example:</> <comment>%s</comment>', $resultPart2Example));
$output->writeln(sprintf('<fg=blue>Result: </> <comment>%s</comment>', $resultPart2));
return Command::SUCCESS;
}
}