2021-12-09 22:41:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace trizz\AdventOfCode;
|
|
|
|
|
|
|
|
use JetBrains\PhpStorm\ArrayShape;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-12-01 09:48:03 +01:00
|
|
|
/**
|
|
|
|
* @var bool When false, do not apply the `array_filter` function when the data is loaded.
|
|
|
|
*/
|
|
|
|
public bool $filterDataOnLoad = true;
|
|
|
|
|
2021-12-09 22:41:22 +01:00
|
|
|
/**
|
|
|
|
* @var string[] The data to use.
|
2022-11-29 19:52:02 +01:00
|
|
|
*
|
2021-12-09 22:41:22 +01:00
|
|
|
* @psalm-suppress PropertyNotSetInConstructor
|
|
|
|
*/
|
|
|
|
public ?array $data = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[] The example data.
|
2022-11-29 19:52:02 +01:00
|
|
|
*
|
2021-12-09 22:41:22 +01:00
|
|
|
* @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';
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:52:02 +01:00
|
|
|
public function loadData(): void
|
2021-12-09 22:41:22 +01:00
|
|
|
{
|
|
|
|
$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) {
|
2022-12-01 09:48:03 +01:00
|
|
|
$this->data = $this->filterDataOnLoad ? array_filter(explode(PHP_EOL, $data)) : explode(PHP_EOL, $data);
|
2021-12-09 22:41:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($dataExampleFile)) {
|
|
|
|
$data = file_get_contents($dataExampleFile);
|
|
|
|
if ($data !== false) {
|
2022-12-01 09:48:03 +01:00
|
|
|
$this->exampleData = $this->filterDataOnLoad ? array_filter(explode(PHP_EOL, $data)) : explode(PHP_EOL, $data);
|
2021-12-09 22:41:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function year(): int
|
|
|
|
{
|
|
|
|
return (int) substr(explode('\\', static::class)[2], 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function day(): int
|
|
|
|
{
|
|
|
|
return (int) substr(explode('\\', static::class)[3], 3);
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:52:02 +01:00
|
|
|
public function hasData(): bool
|
2021-12-09 22:41:22 +01:00
|
|
|
{
|
|
|
|
return !empty($this->data);
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:52:02 +01:00
|
|
|
public function hasExampleData(): bool
|
2021-12-09 22:41:22 +01:00
|
|
|
{
|
|
|
|
return !empty($this->exampleData);
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:52:02 +01:00
|
|
|
/**
|
|
|
|
* @return array{part1: int|string, part2: int|string}
|
|
|
|
*/
|
2021-12-09 22:25:59 +00:00
|
|
|
#[ArrayShape(['part1' => 'int|string', 'part2' => 'int|string'])]
|
2021-12-09 22:41:22 +01:00
|
|
|
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 ?? []);
|
|
|
|
}
|
|
|
|
}
|