From 6860b6e83f2c6be5e59253105cf397c5b27b1590 Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Mon, 3 Mar 2025 01:14:16 +0100 Subject: feat: Move 2024 php files --- 2024/OCaml/lib/day_04/php/Grid.php | 67 ------------------------ 2024/OCaml/lib/day_04/php/Input.php | 25 --------- 2024/OCaml/lib/day_04/php/Position.php | 77 ---------------------------- 2024/OCaml/lib/day_04/php/part_01.php | 94 ---------------------------------- 2024/PHP/day_04/Grid.php | 67 ++++++++++++++++++++++++ 2024/PHP/day_04/Input.php | 25 +++++++++ 2024/PHP/day_04/Position.php | 77 ++++++++++++++++++++++++++++ 2024/PHP/day_04/part_01.php | 94 ++++++++++++++++++++++++++++++++++ 8 files changed, 263 insertions(+), 263 deletions(-) delete mode 100644 2024/OCaml/lib/day_04/php/Grid.php delete mode 100644 2024/OCaml/lib/day_04/php/Input.php delete mode 100644 2024/OCaml/lib/day_04/php/Position.php delete mode 100644 2024/OCaml/lib/day_04/php/part_01.php create mode 100644 2024/PHP/day_04/Grid.php create mode 100644 2024/PHP/day_04/Input.php create mode 100644 2024/PHP/day_04/Position.php create mode 100644 2024/PHP/day_04/part_01.php diff --git a/2024/OCaml/lib/day_04/php/Grid.php b/2024/OCaml/lib/day_04/php/Grid.php deleted file mode 100644 index c0382df..0000000 --- a/2024/OCaml/lib/day_04/php/Grid.php +++ /dev/null @@ -1,67 +0,0 @@ - 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: "", - ); - } - - public function getCharacterAtPosition(Position $position): string - { - if ($this->isInBounds($position)) { - return $this->data[$position->y][$position->x]; - } - - return "."; - } - - /** - * @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: "", - ); - - return $string; - } - - 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; - } - - return true; - } -} diff --git a/2024/OCaml/lib/day_04/php/Input.php b/2024/OCaml/lib/day_04/php/Input.php deleted file mode 100644 index f7e03c1..0000000 --- a/2024/OCaml/lib/day_04/php/Input.php +++ /dev/null @@ -1,25 +0,0 @@ -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 - ); - - return new Grid($gridArray); - } -} diff --git a/2024/OCaml/lib/day_04/php/Position.php b/2024/OCaml/lib/day_04/php/Position.php deleted file mode 100644 index 182da7e..0000000 --- a/2024/OCaml/lib/day_04/php/Position.php +++ /dev/null @@ -1,77 +0,0 @@ -x}, {$this->y})"; - } - - /** - * 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 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 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 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); - } -} diff --git a/2024/OCaml/lib/day_04/php/part_01.php b/2024/OCaml/lib/day_04/php/part_01.php deleted file mode 100644 index 250da0c..0000000 --- a/2024/OCaml/lib/day_04/php/part_01.php +++ /dev/null @@ -1,94 +0,0 @@ - require "{$className}.php"); - -// Assumes executed from workspace root -$input = new Input('input/day_04.txt'); -if ($input->contents === "") { - throw new RuntimeException("File not found"); -} - -$grid = $input->toGrid(); - -$matchCount = 0; -foreach ($grid->data as $y => $row) { - foreach ($row as $x => $char) { - if ($char !== 'X') { - continue; - } - - $start = new Position($x, $y); - - $possibleMatches = []; - - $possibleMatches["north"] = $grid->getStringFromPositions([ - $start, - $start->getNorth(), - $start->getNorth()->getNorth(), - $start->getNorth()->getNorth()->getNorth(), - ]); - - $possibleMatches["east"] = $grid->getStringFromPositions([ - $start, - $start->getEast(), - $start->getEast()->getEast(), - $start->getEast()->getEast()->getEast(), - ]); - - $possibleMatches["south"] = $grid->getStringFromPositions([ - $start, - $start->getSouth(), - $start->getSouth()->getSouth(), - $start->getSouth()->getSouth()->getSouth(), - ]); - - $possibleMatches["west"] = $grid->getStringFromPositions([ - $start, - $start->getWest(), - $start->getWest()->getWest(), - $start->getWest()->getWest()->getWest(), - ]); - - $possibleMatches["north-east"] = $grid->getStringFromPositions([ - $start, - $start->getNorthEast(), - $start->getNorthEast()->getNorthEast(), - $start->getNorthEast()->getNorthEast()->getNorthEast(), - ]); - - $possibleMatches["north-west"] = $grid->getStringFromPositions([ - $start, - $start->getNorthWest(), - $start->getNorthWest()->getNorthWest(), - $start->getNorthWest()->getNorthWest()->getNorthWest(), - ]); - - $possibleMatches["south-east"] = $grid->getStringFromPositions([ - $start, - $start->getSouthEast(), - $start->getSouthEast()->getSouthEast(), - $start->getSouthEast()->getSouthEast()->getSouthEast(), - ]); - - $possibleMatches["south-west"] = $grid->getStringFromPositions([ - $start, - $start->getSouthWest(), - $start->getSouthWest()->getSouthWest(), - $start->getSouthWest()->getSouthWest()->getSouthWest(), - ]); - - $matchCount += array_reduce( - array: $possibleMatches, - callback: fn(int $acc, string $possibleMatch): int => match ($possibleMatch) { - "XMAS" => $acc + 1, - default => $acc, - }, - initial: 0, - ); - } -} - -// ANSWER: 2517 -echo $matchCount; diff --git a/2024/PHP/day_04/Grid.php b/2024/PHP/day_04/Grid.php new file mode 100644 index 0000000..c0382df --- /dev/null +++ b/2024/PHP/day_04/Grid.php @@ -0,0 +1,67 @@ + 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: "", + ); + } + + public function getCharacterAtPosition(Position $position): string + { + if ($this->isInBounds($position)) { + return $this->data[$position->y][$position->x]; + } + + return "."; + } + + /** + * @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: "", + ); + + return $string; + } + + 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; + } + + return true; + } +} diff --git a/2024/PHP/day_04/Input.php b/2024/PHP/day_04/Input.php new file mode 100644 index 0000000..f7e03c1 --- /dev/null +++ b/2024/PHP/day_04/Input.php @@ -0,0 +1,25 @@ +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 + ); + + return new Grid($gridArray); + } +} diff --git a/2024/PHP/day_04/Position.php b/2024/PHP/day_04/Position.php new file mode 100644 index 0000000..182da7e --- /dev/null +++ b/2024/PHP/day_04/Position.php @@ -0,0 +1,77 @@ +x}, {$this->y})"; + } + + /** + * 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 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 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 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); + } +} diff --git a/2024/PHP/day_04/part_01.php b/2024/PHP/day_04/part_01.php new file mode 100644 index 0000000..250da0c --- /dev/null +++ b/2024/PHP/day_04/part_01.php @@ -0,0 +1,94 @@ + require "{$className}.php"); + +// Assumes executed from workspace root +$input = new Input('input/day_04.txt'); +if ($input->contents === "") { + throw new RuntimeException("File not found"); +} + +$grid = $input->toGrid(); + +$matchCount = 0; +foreach ($grid->data as $y => $row) { + foreach ($row as $x => $char) { + if ($char !== 'X') { + continue; + } + + $start = new Position($x, $y); + + $possibleMatches = []; + + $possibleMatches["north"] = $grid->getStringFromPositions([ + $start, + $start->getNorth(), + $start->getNorth()->getNorth(), + $start->getNorth()->getNorth()->getNorth(), + ]); + + $possibleMatches["east"] = $grid->getStringFromPositions([ + $start, + $start->getEast(), + $start->getEast()->getEast(), + $start->getEast()->getEast()->getEast(), + ]); + + $possibleMatches["south"] = $grid->getStringFromPositions([ + $start, + $start->getSouth(), + $start->getSouth()->getSouth(), + $start->getSouth()->getSouth()->getSouth(), + ]); + + $possibleMatches["west"] = $grid->getStringFromPositions([ + $start, + $start->getWest(), + $start->getWest()->getWest(), + $start->getWest()->getWest()->getWest(), + ]); + + $possibleMatches["north-east"] = $grid->getStringFromPositions([ + $start, + $start->getNorthEast(), + $start->getNorthEast()->getNorthEast(), + $start->getNorthEast()->getNorthEast()->getNorthEast(), + ]); + + $possibleMatches["north-west"] = $grid->getStringFromPositions([ + $start, + $start->getNorthWest(), + $start->getNorthWest()->getNorthWest(), + $start->getNorthWest()->getNorthWest()->getNorthWest(), + ]); + + $possibleMatches["south-east"] = $grid->getStringFromPositions([ + $start, + $start->getSouthEast(), + $start->getSouthEast()->getSouthEast(), + $start->getSouthEast()->getSouthEast()->getSouthEast(), + ]); + + $possibleMatches["south-west"] = $grid->getStringFromPositions([ + $start, + $start->getSouthWest(), + $start->getSouthWest()->getSouthWest(), + $start->getSouthWest()->getSouthWest()->getSouthWest(), + ]); + + $matchCount += array_reduce( + array: $possibleMatches, + callback: fn(int $acc, string $possibleMatch): int => match ($possibleMatch) { + "XMAS" => $acc + 1, + default => $acc, + }, + initial: 0, + ); + } +} + +// ANSWER: 2517 +echo $matchCount; -- cgit v1.2.3