Apply code changes after dependency update
This commit is contained in:
parent
c5400b78d8
commit
a3be3f026d
@ -15,6 +15,7 @@ final class ExecuteDay extends Command
|
||||
|
||||
private string $title;
|
||||
|
||||
#[\Override]
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
@ -24,6 +25,7 @@ final class ExecuteDay extends Command
|
||||
->addArgument('year', InputArgument::OPTIONAL, 'The year', date('y'));
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function initialize(InputInterface $input, OutputInterface $output): void
|
||||
{
|
||||
$this->day = $input->getArgument('day');
|
||||
@ -36,6 +38,7 @@ final class ExecuteDay extends Command
|
||||
$output->writeln(str_repeat('-', strlen($this->title)));
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$className = sprintf('%s\\Y%d\\Day%d', __NAMESPACE__, $this->year, $this->day);
|
||||
|
@ -10,6 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
final class Puzzle extends Command
|
||||
{
|
||||
#[\Override]
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
@ -19,6 +20,7 @@ final class Puzzle extends Command
|
||||
->addArgument('year', InputArgument::OPTIONAL, 'The year', date('y'));
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$contents = file_get_contents(
|
||||
|
@ -6,13 +6,13 @@ use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
abstract class Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = null;
|
||||
public static null|int|string $part1ExampleResult = null;
|
||||
|
||||
public static int|string|null $part1Result = null;
|
||||
public static null|int|string $part1Result = null;
|
||||
|
||||
public static int|string|null $part2ExampleResult = null;
|
||||
public static null|int|string $part2ExampleResult = null;
|
||||
|
||||
public static int|string|null $part2Result = null;
|
||||
public static null|int|string $part2Result = null;
|
||||
|
||||
/**
|
||||
* @var bool When false, do not apply the `array_filter` function when the data is loaded.
|
||||
@ -89,12 +89,12 @@ abstract class Solution
|
||||
|
||||
public function hasData(): bool
|
||||
{
|
||||
return !empty($this->data);
|
||||
return $this->data !== null && $this->data !== [];
|
||||
}
|
||||
|
||||
public function hasExampleData(): bool
|
||||
{
|
||||
return !empty($this->exampleData);
|
||||
return $this->exampleData !== [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,7 +25,7 @@ final class Arr
|
||||
} else {
|
||||
$values = $depth === 1
|
||||
? array_values($item)
|
||||
: static::flatten($item, $depth - 1);
|
||||
: self::flatten($item, $depth - 1);
|
||||
|
||||
foreach ($values as $value) {
|
||||
$result[] = $value;
|
||||
|
@ -6,17 +6,15 @@ use trizz\AdventOfCode\Solution;
|
||||
|
||||
final class Day1 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 7;
|
||||
public static null|int|string $part1ExampleResult = 7;
|
||||
|
||||
public static int|string|null $part1Result = 1688;
|
||||
public static null|int|string $part1Result = 1688;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 5;
|
||||
public static null|int|string $part2ExampleResult = 5;
|
||||
|
||||
public static int|string|null $part2Result = 1728;
|
||||
public static null|int|string $part2Result = 1728;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
$previous = null;
|
||||
@ -34,9 +32,7 @@ final class Day1 extends Solution
|
||||
return $increases;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part2(array $data): int
|
||||
{
|
||||
$previousSum = null;
|
||||
|
@ -6,17 +6,15 @@ use trizz\AdventOfCode\Solution;
|
||||
|
||||
final class Day2 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 150;
|
||||
public static null|int|string $part1ExampleResult = 150;
|
||||
|
||||
public static int|string|null $part1Result = 1_654_760;
|
||||
public static null|int|string $part1Result = 1_654_760;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 900;
|
||||
public static null|int|string $part2ExampleResult = 900;
|
||||
|
||||
public static int|string|null $part2Result = 1_956_047_400;
|
||||
public static null|int|string $part2Result = 1_956_047_400;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
$depth = 0;
|
||||
@ -40,9 +38,7 @@ final class Day2 extends Solution
|
||||
return $depth * $horizontal;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part2(array $data): int
|
||||
{
|
||||
$aim = 0;
|
||||
|
@ -6,13 +6,11 @@ use trizz\AdventOfCode\Solution;
|
||||
|
||||
final class Day3 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 198;
|
||||
public static null|int|string $part1ExampleResult = 198;
|
||||
|
||||
public static int|string|null $part1Result = 3_309_596;
|
||||
public static null|int|string $part1Result = 3_309_596;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
$bits = [];
|
||||
@ -29,8 +27,8 @@ final class Day3 extends Solution
|
||||
$epsilonRate = '';
|
||||
|
||||
foreach ($bits as $bit) {
|
||||
$zeros = array_filter($bit, static fn ($value) => $value === '0');
|
||||
$ones = array_filter($bit, static fn ($value) => $value === '1');
|
||||
$zeros = array_filter($bit, static fn ($value): bool => $value === '0');
|
||||
$ones = array_filter($bit, static fn ($value): bool => $value === '1');
|
||||
|
||||
$gammaRate .= ($ones > $zeros) ? '1' : '0';
|
||||
$epsilonRate .= ($ones < $zeros) ? '1' : '0';
|
||||
|
@ -6,25 +6,21 @@ use trizz\AdventOfCode\Solution;
|
||||
|
||||
final class Day4 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 4512;
|
||||
public static null|int|string $part1ExampleResult = 4512;
|
||||
|
||||
public static int|string|null $part1Result = 60368;
|
||||
public static null|int|string $part1Result = 60368;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 1924;
|
||||
public static null|int|string $part2ExampleResult = 1924;
|
||||
|
||||
public static int|string|null $part2Result = 17435;
|
||||
public static null|int|string $part2Result = 17435;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part1(array $data): int|string
|
||||
{
|
||||
return $this->playBingo($data, firstWins: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part2(array $data): int|string
|
||||
{
|
||||
return $this->playBingo($data, firstWins: false);
|
||||
@ -41,7 +37,7 @@ final class Day4 extends Solution
|
||||
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));
|
||||
$unusedNumbers = array_keys(array_filter($return, static fn (bool $value): bool => !$value));
|
||||
|
||||
return (int) array_sum($unusedNumbers) * $number;
|
||||
}
|
||||
@ -56,10 +52,10 @@ final class Day4 extends Solution
|
||||
private function explodeNumbers(string $numberList, string $separator): array
|
||||
{
|
||||
return array_map(
|
||||
static fn ($value) => (int) $value,
|
||||
static fn ($value): int => (int) $value,
|
||||
array_filter(
|
||||
explode($separator, $numberList),
|
||||
static fn (string $value) => $value !== ''
|
||||
static fn (string $value): bool => $value !== ''
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -96,7 +92,7 @@ final class Day4 extends Solution
|
||||
}
|
||||
|
||||
$winningCards = $this->checkCards($cards, $finishedCards);
|
||||
if (empty($winningCards)) {
|
||||
if ($winningCards === []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -129,7 +125,7 @@ final class Day4 extends Solution
|
||||
{
|
||||
$cards = array_chunk($data, 5);
|
||||
foreach ($cards as $card => $rows) {
|
||||
$cards[$card] = array_map(fn ($value) => $this->explodeNumbers($value, ' '), $rows);
|
||||
$cards[$card] = array_map(fn ($value): array => $this->explodeNumbers($value, ' '), $rows);
|
||||
|
||||
foreach ($cards[$card] as $row => $number) {
|
||||
$cards[$card][$row] = array_fill_keys(array_values($number), false);
|
||||
@ -147,6 +143,7 @@ final class Day4 extends Solution
|
||||
private function checkCards(array $cards, array $finishedCards): array
|
||||
{
|
||||
$winningCards = [];
|
||||
|
||||
// Check rows
|
||||
/**
|
||||
* @var int $cardIndex
|
||||
|
@ -7,13 +7,13 @@ use trizz\AdventOfCode\Solution;
|
||||
|
||||
final class Day6 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 5934;
|
||||
public static null|int|string $part1ExampleResult = 5934;
|
||||
|
||||
public static int|string|null $part1Result = 350917;
|
||||
public static null|int|string $part1Result = 350917;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 26_984_457_539;
|
||||
public static null|int|string $part2ExampleResult = 26_984_457_539;
|
||||
|
||||
public static int|string|null $part2Result = 1_592_918_715_629;
|
||||
public static null|int|string $part2Result = 1_592_918_715_629;
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
@ -32,17 +32,13 @@ final class Day6 extends Solution
|
||||
0 => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
return $this->processPuzzle(80, $data[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part2(array $data): int
|
||||
{
|
||||
return $this->processPuzzle(256, $data[0]);
|
||||
|
@ -6,25 +6,21 @@ use trizz\AdventOfCode\Solution;
|
||||
|
||||
final class Day7 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 37;
|
||||
public static null|int|string $part1ExampleResult = 37;
|
||||
|
||||
public static int|string|null $part1Result = 344297;
|
||||
public static null|int|string $part1Result = 344297;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 168;
|
||||
public static null|int|string $part2ExampleResult = 168;
|
||||
|
||||
public static int|string|null $part2Result = 97_164_301;
|
||||
public static null|int|string $part2Result = 97_164_301;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
return $this->calculateFuel($data[0], forPart2: false);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part2(array $data): int
|
||||
{
|
||||
return $this->calculateFuel($data[0], forPart2: true);
|
||||
@ -32,7 +28,7 @@ final class Day7 extends Solution
|
||||
|
||||
private function calculateFuel(string $data, bool $forPart2 = false): int
|
||||
{
|
||||
$crabs = array_map(static fn (string $crab) => (int) $crab, explode(',', $data));
|
||||
$crabs = array_map(static fn (string $crab): int => (int) $crab, explode(',', $data));
|
||||
|
||||
/** @psalm-param array{int: int} $fuelPerPosition */
|
||||
$fuelPerPosition = [];
|
||||
|
@ -8,30 +8,28 @@ use trizz\AdventOfCode\Utils\Str;
|
||||
|
||||
final class Day8 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 26;
|
||||
public static null|int|string $part1ExampleResult = 26;
|
||||
|
||||
public static int|string|null $part1Result = 397;
|
||||
public static null|int|string $part1Result = 397;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 61229;
|
||||
public static null|int|string $part2ExampleResult = 61229;
|
||||
|
||||
public static int|string|null $part2Result = 1_027_422;
|
||||
public static null|int|string $part2Result = 1_027_422;
|
||||
|
||||
private array $digitPatterns;
|
||||
|
||||
private array $patternDigits;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
$values = array_map(
|
||||
static fn ($item) => strlen((string) $item),
|
||||
static fn ($item): int => strlen((string) $item),
|
||||
Arr::flatten(
|
||||
array_map(
|
||||
static fn ($item) => explode(' ', $item),
|
||||
static fn ($item): array => explode(' ', (string) $item),
|
||||
array_map(
|
||||
static fn ($item) => explode(' | ', $item)[1],
|
||||
static fn ($item): string => explode(' | ', (string) $item)[1],
|
||||
$data
|
||||
)
|
||||
)
|
||||
@ -41,6 +39,7 @@ final class Day8 extends Solution
|
||||
return count(array_intersect($values, [2, 4, 3, 7]));
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function part2(array $data): int|string
|
||||
{
|
||||
$sequences = [];
|
||||
@ -80,37 +79,37 @@ final class Day8 extends Solution
|
||||
/** @noinspection PackedHashtableOptimizationInspection */
|
||||
$findDigit = [
|
||||
// 1 is the only one with 2 segments
|
||||
1 => static fn (string $pattern) => strlen($pattern) === 2,
|
||||
1 => static fn (string $pattern): bool => strlen($pattern) === 2,
|
||||
// 7 is the only one with 3 segments
|
||||
7 => static fn (string $pattern) => strlen($pattern) === 3,
|
||||
7 => static fn (string $pattern): bool => strlen($pattern) === 3,
|
||||
// 4 is the only one with 4 segments
|
||||
4 => static fn (string $pattern) => strlen($pattern) === 4,
|
||||
4 => static fn (string $pattern): bool => strlen($pattern) === 4,
|
||||
// 8 is the only one with 7 segments
|
||||
8 => static fn (string $pattern) => strlen($pattern) === 7,
|
||||
8 => static fn (string $pattern): bool => strlen($pattern) === 7,
|
||||
// 9 is 6 segments, matches segments for 4
|
||||
9 => fn (string $pattern) => strlen($pattern) === 6 && Str::matchesAll(
|
||||
9 => fn (string $pattern): bool => strlen($pattern) === 6 && Str::matchesAll(
|
||||
$pattern,
|
||||
$this->digitPatterns[4] ?? ''
|
||||
),
|
||||
// 0 is 6 segments, matching 1's segments (9 is already out)
|
||||
0 => fn (string $pattern) => strlen($pattern) === 6 && Str::matchesAll(
|
||||
0 => fn (string $pattern): bool => strlen($pattern) === 6 && Str::matchesAll(
|
||||
$pattern,
|
||||
$this->digitPatterns[1] ?? ''
|
||||
),
|
||||
// 6 is 6 segments, the only one left
|
||||
6 => static fn (string $pattern) => strlen($pattern) === 6,
|
||||
6 => static fn (string $pattern): bool => strlen($pattern) === 6,
|
||||
// 3 is 5 segments and matches 1's segments
|
||||
3 => fn (string $pattern) => strlen($pattern) === 5 && Str::matchesAll(
|
||||
3 => fn (string $pattern): bool => strlen($pattern) === 5 && Str::matchesAll(
|
||||
$pattern,
|
||||
$this->digitPatterns[1] ?? ''
|
||||
),
|
||||
// 5 is 5 segments, and 9 has all the segments of 5
|
||||
5 => fn (string $pattern) => strlen($pattern) === 5 && Str::matchesAll(
|
||||
5 => fn (string $pattern): bool => strlen($pattern) === 5 && Str::matchesAll(
|
||||
$this->digitPatterns[9] ?? '',
|
||||
$pattern
|
||||
),
|
||||
// 2 is the only one remaining
|
||||
2 => static fn (string $pattern) => true,
|
||||
2 => static fn (string $pattern): bool => true,
|
||||
];
|
||||
|
||||
foreach ($findDigit as $digit => $test) {
|
||||
|
@ -6,21 +6,23 @@ use trizz\AdventOfCode\Solution;
|
||||
|
||||
final class Day1 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 24000;
|
||||
public static null|int|string $part1ExampleResult = 24000;
|
||||
|
||||
public static int|string|null $part1Result = 72240;
|
||||
public static null|int|string $part1Result = 72240;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 45000;
|
||||
public static null|int|string $part2ExampleResult = 45000;
|
||||
|
||||
public static int|string|null $part2Result = 210957;
|
||||
public static null|int|string $part2Result = 210957;
|
||||
|
||||
public bool $filterDataOnLoad = false;
|
||||
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
return $this->calculateCalories($data)[0];
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function part2(array $data): int
|
||||
{
|
||||
$results = $this->calculateCalories($data);
|
||||
|
@ -7,14 +7,15 @@ use trizz\AdventOfCode\Y22\Day2\RPS;
|
||||
|
||||
final class Day2 extends Solution
|
||||
{
|
||||
public static int|string|null $part1ExampleResult = 15;
|
||||
public static null|int|string $part1ExampleResult = 15;
|
||||
|
||||
public static int|string|null $part1Result = 10994;
|
||||
public static null|int|string $part1Result = 10994;
|
||||
|
||||
public static int|string|null $part2ExampleResult = 12;
|
||||
public static null|int|string $part2ExampleResult = 12;
|
||||
|
||||
public static int|string|null $part2Result = 12526;
|
||||
public static null|int|string $part2Result = 12526;
|
||||
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
$score = 0;
|
||||
@ -28,6 +29,7 @@ final class Day2 extends Solution
|
||||
return $score;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function part2(array $data): int
|
||||
{
|
||||
$score = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user