Refactor framework to support more years

This commit is contained in:
2021-12-09 22:41:22 +01:00
parent d3cd95aed2
commit 9891975d0f
56 changed files with 1257 additions and 1084 deletions

View File

@ -1,125 +0,0 @@
<?php
namespace AdventOfCode21;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
abstract class AbstractCommand extends Command
{
/**
* @var int The day number.
*/
protected static int $day = -1;
/**
* @var string[] The data to use.
* @psalm-suppress PropertyNotSetInConstructor
*/
protected ?array $data = null;
/**
* @var string[] The example data.
* @psalm-suppress PropertyNotSetInConstructor
*/
protected ?array $exampleData = null;
/**
* @var string The title.
*/
private string $title;
/**
* Configure the command.
*/
protected function configure(): void
{
$this
->setName((string) static::$day)
->setDescription('Run day '.static::$day);
}
/**
* Initializes the command after the input has been bound and before the input
* is validated.
*/
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->title = 'Advent of Code - Day '.static::$day;
$dataFile = sprintf('%s/../data/day%d/data.txt', __DIR__, static::$day);
$dataExampleFile = sprintf('%s/../data/day%d/example.txt', __DIR__, static::$day);
if (file_exists($dataFile)) {
$this->data = array_filter(explode(PHP_EOL, file_get_contents($dataFile)));
}
if (file_exists($dataExampleFile)) {
$this->exampleData = array_filter(explode(PHP_EOL, file_get_contents($dataExampleFile)));
}
$output->writeln('');
$output->writeln($this->title);
$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
{
// Solve the examples if available.
$resultPart1Example = 'n/a';
$resultPart2Example = 'n/a';
if ($this->exampleData) {
$resultPart1Example = $this->part1($this->exampleData);
$resultPart2Example = $this->part2($this->exampleData);
}
// Solve the real puzzle if available.
$resultPart1 = 'n/a';
$resultPart2 = 'n/a';
if ($this->data) {
$resultPart1 = $this->part1($this->data);
$resultPart2 = $this->part2($this->data);
}
// 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;
}
/**
* Solve the given data for part one of the puzzle.
*
* @param string[] $data The data to process.
*
* @return int|string The result or null if not (yet?) implemented.
*/
protected function part1(array $data): int|string
{
return 'n/a';
}
/**
* Solve the given data for part one of the puzzle.
*
* @param string[] $data The data to process.
*
* @return int|string The result or null if not (yet?) implemented.
*/
protected function part2(array $data): int|string
{
return 'n/a';
}
}

View File

@ -1,66 +0,0 @@
<?php
namespace AdventOfCode21;
class Day3 extends AbstractCommand
{
/**
* {@inheritdoc}
*/
protected static int $day = 3;
/**
* {@inheritdoc}
*/
protected function part1(array $data): int
{
$bits = [];
foreach ($data as $binary) {
$split = str_split($binary);
foreach ($split as $position => $value) {
$bits[$position][] = $value;
}
}
$gammaRate = '';
$epsilonRate = '';
foreach ($bits as $values) {
$zeros = array_filter($values, static fn ($value) => $value === '0');
$ones = array_filter($values, static fn ($value) => $value === '1');
$gammaRate .= ($ones > $zeros) ? '1' : '0';
$epsilonRate .= ($ones < $zeros) ? '1' : '0';
}
return (int) (bindec($gammaRate) * bindec($epsilonRate));
}
/**
* @param string[] $data
* @param int $position
* @param bool $highest
*
* @return string
*
* @psalm-return '0'|'1'
*/
private function valueAtPosition(array $data, int $position, bool $highest = true): string
{
$zeros = count(array_filter($data, static fn ($value) => $value[$position] === '0'));
$ones = count(array_filter($data, static fn ($value) => $value[$position] === '1'));
if ($highest) {
return $zeros > $ones ? '0' : '1';
}
if ($zeros === $ones) {
return '0';
}
// If there are more zeros, return 1 (as that is the lowest).
return $zeros > $ones ? '1' : '0';
}
}

86
src/ExecuteDay.php Normal file
View File

@ -0,0 +1,86 @@
<?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;
class ExecuteDay extends Command
{
/**
* @var string The title.
*/
private string $title;
protected int $day;
protected int $year;
/**
* Configure the command.
*/
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'));
}
/**
* 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');
$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)));
}
/**
* 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();
// 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;
}
}

View File

@ -1,8 +1,8 @@
<?php
namespace AdventOfCode21;
namespace trizz\AdventOfCode;
use AdventOfCode21\Utils\SymfonyConsoleMarkdown;
use trizz\AdventOfCode\Utils\SymfonyConsoleMarkdown;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;

120
src/Solution.php Normal file
View File

@ -0,0 +1,120 @@
<?php
namespace trizz\AdventOfCode;
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
{
public static int|string|null $part1ExampleResult = null;
public static int|string|null $part1Result = null;
public static int|string|null $part2ExampleResult = null;
public static int|string|null $part2Result = null;
/**
* @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;
/**
* Solve the given data for part one of the puzzle.
*
* @param string[] $data The data to process.
*
* @return int|string The result or null if not (yet?) implemented.
*/
public function part1(array $data): int|string
{
return 'n/a';
}
/**
* Solve the given data for part one of the puzzle.
*
* @param string[] $data The data to process.
*
* @return int|string The result or null if not (yet?) implemented.
*/
public function part2(array $data): int|string
{
return 'n/a';
}
public function loadData()
{
$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());
if (file_exists($dataFile)) {
$data = file_get_contents($dataFile);
if ($data !== false) {
$this->data = array_filter(explode(PHP_EOL, $data));
}
}
if (file_exists($dataExampleFile)) {
$data = file_get_contents($dataExampleFile);
if ($data !== false) {
$this->exampleData = array_filter(explode(PHP_EOL, $data));
}
}
}
public function year(): int
{
return (int) substr(explode('\\', static::class)[2], 1);
}
public function day(): int
{
return (int) substr(explode('\\', static::class)[3], 3);
}
public function hasData()
{
return !empty($this->data);
}
public function hasExampleData()
{
return !empty($this->exampleData);
}
#[ArrayShape(['part1' => "int|string", 'part2' => "int|string"])]
public function results(bool $useExampleData = true): array
{
$data = $useExampleData ? $this->exampleData : $this->data;
return [
'part1' => $this->part1($data ?? []),
'part2' => $this->part2($data ?? []),
];
}
public function part1Data(bool $useExampleData = true): int|string
{
$data = $useExampleData ? $this->exampleData : $this->data;
return $this->part1($data ?? []);
}
public function part2Data(bool $useExampleData = true): int|string
{
$data = $useExampleData ? $this->exampleData : $this->data;
return $this->part2($data ?? []);
}
}

8
src/Utils/Arr.php Normal file
View File

@ -0,0 +1,8 @@
<?php
namespace AdventOfCode21\Utils;
class Arr
{
}

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace AdventOfCode21\Utils;
namespace trizz\AdventOfCode\Utils;
use cebe\markdown\GithubMarkdown;
use function explode;

View File

@ -1,18 +1,21 @@
<?php
namespace AdventOfCode21;
namespace trizz\AdventOfCode\Y21;
class Day1 extends AbstractCommand
use trizz\AdventOfCode\Solution;
class Day1 extends Solution
{
/**
* {@inheritdoc}
*/
protected static int $day = 1;
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;
/**
* {@inheritdoc}
*/
protected function part1(array $data): int
public function part1(array $data): int
{
$previous = null;
$increases = 0;
@ -32,7 +35,7 @@ class Day1 extends AbstractCommand
/**
* {@inheritdoc}
*/
protected function part2(array $data): int
public function part2(array $data): int
{
$previousSum = null;
$increases = 0;

View File

@ -1,18 +1,21 @@
<?php
namespace AdventOfCode21;
namespace trizz\AdventOfCode\Y21;
class Day2 extends AbstractCommand
use trizz\AdventOfCode\Solution;
class Day2 extends Solution
{
/**
* {@inheritdoc}
*/
protected static int $day = 2;
public static int|string|null $part1ExampleResult = 150;
public static int|string|null $part1Result = 1654760;
public static int|string|null $part2ExampleResult = 900;
public static int|string|null $part2Result = 1956047400;
/**
* {@inheritdoc}
*/
protected function part1(array $data): int
public function part1(array $data): int
{
$depth = 0;
$horizontal = 0;
@ -28,6 +31,7 @@ class Day2 extends AbstractCommand
'forward' => $horizontal += $distance,
'down' => $depth += $distance,
'up' => $depth -= $distance,
default => null,
};
}
@ -37,7 +41,7 @@ class Day2 extends AbstractCommand
/**
* {@inheritdoc}
*/
protected function part2(array $data): int
public function part2(array $data): int
{
$aim = 0;
$depth = 0;

40
src/Y21/Day3.php Normal file
View File

@ -0,0 +1,40 @@
<?php
namespace trizz\AdventOfCode\Y21;
use trizz\AdventOfCode\Solution;
class Day3 extends Solution
{
public static int|string|null $part1ExampleResult = 198;
public static int|string|null $part1Result = 3309596;
/**
* {@inheritdoc}
*/
public function part1(array $data): int
{
$bits = [];
foreach ($data as $binary) {
$split = str_split($binary);
foreach ($split as $position => $value) {
$bits[$position][] = $value;
}
}
$gammaRate = '';
$epsilonRate = '';
foreach ($bits as $values) {
$zeros = array_filter($values, static fn ($value) => $value === '0');
$ones = array_filter($values, static fn ($value) => $value === '1');
$gammaRate .= ($ones > $zeros) ? '1' : '0';
$epsilonRate .= ($ones < $zeros) ? '1' : '0';
}
return (int) (bindec($gammaRate) * bindec($epsilonRate));
}
}

View File

@ -1,13 +1,16 @@
<?php
namespace AdventOfCode21;
namespace trizz\AdventOfCode\Y21;
class Day4 extends AbstractCommand
use trizz\AdventOfCode\Solution;
class Day4 extends Solution
{
/**
* {@inheritdoc}
*/
protected static int $day = 4;
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;
/**
* @param int[] $winningCard
@ -30,7 +33,7 @@ class Day4 extends AbstractCommand
/**
* {@inheritdoc}
*/
protected function part1(array $data): int|string
public function part1(array $data): int|string
{
return $this->playBingo($data, firstWins: true);
}
@ -38,7 +41,7 @@ class Day4 extends AbstractCommand
/**
* {@inheritdoc}
*/
protected function part2(array $data): int|string
public function part2(array $data): int|string
{
return $this->playBingo($data, firstWins: false);
}

View File

@ -1,15 +1,17 @@
<?php
namespace AdventOfCode21;
namespace trizz\AdventOfCode\Y21;
use trizz\AdventOfCode\Solution;
use JetBrains\PhpStorm\Immutable;
class Day6 extends AbstractCommand
class Day6 extends Solution
{
/**
* {@inheritdoc}
*/
protected static int $day = 6;
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;
/**
* @var int[]
@ -31,7 +33,7 @@ class Day6 extends AbstractCommand
/**
* {@inheritdoc}
*/
protected function part1(array $data): int
public function part1(array $data): int
{
return $this->processPuzzle(80, $data[0]);
}
@ -39,7 +41,7 @@ class Day6 extends AbstractCommand
/**
* {@inheritdoc}
*/
protected function part2(array $data): int
public function part2(array $data): int
{
return $this->processPuzzle(256, $data[0]);
}

View File

@ -1,18 +1,22 @@
<?php
namespace AdventOfCode21;
namespace trizz\AdventOfCode\Y21;
class Day7 extends AbstractCommand
use trizz\AdventOfCode\ExecuteDay;
use trizz\AdventOfCode\Solution;
class Day7 extends Solution
{
/**
* {@inheritdoc}
*/
protected static int $day = 7;
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;
/**
* {@inheritdoc}
*/
protected function part1(array $data): int
public function part1(array $data): int
{
return $this->calculateFuel($data[0], forPart2: false);
}
@ -20,7 +24,7 @@ class Day7 extends AbstractCommand
/**
* {@inheritdoc}
*/
protected function part2(array $data): int
public function part2(array $data): int
{
return $this->calculateFuel($data[0], forPart2: true);
}