--- 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 "node:fs";

const a = fs.readFileSync("./input.txt", "utf8").toString().split("\n");
const data = a.map((item) => item.split(""));

const values = {
  horizontalLR: 0,
  horizontalRL: 0,
  verticalTB: 0,
  verticalBT: 0,
  diagonalTLtoBR: 0,
  diagonalBRtoTL: 0,
  diagonalTRtoBL: 0,
  diagonalBLtoTR: 0,
};

data.forEach((item, itemIdx) => {
  item.forEach((child, childIdx) => {
    // Horizontal (left to right)
    if (child === "X") {
      if (data[itemIdx][childIdx + 1] === "M") {
        if (data[itemIdx][childIdx + 2] === "A") {
          if (data[itemIdx][childIdx + 3] === "S") values.horizontalLR += 1;
        }
      }

      // Descending Diagonal (center to bottom right)
      if (data[itemIdx + 1][childIdx + 1] === "M") {
        if (data[itemIdx + 2][childIdx + 2] === "A") {
          if (data[itemIdx + 3][childIdx + 3] === "S")
            values.diagonalTLtoBR += 1;
        }
      }

      // Descending Vertical (center to bottom)
      if (data[itemIdx + 1][childIdx] === "M") {
        if (data[itemIdx + 2][childIdx] === "A") {
          if (data[itemIdx + 3][childIdx] === "S") values.verticalTB += 1;
        }
      }

      // Descending diagonal (center to bottom left)
      if (childIdx >= 3) {
        if (data[itemIdx + 1][childIdx - 1] === "M") {
          if (data[itemIdx + 2][childIdx - 2] === "A") {
            if (data[itemIdx + 3][childIdx - 3] === "S")
              values.diagonalTRtoBL += 1;
          }
        }
      }

      // Horizontal (right to left)
      if (childIdx >= 3) {
        if (data[itemIdx][childIdx - 1] === "M") {
          if (data[itemIdx][childIdx - 2] === "A") {
            if (data[itemIdx][childIdx - 3] === "S") values.horizontalRL += 1;
          }
        }
      }

      // ascending diagonal (center to top left)
      if (itemIdx >= 3 && childIdx >= 3) {
        if (data[itemIdx - 1][childIdx - 1] === "M") {
          if (data[itemIdx - 2][childIdx - 2] === "A") {
            if (data[itemIdx - 3][childIdx - 3] === "S")
              values.diagonalBRtoTL += 1;
          }
        }
      }

      // ascending vertical (center to top)
      if (itemIdx >= 3) {
        if (data[itemIdx - 1][childIdx] === "M") {
          if (data[itemIdx - 2][childIdx] === "A") {
            if (data[itemIdx - 3][childIdx] === "S") values.verticalBT += 1;
          }
        }
      }

      // ascending diagonal (center to top right)
      if (itemIdx >= 3) {
        if (data[itemIdx - 1][childIdx + 1] === "M") {
          if (data[itemIdx - 2][childIdx + 2] === "A") {
            if (data[itemIdx - 3][childIdx + 3] === "S")
              values.diagonalBLtoTR += 1;
          }
        }
      }
    }
  });
});

console.log(values);
let count = 0;
Object.values(values).forEach((item) => {
  count += item;
});
console.log(count);