--- Day 4: Ceres Search ---

"Looks like the Chief's not here. Next!" One of The Historians pulls out a device and pushes the only button on it. After a brief flash, you recognize the interior of the Ceres monitoring station!

As the search for the Chief continues, a small Elf who lives on the station tugs on your shirt; she'd like to know if you could help her with her word search (your puzzle input). She only has to find one word: XMAS.

This word search allows words to be horizontal, vertical, diagonal, written backwards, or even overlapping other words. It's a little unusual, though, as you don't merely need to find one instance of XMAS - you need to find all of them. Here are a few ways XMAS might appear, where irrelevant characters have been replaced with .:

..X...
.SAMX.
.A..A.
XMAS.S
.X....
The actual word search will be full of letters instead. For example:

MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
In this word search, XMAS occurs a total of 18 times; here's the same word search again, but where letters not involved in any XMAS have been replaced with .:

....XXMAS.
.SAMXMS...
...S..A...
..A.A.MS.X
XMASAMX.MM
X.....XA.A
S.S.S.S.SS
.A.A.A.A.A
..M.M.M.MM
.X.X.XMASX
Take a look at the little Elf's word search. How many times does XMAS appear?
import fs from "fs";

const countXmasOccurrences = (inputFilePath) => {
  const xmasMatrix = [];

  try {
    const text = fs.readFileSync(inputFilePath, "utf8");
    if (!text) return 0;

    const rows = text.trim().split("\n");

    for (const row of rows) {
      xmasMatrix.push(row.split(""));
    }

    let xmasCounter = 0;

    for (let i = 0; i < xmasMatrix.length; i++) {
      for (let j = 0; j < xmasMatrix[i].length; j++) {
        const currentChar = xmasMatrix[i][j];

        if (xmasMatrix?.[i - 3]?.[j - 3]) {
          const topLeftDiagonal =
            currentChar +
            xmasMatrix[i - 1][j - 1] +
            xmasMatrix[i - 2][j - 2] +
            xmasMatrix[i - 3][j - 3];

          if (topLeftDiagonal === "XMAS") xmasCounter++;
        }

        if (xmasMatrix?.[i - 3]?.[j + 3]) {
          const topRightDiagonal =
            currentChar +
            xmasMatrix[i - 1][j + 1] +
            xmasMatrix[i - 2][j + 2] +
            xmasMatrix[i - 3][j + 3];

          if (topRightDiagonal === "XMAS") xmasCounter++;
        }

        if (xmasMatrix?.[i + 3]?.[j - 3]) {
          const botLeftDiagonal =
            currentChar +
            xmasMatrix[i + 1][j - 1] +
            xmasMatrix[i + 2][j - 2] +
            xmasMatrix[i + 3][j - 3];

          if (botLeftDiagonal === "XMAS") xmasCounter++;
        }

        if (xmasMatrix?.[i + 3]?.[j + 3]) {
          const botRightDiagonal =
            currentChar +
            xmasMatrix[i + 1][j + 1] +
            xmasMatrix[i + 2][j + 2] +
            xmasMatrix[i + 3][j + 3];

          if (botRightDiagonal === "XMAS") xmasCounter++;
        }

        if (xmasMatrix[i]?.[j - 3]) {
          const left =
            currentChar +
            xmasMatrix[i][j - 1] +
            xmasMatrix[i][j - 2] +
            xmasMatrix[i][j - 3];

          if (left === "XMAS") xmasCounter++;
        }

        if (xmasMatrix[i]?.[j + 3]) {
          const right =
            currentChar +
            xmasMatrix[i][j + 1] +
            xmasMatrix[i][j + 2] +
            xmasMatrix[i][j + 3];

          if (right === "XMAS") xmasCounter++;
        }

        if (xmasMatrix?.[i - 3]?.[j]) {
          const top =
            currentChar +
            xmasMatrix[i - 1][j] +
            xmasMatrix[i - 2][j] +
            xmasMatrix[i - 3][j];

          if (top === "XMAS") xmasCounter++;
        }

        if (xmasMatrix?.[i + 3]?.[j]) {
          const bot =
            currentChar +
            xmasMatrix[i + 1][j] +
            xmasMatrix[i + 2][j] +
            xmasMatrix[i + 3][j];

          if (bot === "XMAS") xmasCounter++;
        }
      }
    }
    return xmasCounter;
  } catch (err) {
    console.error(err);
  }
};

const totalXmasOccurrences = countXmasOccurrences("./puzzle-input.txt");
console.log(totalXmasOccurrences);