82 lines
2.5 KiB
PHP
82 lines
2.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Lubiana\Aoc\Y2023\D03;
|
||
|
|
||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||
|
use PHPUnit\Framework\TestCase;
|
||
|
|
||
|
final class LineTest extends TestCase
|
||
|
{
|
||
|
private $assertEquals;
|
||
|
|
||
|
public static function lineSoure(): \Generator
|
||
|
{
|
||
|
yield ['467..114..', [], [467, 114], [467]];
|
||
|
yield ['...*.....-', [3], [], []];
|
||
|
yield ['..35..633.', [], [35, 633], [35, 633]];
|
||
|
yield ['......#...', [6], [], []];
|
||
|
yield ['617*......', [3], [617], [617]];
|
||
|
yield ['.....+.58.', [5], [58], []];
|
||
|
yield ['..592.....', [], [592], [592]];
|
||
|
yield ['......755.', [], [755], [755]];
|
||
|
yield ['...$.*....', [3, 5], [], []];
|
||
|
yield ['.664.598..', [], [664, 598], [664, 598]];
|
||
|
}
|
||
|
|
||
|
#[DataProvider('lineSoure')]
|
||
|
public function testSymbolPosition(
|
||
|
string $input,
|
||
|
array $symbolPositions,
|
||
|
array $numbers,
|
||
|
array $valids
|
||
|
): void {
|
||
|
$line = Line::fromString($input);
|
||
|
$this->assertEquals($symbolPositions, $line->symbolPositions);
|
||
|
$this->assertEquals($numbers, array_map(
|
||
|
fn (Number $n): int => $n->value,
|
||
|
$line->numbers,
|
||
|
));
|
||
|
$this->assertEquals($valids, array_map(
|
||
|
fn (Number $n): int => $n->value,
|
||
|
$line->getValidNumbers(),
|
||
|
));
|
||
|
}
|
||
|
|
||
|
public function testSmallInput(): void
|
||
|
{
|
||
|
$line = Line::fromString(
|
||
|
"................939...644..............791..*.551....35...937..806...............&.............536.930...703...565......*...............*640"
|
||
|
);
|
||
|
$validNumbers = $line->getValidNumbers();
|
||
|
|
||
|
$this->assertEquals(1, count($validNumbers));
|
||
|
|
||
|
}
|
||
|
|
||
|
public function testSolve(): void
|
||
|
{
|
||
|
$d03 = new D03(D03::TESTFILE);
|
||
|
$this->assertEquals(4361, $d03->solve());
|
||
|
}
|
||
|
|
||
|
public function testValidNumbers(): void {
|
||
|
$d03 = new D03(D03::TESTFILE);
|
||
|
$lines = $d03->getLines();
|
||
|
$this->assertEquals(
|
||
|
[467],
|
||
|
array_map(fn (Number $n):int => $n->value, array_shift($lines)->getValidNumbers())
|
||
|
);
|
||
|
$this->assertEquals(
|
||
|
[],
|
||
|
array_map(fn (Number $n):int => $n->value, array_shift($lines)->getValidNumbers())
|
||
|
);
|
||
|
$this->assertEquals(
|
||
|
[35, 633],
|
||
|
array_map(fn (Number $n):int => $n->value, array_shift($lines)->getValidNumbers())
|
||
|
);
|
||
|
$this->assertEquals(
|
||
|
[],
|
||
|
array_map(fn (Number $n):int => $n->value, array_shift($lines)->getValidNumbers())
|
||
|
);
|
||
|
}
|
||
|
}
|