{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xgruaa0X9m1U",
        "outputId": "374ce0d2-a69a-4e08-d15e-3c85b2710ad9"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "344"
            ]
          },
          "metadata": {},
          "execution_count": 2
        }
      ],
      "source": [
        "# Load the larger test file and process it using the solution\n",
        "file_path = 'input'\n",
        "\n",
        "def process_large_file(file_path):\n",
        "    with open(file_path, 'r') as file:\n",
        "        lines = [line.rstrip('\\n') for line in file.readlines()]\n",
        "\n",
        "    height = len(lines)\n",
        "    width = len(lines[0]) if height > 0 else 0\n",
        "\n",
        "    # Identify antennas and their frequencies\n",
        "    freq_map = {}\n",
        "    for y in range(height):\n",
        "        for x in range(width):\n",
        "            c = lines[y][x]\n",
        "            if c != '.':\n",
        "                if c not in freq_map:\n",
        "                    freq_map[c] = []\n",
        "                freq_map[c].append((x, y))\n",
        "\n",
        "    # A set to hold all unique antinode locations\n",
        "    antinodes = set()\n",
        "\n",
        "    # Candidate λ values based on derived equations\n",
        "    lambdas = [2, -1, 1/3, 2/3]\n",
        "\n",
        "    for freq, antennas in freq_map.items():\n",
        "        n = len(antennas)\n",
        "        if n < 2:\n",
        "            continue\n",
        "\n",
        "        for i in range(n):\n",
        "            for j in range(i+1, n):\n",
        "                x1, y1 = antennas[i]\n",
        "                x2, y2 = antennas[j]\n",
        "                dx = x2 - x1\n",
        "                dy = y2 - y1\n",
        "\n",
        "                for lam in lambdas:\n",
        "                    px = x1 + lam * dx\n",
        "                    py = y1 + lam * dy\n",
        "\n",
        "                    if abs(px - round(px)) < 1e-12 and abs(py - round(py)) < 1e-12:\n",
        "                        rx = round(px)\n",
        "                        ry = round(py)\n",
        "\n",
        "                        if 0 <= rx < width and 0 <= ry < height:\n",
        "                            antinodes.add((rx, ry))\n",
        "\n",
        "    # Output the number of unique antinode locations\n",
        "    return len(antinodes)\n",
        "\n",
        "process_large_file('input')\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RvPJM8km9m1W",
        "outputId": "77731738-0776-46da-e8e5-4faf9e334515"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1182"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ],
      "source": [
        "def process_part_two(file_path):\n",
        "    with open(file_path, 'r') as file:\n",
        "        lines = [line.rstrip('\\n') for line in file.readlines()]\n",
        "\n",
        "    height = len(lines)\n",
        "    width = len(lines[0]) if height > 0 else 0\n",
        "\n",
        "    # Identify antennas and their frequencies\n",
        "    freq_map = {}\n",
        "    for y in range(height):\n",
        "        for x in range(width):\n",
        "            c = lines[y][x]\n",
        "            if c != '.':\n",
        "                if c not in freq_map:\n",
        "                    freq_map[c] = []\n",
        "                freq_map[c].append((x, y))\n",
        "\n",
        "    # A set to hold all unique antinode locations\n",
        "    antinodes = set()\n",
        "\n",
        "    # For each frequency group, consider all pairs of antennas\n",
        "    for freq, antennas in freq_map.items():\n",
        "        n = len(antennas)\n",
        "        if n < 2:\n",
        "            continue\n",
        "\n",
        "        for i in range(n):\n",
        "            for j in range(i + 1, n):\n",
        "                x1, y1 = antennas[i]\n",
        "                x2, y2 = antennas[j]\n",
        "\n",
        "                # Calculate the collinearity condition\n",
        "                for x in range(width):\n",
        "                    for y in range(height):\n",
        "                        if (x2 - x1) * (y - y1) == (y2 - y1) * (x - x1):\n",
        "                            antinodes.add((x, y))\n",
        "\n",
        "    # Include the positions of all antennas as antinodes\n",
        "    for freq, antennas in freq_map.items():\n",
        "        for x, y in antennas:\n",
        "            antinodes.add((x, y))\n",
        "\n",
        "    # Return the number of unique antinode locations\n",
        "    return len(antinodes)\n",
        "\n",
        "\n",
        "# Process the larger test file for part two\n",
        "process_part_two('input')\n"
      ]
    },
    {
      "source": [
        "# prompt: visualize the map\n",
        "\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "# Load the larger test file and process it using the solution\n",
        "file_path = 'input'\n",
        "\n",
        "def process_large_file(file_path):\n",
        "    with open(file_path, 'r') as file:\n",
        "        lines = [line.rstrip('\\n') for line in file.readlines()]\n",
        "\n",
        "    height = len(lines)\n",
        "    width = len(lines[0]) if height > 0 else 0\n",
        "\n",
        "    # Identify antennas and their frequencies\n",
        "    freq_map = {}\n",
        "    for y in range(height):\n",
        "        for x in range(width):\n",
        "            c = lines[y][x]\n",
        "            if c != '.':\n",
        "                if c not in freq_map:\n",
        "                    freq_map[c] = []\n",
        "                freq_map[c].append((x, y))\n",
        "\n",
        "    # A set to hold all unique antinode locations\n",
        "    antinodes = set()\n",
        "\n",
        "    # Candidate λ values based on derived equations\n",
        "    lambdas = [2, -1, 1/3, 2/3]\n",
        "\n",
        "    for freq, antennas in freq_map.items():\n",
        "        n = len(antennas)\n",
        "        if n < 2:\n",
        "            continue\n",
        "\n",
        "        for i in range(n):\n",
        "            for j in range(i+1, n):\n",
        "                x1, y1 = antennas[i]\n",
        "                x2, y2 = antennas[j]\n",
        "                dx = x2 - x1\n",
        "                dy = y2 - y1\n",
        "\n",
        "                for lam in lambdas:\n",
        "                    px = x1 + lam * dx\n",
        "                    py = y1 + lam * dy\n",
        "\n",
        "                    if abs(px - round(px)) < 1e-12 and abs(py - round(py)) < 1e-12:\n",
        "                        rx = round(px)\n",
        "                        ry = round(py)\n",
        "\n",
        "                        if 0 <= rx < width and 0 <= ry < height:\n",
        "                            antinodes.add((rx, ry))\n",
        "    # The line below was previously commented out and caused the error\n",
        "    # because the variable 'antinodes' was not defined.\n",
        "    # It has been uncommented and restored to its original functionality.\n",
        "    return antinodes, width, height, lines # Return antinodes and dimensions\n",
        "\n",
        "\n",
        "def visualize_map(antinodes, width, height, lines):\n",
        "    # Create a 2D array to represent the map\n",
        "    map_array = [['.' for _ in range(width)] for _ in range(height)]\n",
        "\n",
        "    # Place antennas on the map\n",
        "    for y in range(height):\n",
        "      for x in range(width):\n",
        "        map_array[y][x] = lines[y][x]\n",
        "\n",
        "    # Mark antinodes on the map\n",
        "    for x, y in antinodes:\n",
        "        map_array[y][x] = '#'\n",
        "\n",
        "    # Display the map using matplotlib\n",
        "    plt.figure(figsize=(10, 10))  # Adjust figure size as needed\n",
        "    plt.imshow([[ord(c) for c in row] for row in map_array], cmap='viridis')\n",
        "    plt.colorbar()\n",
        "    plt.show()\n",
        "\n",
        "\n",
        "# Call the function and get the results\n",
        "antinodes, width, height, lines = process_large_file('testinput')\n",
        "visualize_map(antinodes, width, height, lines)"
      ],
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 821
        },
        "id": "TbsEuRwx-giw",
        "outputId": "2b2f61ab-c2b5-40e5-bddc-2c191f6529aa"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 1000x1000 with 2 Axes>"
            ],
            "image/png": "[encoded data, 19680 characters]\n"
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "yqLLsSep_F_j"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.8"
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}