2021-12-07 09:49:43 +01:00
|
|
|
<?php
|
|
|
|
|
2021-12-09 22:41:22 +01:00
|
|
|
namespace trizz\AdventOfCode\Y21;
|
2021-12-07 09:49:43 +01:00
|
|
|
|
2021-12-09 22:41:22 +01:00
|
|
|
use trizz\AdventOfCode\Solution;
|
2021-12-07 09:49:43 +01:00
|
|
|
use JetBrains\PhpStorm\Immutable;
|
|
|
|
|
2021-12-09 22:41:22 +01:00
|
|
|
class Day6 extends Solution
|
2021-12-07 09:49:43 +01:00
|
|
|
{
|
2021-12-09 22:41:22 +01:00
|
|
|
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;
|
2021-12-07 09:49:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int[]
|
|
|
|
* @psalm-param array{int: int}
|
|
|
|
*/
|
|
|
|
#[Immutable]
|
|
|
|
protected array $startState = [
|
|
|
|
8 => 0,
|
|
|
|
7 => 0,
|
|
|
|
6 => 0,
|
|
|
|
5 => 0,
|
|
|
|
4 => 0,
|
|
|
|
3 => 0,
|
|
|
|
2 => 0,
|
|
|
|
1 => 0,
|
|
|
|
0 => 0,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2021-12-09 22:41:22 +01:00
|
|
|
public function part1(array $data): int
|
2021-12-07 09:49:43 +01:00
|
|
|
{
|
|
|
|
return $this->processPuzzle(80, $data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2021-12-09 22:41:22 +01:00
|
|
|
public function part2(array $data): int
|
2021-12-07 09:49:43 +01:00
|
|
|
{
|
|
|
|
return $this->processPuzzle(256, $data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int[] $state
|
|
|
|
* @psalm-param array{int: int} $state
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function processDay(array $state): array
|
|
|
|
{
|
|
|
|
$newState = $state;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int $key
|
|
|
|
* @var int $stateValue
|
|
|
|
*/
|
|
|
|
foreach ($state as $key => $stateValue) {
|
|
|
|
$newKey = $key - 1;
|
|
|
|
|
|
|
|
if ($newKey < 0) {
|
|
|
|
$newState[8] = $stateValue;
|
|
|
|
$newKey = 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newState[$key] -= $stateValue;
|
|
|
|
$newState[$newKey] += $stateValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $newState;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function processPuzzle(int $numberOfDays, string $data): int
|
|
|
|
{
|
|
|
|
$state = $this->startState;
|
|
|
|
|
|
|
|
array_map(static function (string $stateValue) use (&$state) {
|
|
|
|
++$state[(int) $stateValue];
|
|
|
|
}, explode(',', $data));
|
|
|
|
|
|
|
|
for ($day = 0; $day < $numberOfDays; ++$day) {
|
|
|
|
$state = $this->processDay($state);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) array_sum($state);
|
|
|
|
}
|
|
|
|
}
|