--- Day 8: Resonant Collinearity ---

You find yourselves on the roof of a top-secret Easter Bunny installation.

While The Historians do their thing, you take a look at the familiar huge antenna. Much to your surprise, it seems to have been reconfigured to emit a signal that makes people 0.1% more likely to buy Easter Bunny brand Imitation Mediocre Chocolate as a Christmas gift! Unthinkable!

Scanning across the city, you find that there are actually many such antennas. Each antenna is tuned to a specific frequency indicated by a single lowercase letter, uppercase letter, or digit. You create a map (your puzzle input) of these antennas. For example:

............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
The signal only applies its nefarious effect at specific antinodes based on the resonant frequencies of the antennas. In particular, an antinode occurs at any point that is perfectly in line with two antennas of the same frequency - but only when one of the antennas is twice as far away as the other. This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them.

So, for these two antennas with frequency a, they create the two antinodes marked with #:

..........
...#......
..........
....a.....
..........
.....a....
..........
......#...
..........
..........
Adding a third antenna with the same frequency creates several more antinodes. It would ideally add four antinodes, but two are off the right side of the map, so instead it adds only two:

..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......#...
..........
..........
Antennas with different frequencies don't create antinodes; A and a count as different frequencies. However, antinodes can occur at locations that contain antennas. In this diagram, the lone antenna with frequency capital A creates no antinodes but has a lowercase-a-frequency antinode at its location:

..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......A...
..........
..........
The first example has antennas with two different frequencies, so the antinodes they create look like this, plus an antinode overlapping the topmost A-frequency antenna:

......#....#
...#....0...
....#0....#.
..#....0....
....0....#..
.#....A.....
...#........
#......#....
........A...
.........A..
..........#.
..........#.
Because the topmost A-frequency antenna overlaps with a 0-frequency antinode, there are 14 total unique locations that contain an antinode within the bounds of the map.

Calculate the impact of the signal. How many unique locations within the bounds of the map contain an antinode?
const fs = require('fs');

// Read input file and split into lines
const input = fs.readFileSync('input.txt', 'utf8').trim().split('\n');

// Parse the map into a grid of characters
const grid = input.map(line => line.split(''));

// Function to calculate the greatest common divisor (GCD) of two numbers
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));

// Function to calculate all unique antinode positions
const calculateAntinodes = (grid) => {
    const antinodes = new Set();
    const rows = grid.length;
    const cols = grid[0].length;

    // Iterate over all pairs of antennas
    for (let i = 0; i < rows; i++) {
        for (let j = 0; j < cols; j++) {
            const frequency1 = grid[i][j];
            if (frequency1 === '.') continue; // Skip empty spaces

            for (let k = 0; k < rows; k++) {
                for (let l = 0; l < cols; l++) {
                    const frequency2 = grid[k][l];
                    if (frequency2 !== frequency1 || (i === k && j === l)) continue; // Skip same antenna or different frequencies

                    // Calculate the differences in positions
                    const dx = k - i;
                    const dy = l - j;

                    // Calculate the GCD to simplify the direction vector
                    const divisor = gcd(Math.abs(dx), Math.abs(dy));
                    const stepX = dx / divisor;
                    const stepY = dy / divisor;

                    // Calculate the two antinode positions
                    const antinode1X = i - stepX;
                    const antinode1Y = j - stepY;
                    const antinode2X = k + stepX;
                    const antinode2Y = l + stepY;

                    // Check if the antinodes are within the grid bounds
                    if (antinode1X >= 0 && antinode1X < rows && antinode1Y >= 0 && antinode1Y < cols) {
                        antinodes.add(`${antinode1X},${antinode1Y}`);
                    }
                    if (antinode2X >= 0 && antinode2X < rows && antinode2Y >= 0 && antinode2Y < cols) {
                        antinodes.add(`${antinode2X},${antinode2Y}`);
                    }
                }
            }
        }
    }

    return antinodes.size;
};

// Calculate and print the number of unique antinode locations
console.log(calculateAntinodes(grid));