--- Day 16: Reindeer Maze ---

It's time again for the Reindeer Olympics! This year, the big event is the Reindeer Maze, where the Reindeer compete for the lowest score.

You and The Historians arrive to search for the Chief right as the event is about to start. It wouldn't hurt to watch a little, right?

The Reindeer start on the Start Tile (marked S) facing East and need to reach the End Tile (marked E). They can move forward one tile at a time (increasing their score by 1 point), but never into a wall (#). They can also rotate clockwise or counterclockwise 90 degrees at a time (increasing their score by 1000 points).

To figure out the best place to sit, you start by grabbing a map (your puzzle input) from a nearby kiosk. For example:

###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
There are many paths through this maze, but taking any of the best paths would incur a score of only 7036. This can be achieved by taking a total of 36 steps forward and turning 90 degrees a total of 7 times:


###############
#.......#....E#
#.#.###.#.###^#
#.....#.#...#^#
#.###.#####.#^#
#.#.#.......#^#
#.#.#####.###^#
#..>>>>>>>>v#^#
###^#.#####v#^#
#>>^#.....#v#^#
#^#.#.###.#v#^#
#^....#...#v#^#
#^###.#.#.#v#^#
#S..#.....#>>^#
###############
Here's a second example:

#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################
In this maze, the best paths cost 11048 points; following one such path would look like this:

#################
#...#...#...#..E#
#.#.#.#.#.#.#.#^#
#.#.#.#...#...#^#
#.#.#.#.###.#.#^#
#>>v#.#.#.....#^#
#^#v#.#.#.#####^#
#^#v..#.#.#>>>>^#
#^#v#####.#^###.#
#^#v#..>>>>^#...#
#^#v###^#####.###
#^#v#>>^#.....#.#
#^#v#^#####.###.#
#^#v#^........#.#
#^#v#^#########.#
#S#>>^..........#
#################
Note that the path shown above includes one 90 degree turn as the very first move, rotating the Reindeer from facing East to facing North.

Analyze your map carefully. What is the lowest score a Reindeer could possibly get?
const fs = require('fs');

// Get the lowest score from the maze
function getLowestScore(maze) {
    const m = maze.length;
    const n = maze[0].length;
    let start = [-1, -1];
    let end = [-1, -1];

    // Find start ('S') and end ('E') positions
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (maze[i][j] === 'S') {
                start = [i, j];
            } else if (maze[i][j] === 'E') {
                end = [i, j];
            }
        }
    }

    maze[end[0]][end[1]] = '.';  // Mark end as empty

    const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
    const visited = new Set();
    const heap = [{ score: 0, dI: 0, i: start[0], j: start[1] }];  // Min-heap

    while (heap.length > 0) {
        // Sort heap by score (min-heap)
        heap.sort((a, b) => a.score - b.score);
        const { score, dI, i, j } = heap.shift();

        if (i === end[0] && j === end[1]) {
            return score;  // Found the end, return the score
        }

        if (visited.has(`${dI},${i},${j}`)) {
            continue;
        }

        visited.add(`${dI},${i},${j}`);

        // Move forward in the current direction
        const x = i + dirs[dI][0];
        const y = j + dirs[dI][1];

        if (maze[x] && maze[x][y] === '.' && !visited.has(`${dI},${x},${y}`)) {
            heap.push({ score: score + 1, dI, i: x, j: y });
        }

        // Turn left
        const left = (dI - 1 + 4) % 4;
        if (!visited.has(`${left},${i},${j}`)) {
            heap.push({ score: score + 1000, dI: left, i, j });
        }

        // Turn right
        const right = (dI + 1) % 4;
        if (!visited.has(`${right},${i},${j}`)) {
            heap.push({ score: score + 1000, dI: right, i, j });
        }
    }

    return -1;  // Return -1 if no path is found
}

// Main function
const filePath = 'input.txt';
fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading the file:', err);
        return;
    }

    const maze = data.split('\n').map(line => line.trim().split(''));
    const lowestScore = getLowestScore(maze);
    console.log("Lowest Score:", lowestScore);
});