summaryrefslogtreecommitdiff
path: root/2024/PHP
diff options
context:
space:
mode:
Diffstat (limited to '2024/PHP')
-rw-r--r--2024/PHP/day_04/Grid.php102
-rw-r--r--2024/PHP/day_04/Input.php30
-rw-r--r--2024/PHP/day_04/Position.php124
-rw-r--r--2024/PHP/day_04/part_01.php4
4 files changed, 132 insertions, 128 deletions
diff --git a/2024/PHP/day_04/Grid.php b/2024/PHP/day_04/Grid.php
index c0382df..75d531b 100644
--- a/2024/PHP/day_04/Grid.php
+++ b/2024/PHP/day_04/Grid.php
@@ -4,64 +4,66 @@ declare(strict_types=1);
class Grid implements Stringable
{
- /**
- * @param string[][] $data
- */
- public function __construct(public array $data) {}
+ /**
+ * @param string[][] $data
+ */
+ public function __construct(public array $data)
+ {
+ }
- public function __toString(): string
- {
- $rowToLine = fn(array $row): string => array_reduce(
- array: $row,
- callback: fn(string $acc, string $character): string => $acc . $character,
- initial: "",
- );
+ public function __toString(): string
+ {
+ $rowToLine = fn (array $row): string => array_reduce(
+ array: $row,
+ callback: fn (string $acc, string $character): string => $acc . $character,
+ initial: "",
+ );
- return array_reduce(
- array: $this->data,
- callback: fn(string $acc, array $row): string => $acc . $rowToLine($row) . PHP_EOL,
- initial: "",
- );
- }
+ return array_reduce(
+ array: $this->data,
+ callback: fn (string $acc, array $row): string => $acc . $rowToLine($row) . PHP_EOL,
+ initial: "",
+ );
+ }
- public function getCharacterAtPosition(Position $position): string
- {
- if ($this->isInBounds($position)) {
- return $this->data[$position->y][$position->x];
- }
+ public function getCharacterAtPosition(Position $position): string
+ {
+ if ($this->isInBounds($position)) {
+ return $this->data[$position->y][$position->x];
+ }
- return ".";
- }
+ return ".";
+ }
- /**
- * @param Position[] $positions
- */
- public function getStringFromPositions(array $positions): string
- {
- $characters = array_map(
- callback: fn(Position $position): string => $this->getCharacterAtPosition($position),
- array: $positions,
- );
+ /**
+ * @param Position[] $positions
+ */
+ public function getStringFromPositions(array $positions): string
+ {
+ $characters = array_map(
+ callback: fn (Position $position): string => $this->getCharacterAtPosition($position),
+ array: $positions,
+ );
- $string = array_reduce(
- array: $characters,
- callback: fn(string $acc, string $character): string => $acc . $character,
- initial: "",
- );
+ $string = array_reduce(
+ array: $characters,
+ callback: fn (string $acc, string $character): string => $acc . $character,
+ initial: "",
+ );
- return $string;
- }
+ return $string;
+ }
- public function isInBounds(Position $position): bool
- {
- if (!array_key_exists($position->y, $this->data)) {
- return false;
- }
+ public function isInBounds(Position $position): bool
+ {
+ if (!array_key_exists($position->y, $this->data)) {
+ return false;
+ }
- if (!array_key_exists($position->x, $this->data[$position->y])) {
- return false;
- }
+ if (!array_key_exists($position->x, $this->data[$position->y])) {
+ return false;
+ }
- return true;
- }
+ return true;
+ }
}
diff --git a/2024/PHP/day_04/Input.php b/2024/PHP/day_04/Input.php
index f7e03c1..60a16a0 100644
--- a/2024/PHP/day_04/Input.php
+++ b/2024/PHP/day_04/Input.php
@@ -4,22 +4,22 @@ declare(strict_types=1);
class Input
{
- public string $contents;
+ public string $contents;
- public function __construct(private string $fileName)
- {
- $contents = file_get_contents($this->fileName, use_include_path: true);
- $this->contents = $contents === false ? "" : $contents;
- }
+ public function __construct(private string $fileName)
+ {
+ $contents = file_get_contents($this->fileName, use_include_path: true);
+ $this->contents = $contents === false ? "" : $contents;
+ }
- public function toGrid(): Grid
- {
- $lines = explode("\n", $this->contents);
- $gridArray = array_map(
- callback: fn(string $line): array => str_split(trim($line)),
- array: $lines
- );
+ public function toGrid(): Grid
+ {
+ $lines = explode("\n", $this->contents);
+ $gridArray = array_map(
+ callback: fn (string $line): array => str_split(trim($line)),
+ array: $lines
+ );
- return new Grid($gridArray);
- }
+ return new Grid($gridArray);
+ }
}
diff --git a/2024/PHP/day_04/Position.php b/2024/PHP/day_04/Position.php
index 182da7e..0dcf96c 100644
--- a/2024/PHP/day_04/Position.php
+++ b/2024/PHP/day_04/Position.php
@@ -4,74 +4,76 @@ declare(strict_types=1);
class Position implements Stringable
{
- public function __construct(public $x, public $y) {}
+ public function __construct(public $x, public $y)
+ {
+ }
- public function __toString(): string
- {
- return "({$this->x}, {$this->y})";
- }
+ public function __toString(): string
+ {
+ return "({$this->x}, {$this->y})";
+ }
- /**
- * Not validated
- */
- public function getNorth(): self
- {
- return new self($this->x, $this->y - 1);
- }
+ /**
+ * Not validated
+ */
+ public function getNorth(): self
+ {
+ return new self($this->x, $this->y - 1);
+ }
- /**
- * Not validated
- */
- public function getNorthEast(): self
- {
- return new self($this->x + 1, $this->y - 1);
- }
+ /**
+ * Not validated
+ */
+ public function getNorthEast(): self
+ {
+ return new self($this->x + 1, $this->y - 1);
+ }
- /**
- * Not validated
- */
- public function getEast(): self
- {
- return new self($this->x + 1, $this->y);
- }
+ /**
+ * Not validated
+ */
+ public function getEast(): self
+ {
+ return new self($this->x + 1, $this->y);
+ }
- /**
- * Not validated
- */
- public function getSouthEast(): self
- {
- return new self($this->x + 1, $this->y + 1);
- }
+ /**
+ * Not validated
+ */
+ public function getSouthEast(): self
+ {
+ return new self($this->x + 1, $this->y + 1);
+ }
- /**
- * Not validated
- */
- public function getSouth(): self
- {
- return new self($this->x, $this->y + 1);
- }
+ /**
+ * Not validated
+ */
+ public function getSouth(): self
+ {
+ return new self($this->x, $this->y + 1);
+ }
- /**
- * Not validated
- */
- public function getSouthWest(): self
- {
- return new self($this->x - 1, $this->y + 1);
- }
+ /**
+ * Not validated
+ */
+ public function getSouthWest(): self
+ {
+ return new self($this->x - 1, $this->y + 1);
+ }
- /**
- * Not validated
- */
- public function getWest(): self
- {
- return new self($this->x - 1, $this->y);
- }
+ /**
+ * Not validated
+ */
+ public function getWest(): self
+ {
+ return new self($this->x - 1, $this->y);
+ }
- /**
- * Not validated
- */
- public function getNorthWest(): self
- {
- return new self($this->x - 1, $this->y - 1);
- }
+ /**
+ * Not validated
+ */
+ public function getNorthWest(): self
+ {
+ return new self($this->x - 1, $this->y - 1);
+ }
}
diff --git a/2024/PHP/day_04/part_01.php b/2024/PHP/day_04/part_01.php
index ae67a4f..0faa5b0 100644
--- a/2024/PHP/day_04/part_01.php
+++ b/2024/PHP/day_04/part_01.php
@@ -2,7 +2,7 @@
declare(strict_types=1);
-spl_autoload_register(fn(string $className) => require "{$className}.php");
+spl_autoload_register(fn (string $className) => require "{$className}.php");
// Assumes executed from workspace root
$input = new Input('../_input/day_04.txt');
@@ -81,7 +81,7 @@ foreach ($grid->data as $y => $row) {
$matchCount += array_reduce(
array: $possibleMatches,
- callback: fn(int $acc, string $possibleMatch): int => match ($possibleMatch) {
+ callback: fn (int $acc, string $possibleMatch): int => match ($possibleMatch) {
"XMAS" => $acc + 1,
default => $acc,
},