summaryrefslogtreecommitdiff
path: root/2024/php/day_04/part_01.php
blob: 0faa5b095760c3a3f345331c46852b64ba4cd0d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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;