-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e1b8f1
commit 11983ad
Showing
4 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace App\Year2024\Day03; | ||
|
||
use App\Puzzle\AbstractPuzzle; | ||
|
||
class Puzzle extends AbstractPuzzle | ||
{ | ||
public function read(): array | ||
{ | ||
$array = []; | ||
|
||
if ($file = fopen($this->getPathIn(), 'r')) { | ||
while (($line = fgets($file)) !== false) { | ||
preg_match_all('/mul\(\d+,\d+\)|do\(\)|don\'t\(\)/', $line, $matches, PREG_SET_ORDER); | ||
|
||
$enabled = true; | ||
foreach ($matches as $match) { | ||
if ($match[0] === 'do()') { | ||
$enabled = true; | ||
} elseif ($match[0] === "don't()") { | ||
$enabled = false; | ||
} elseif ($enabled) { | ||
$array[] = $match[0]; | ||
} | ||
} | ||
|
||
// preg_match_all('/mul\(\d+,\d+\)/', $line, $matches, PREG_SET_ORDER); | ||
// foreach ($matches as $match) { | ||
// $array[] = $match[0]; | ||
// } | ||
} | ||
fclose($file); | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
public function exec1(array $input = []): int | ||
{ | ||
$result = 0; | ||
|
||
foreach ($input as $instruction) { | ||
preg_match('/mul\((?<x>\d+),(?<y>\d+)\)/', $instruction, $matches); | ||
$result += ($matches['x'] * $matches['y']); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function exec2(array $input = []): int | ||
{ | ||
$result = 0; | ||
|
||
foreach ($input as $instruction) { | ||
preg_match('/mul\((?<x>\d+),(?<y>\d+)\)/', $instruction, $matches); | ||
$result += ($matches['x'] * $matches['y']); | ||
} | ||
|
||
return $result; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Tests\Year2024\Day03; | ||
|
||
use App\Year2024\Day03\Puzzle; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PuzzleTest extends TestCase | ||
{ | ||
/** | ||
* @var Puzzle | ||
*/ | ||
private object $runner; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private array $array; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->runner = new Puzzle(); | ||
|
||
$this->array = $this->runner->read(); | ||
} | ||
|
||
public function testExec1(): void | ||
{ | ||
$this->assertEquals(48, $this->runner->exec1($this->array)); | ||
} | ||
|
||
public function testExec2(): void | ||
{ | ||
$this->assertEquals(48, $this->runner->exec2($this->array)); | ||
} | ||
} |