Skip to content

Commit

Permalink
feat: add day
Browse files Browse the repository at this point in the history
  • Loading branch information
migueabellan committed Dec 3, 2024
1 parent 0e1b8f1 commit 11983ad
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test:
@docker-compose exec php php vendor/bin/phpunit --testdox tests/Year2024/

day1:
@docker-compose exec php bin/console puzzle:exec -y 2024 -d 02 -p 1
@docker-compose exec php bin/console puzzle:exec -y 2024 -d 03 -p 1

day2:
@docker-compose exec php bin/console puzzle:exec -y 2024 -d 02 -p 2
@docker-compose exec php bin/console puzzle:exec -y 2024 -d 03 -p 2
63 changes: 63 additions & 0 deletions src/Year2024/Day03/Puzzle.php
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;
}
}

1 change: 1 addition & 0 deletions src/Year2024/Day03/_in.txt
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))
36 changes: 36 additions & 0 deletions tests/Year2024/Day03/PuzzleTest.php
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));
}
}

0 comments on commit 11983ad

Please sign in to comment.