summaryrefslogtreecommitdiff
path: root/2024/OCaml/lib/day_04/php/part_01.php
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2025-03-03 00:56:33 +0100
committerDJ O'Leary <dijitol@proton.me>2025-03-03 00:56:33 +0100
commite24effa8fd3574e9d396be0e1a47f101c27c0a60 (patch)
tree793896d20384facd10d925e09f8f0dd89c930cce /2024/OCaml/lib/day_04/php/part_01.php
perf: Merge Advent of Code year specific repos
Diffstat (limited to '2024/OCaml/lib/day_04/php/part_01.php')
-rw-r--r--2024/OCaml/lib/day_04/php/part_01.php94
1 files changed, 94 insertions, 0 deletions
diff --git a/2024/OCaml/lib/day_04/php/part_01.php b/2024/OCaml/lib/day_04/php/part_01.php
new file mode 100644
index 0000000..250da0c
--- /dev/null
+++ b/2024/OCaml/lib/day_04/php/part_01.php
@@ -0,0 +1,94 @@
+<?php
+
+declare(strict_types=1);
+
+spl_autoload_register(fn(string $className) => 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;