--- 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?
const fs = require("fs");
// const puzzleInput = fs.readFileSync("./sample_input.txt").toString().split('\n');
const puzzleInput = fs.readFileSync("./input.txt").toString().split('\n');

function findWordCount(grid, word) {
    const rows = grid.length;
    const cols = grid[0].length;
    const wordLength = word.length;
    const reverseWord = word.split('').reverse().join('');
    let count = 0;

    function checkHorizontal(row, col) {
        if (col <= cols - wordLength) {
            const horizontalWord = grid[row].slice(col, col + wordLength);
            if (horizontalWord === word || horizontalWord === reverseWord) {
count++;
                // console.log(`Found ${word} horizontally at row ${row}, starting column ${col}`);
            }
        }
    }

    function checkVertical(row, col) {
        if (row <= rows - wordLength) {
            let verticalWord = '';
            for (let i = 0; i < wordLength; i++) {
                verticalWord += grid[row + i][col];
            }
            if (verticalWord === word || verticalWord === reverseWord) {
                count++;
                // console.log(`Found ${word} vertically at column ${col}, starting row ${row}`);
            }
        }
    }

    function checkDiagonal(row, col) {
        // Diagonal down-right
        if (row <= rows - wordLength && col <= cols - wordLength) {
            let diagonalWord = '';
            for (let i = 0; i < wordLength; i++) {
                diagonalWord += grid[row + i][col + i];
            }
            if (diagonalWord === word || diagonalWord === reverseWord) {
count++;
                // console.log(`Found ${word} diagonally (down-right) starting at row ${row}, column ${col}`);
            }
        }

        // Diagonal down-left
        if (row <= rows - wordLength && col >= wordLength - 1) {
            let diagonalWord = '';
            for (let i = 0; i < wordLength; i++) {
                diagonalWord += grid[row + i][col - i];
            }
            if (diagonalWord === word || diagonalWord === reverseWord) {
count++;
                // console.log(`Found ${word} diagonally (down-left) starting at row ${row}, column ${col}`);
            }
        }
    }

    for (let r = 0; r < rows; r++) {
        for (let c = 0; c < cols; c++) {
            checkHorizontal(r, c);
            checkVertical(r, c);
            checkDiagonal(r, c);
        }
    }
  return count;
}

const wordCount = findWordCount(puzzleInput, "XMAS");
console.log(`Part 1: ${wordCount}`);