Update stuff

This commit is contained in:
2024-12-01 11:30:31 +01:00
parent d3f2406b23
commit 6915c35fed
13 changed files with 784 additions and 805 deletions

View File

@ -21,11 +21,9 @@ final class Day2 extends Solution
$horizontal = 0;
foreach ($data as $current) {
/**
* @var string $direction
* @var int $distance
*/
[$direction, $distance] = explode(' ', $current);
$direction = (string) $direction;
$distance = (int) $distance;
match ($direction) {
'forward' => $horizontal += $distance,
@ -46,11 +44,9 @@ final class Day2 extends Solution
$horizontal = 0;
foreach ($data as $current) {
/**
* @var string $direction
* @var int $distance
*/
[$direction, $distance] = explode(' ', $current);
$direction = (string) $direction;
$distance = (int) $distance;
// Can't use 'match' here because of the multiple expressions for 'forward'.
switch ($direction) {

View File

@ -52,6 +52,10 @@ final class Day7 extends Solution
}
}
if (empty($fuelPerPosition)) {
return -1;
}
return (int) min($fuelPerPosition);
}
}

View File

@ -40,7 +40,7 @@ final class Day8 extends Solution
}
#[\Override]
public function part2(array $data): int|string
public function part2(array $data): int
{
$sequences = [];

View File

@ -69,7 +69,7 @@ final class Day2 extends Solution
}
/**
* @return array<int, array<int, string>>
* @return array{string, non-empty-list<string>}
*/
private function getHand(string $line): array
{

View File

@ -30,6 +30,17 @@ final class Day3 extends Solution
$numbers = $this->checkLocation($row, $col);
$top = (int) $this->processNumbers($numbers['top'], sum: true);
$bottom = (int) $this->processNumbers($numbers['bottom'], sum: true);
if ($numbers === []) {
continue;
}
if (empty($numbers['left'])) {
continue;
}
if (empty($numbers['right'])) {
continue;
}
$score += $top + $bottom + max($numbers['left']) + max($numbers['right']);
}
@ -54,6 +65,18 @@ final class Day3 extends Solution
$top = (array) $this->processNumbers($numbers['top']);
$bottom = (array) $this->processNumbers($numbers['bottom']);
if ($numbers === []) {
continue;
}
if (empty($numbers['left'])) {
continue;
}
if (empty($numbers['right'])) {
continue;
}
$left = max($numbers['left']);
$right = max($numbers['right']);
@ -81,7 +104,7 @@ final class Day3 extends Solution
if ($numbers[0] !== 0 && $numbers[1] === 0 && $numbers[2] !== 0) {
$result[] = $numbers[0];
$result[] = $numbers[2];
} else {
} elseif ($numbers !== []) {
$result[] = max($numbers);
}
@ -127,7 +150,7 @@ final class Day3 extends Solution
return $numbers;
}
private function getNumber(int $row, int $col, string $direction = null): ?string
private function getNumber(int $row, int $col, ?string $direction = null): ?string
{
$number = $this->matrix[$row][$col] ?? null;

View File

@ -2,7 +2,9 @@
namespace trizz\AdventOfCode\Y23;
final class Day5 extends \trizz\AdventOfCode\Solution
use trizz\AdventOfCode\Solution;
final class Day5 extends Solution
{
public static null|int|string $part1ExampleResult = 35;
@ -47,6 +49,10 @@ final class Day5 extends \trizz\AdventOfCode\Solution
$locations[$seed] = $location;
}
if ($locations === []) {
return -1;
}
return min($locations);
}
@ -97,7 +103,7 @@ final class Day5 extends \trizz\AdventOfCode\Solution
}
if (!empty($line) && !str_contains($line, ':')) {
[$destinationRange, $sourceRangeStart, $rangeLength] = array_values(array_map('intval', explode(' ', $line)));
[$destinationRange, $sourceRangeStart, $rangeLength] = array_map('intval', explode(' ', $line));
$this->maps[$currentMap][] = [
'destinationRange' => $destinationRange,
'sourceRangeStart' => $sourceRangeStart,

View File

@ -7,10 +7,14 @@ use trizz\AdventOfCode\Solution;
final class Day1 extends Solution
{
public static null|int|string $part1ExampleResult = 11;
public static null|int|string $part1Result = 1882714;
public static null|int|string $part2ExampleResult = 31;
public static null|int|string $part2Result = null;
public static null|int|string $part1Result = 1882714;
public static null|int|string $part2ExampleResult = 31;
public static null|int|string $part2Result = 19437052;
#[\Override]
public function part1(array $data): int
{
[$listLeft, $listRight] = $this->getList($data);
@ -23,13 +27,14 @@ final class Day1 extends Solution
return $score;
}
#[\Override]
public function part2(array $data): int
{
$score = 0;
[$listLeft, $listRight] = $this->getList($data);
foreach ($listLeft as $index => $left) {
$right = count(array_filter($listRight, fn ($x) => $x === $left));
$right = count(array_filter($listRight, fn ($x): bool => $x === $left));
if ($right > 0) {
$score += $left * $right;
}