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 4, 2024
1 parent 2ff34be commit 526c212
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 43 deletions.
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ lint:
test:
@docker-compose exec php php vendor/bin/phpunit --testdox tests/Year2024/

day1:
@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 03 -p 2
# make exec d=04 p=1
exec:
@docker-compose exec php bin/console puzzle:exec -y 2024 -d ${d} -p ${p}
59 changes: 22 additions & 37 deletions src/Year2024/Day03/Puzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,17 @@

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']);
foreach ($input as $line) {
preg_match_all('/mul\(\d+,\d+\)/', $line, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
preg_match('/mul\((?<x>\d+),(?<y>\d+)\)/', $match[0], $m);
$result += ($m['x'] * $m['y']);
}
}

return $result;
Expand All @@ -52,11 +26,22 @@ 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']);
foreach ($input as $line) {
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) {
preg_match('/mul\((?<x>\d+),(?<y>\d+)\)/', $match[0], $m);
$result += ($m['x'] * $m['y']);
}
}
}

return $result;
}
}
86 changes: 86 additions & 0 deletions src/Year2024/Day04/Puzzle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Year2024\Day04;

use App\Puzzle\AbstractPuzzle;

class Puzzle extends AbstractPuzzle
{
public function read(): array
{
$array = [];

if ($file = fopen($this->getPathIn(), 'r')) {
while (($line = fgets($file)) !== false) {
$array[] = str_split(trim($line));
}
fclose($file);
}

return $array;
}

public function exec1(array $input = []): int
{
$result = 0;

$word = 'XMAS';

$coorddinates = [
[-1, -1], [-1, +0], [-1, 1],
[+0, -1], /*******/ [+0, 1],
[+1, -1], [+1, +0], [+1, 1],
];

for ($i = 0; $i < count($input); $i++) {
for ($j = 0; $j < count($input); $j++) {
if ($input[$i][$j] === $word[0]) {
foreach ($coorddinates as $coordinate) {
$_i = $i;
$_j = $j;
$acc = $word[0];
while (str_starts_with($word, $acc)) {
$_i += $coordinate[0];
$_j += $coordinate[1];
if (!isset($input[$_i][$_j])) {
break;
}

$acc .= $input[$_i][$_j];

if ($word === $acc) {
$result++;
}
}
}
}
}
}

return $result;
}

public function exec2(array $input = []): int
{
$result = 0;

$words = ['MAS', 'SAM'];

for ($i = 0; $i < count($input); $i++) {
for ($j = 0; $j < count($input); $j++) {
if ($input[$i][$j] === 'A') {
$tl = $input[$i + 1][$j - 1] ?? '';
$tr = $input[$i + 1][$j + 1] ?? '';
$bl = $input[$i - 1][$j - 1] ?? '';
$br = $input[$i - 1][$j + 1] ?? '';

if (in_array($tl.'A'.$br, $words) && in_array($tr.'A'.$bl, $words)) {
$result++;
}
}
}
}

return $result;
}
}
10 changes: 10 additions & 0 deletions src/Year2024/Day04/_in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
2 changes: 1 addition & 1 deletion tests/Year2024/Day03/PuzzleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function setUp(): void

public function testExec1(): void
{
$this->assertEquals(48, $this->runner->exec1($this->array));
$this->assertEquals(161, $this->runner->exec1($this->array));
}

public function testExec2(): void
Expand Down
36 changes: 36 additions & 0 deletions tests/Year2024/Day04/PuzzleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tests\Year2024\Day04;

use App\Year2024\Day04\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(18, $this->runner->exec1($this->array));
}

public function testExec2(): void
{
$this->assertEquals(9, $this->runner->exec2($this->array));
}
}

0 comments on commit 526c212

Please sign in to comment.