Light Clean up
This commit is contained in:
1 parent
69bf933f03
commit
77c5f531f0
11 files changed
+403
-2394
No files matched your search
@@ -1,164 +1,295 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
"cells": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"344"
|
||||
"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"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"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(file_path)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1182"
|
||||
"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"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
},
|
||||
{
|
||||
"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": "iVBORw0KGgoAAAANSUhEUgAAAwMAAAMkCAYAAADki/ytAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA5E0lEQVR4nO3df7DV9X0n/ucB9YLKRUXlR70S4pr4A4k/SxHrmEpDjbo6dW1NyK41GW0SjAKbROmoqFFRZ2oZoxF1U01npSbdxCTtfNVRWrGuoqih6mQXxdhKokCaiVzBA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A7jDAAAACFMiYEAEDtNDMkzYr93ttuQgAAQGUIAwAAUChjQgAA1E5fq5G+VqPTZQxQtXoSnQEAACiWMAAAAIXabmHg5ptvzkc+8pEMGzYskydPzhNPPLG9TgUAAAP0ZUglV9Vsl4q++93vZs6cOZk3b16efvrpfOITn8j06dOzdu3a7XE6AADgfdguYeCGG27Iueeem3POOSeHHHJIFi5cmF133TV/+Zd/uT1OBwAAvA+DvpvQm2++maeeeipz587tPzZkyJBMmzYtjz322Bav37RpUzZt2tT/uLe3d7BLAgCgMM3WkDRb1RrLabYKuOnYv/7rv6avry+jR48ecHz06NFZvXr1Fq+fP39+Ro4c2b96enoGuyQAAGArOh6X5s6dm3Xr1vWvVatWdbokAAAowqCPCe29994ZOnRo1qxZM+D4mjVrMmbMmC1e39XVla6ursEuAwCAglVx956+FDAmtMsuu+Soo47K4sWL+481m80sXrw4U6ZMGezTAQAA79OgdwaSZM6cOTn77LNz9NFH57d/+7ezYMGCbNiwIeecc872OB0AAPA+bJcw8Md//Mf55S9/mcsuuyyrV6/O4Ycfnvvuu2+Li4oBAGB7aCbpazU6XcYAzU4XsBXbJQwkyfnnn5/zzz9/e709AADwAVXrqgoAAGCH2W6dAQAA6JRmhqRZsd97V62eRGcAAACKJQwAAEChjAkBAFA7fa0h6WtV6/feVasn0RkAAIBiCQMAAFAoY0IAANROM400U7WbjlWrnkRnAAAAiiUMAABAoYwJAQBQO3YTak/1KgIAAHYIYQAAAAplTAgAgNrpy5D0Vez33lWrJ9EZAACAYgkDAABQKGEAAIDaabYalVzb4he/+EU+97nPZdSoURk+fHgOO+ywPPnkk/3Pt1qtXHbZZRk7dmyGDx+eadOm5YUXXtimcwgDAABQMb/+9a8zderU7Lzzzrn33nvz05/+NH/+53+ePffcs/81119/fW688cYsXLgwjz/+eHbbbbdMnz49GzdubPs8LiAGAICKue6669LT05M77rij/9iECRP6/9xqtbJgwYJccsklOe2005Ikf/VXf5XRo0fnhz/8Yc4666y2ziMMULTRNz7a6RIG1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6dAQAAKJQwAAAAhTImBABA7bRaQ9JsVev33q2K1ZPoDAAAQLGEAQAAKJQxIQAAaqcvjfSlWrv3VK2eRGcAAACKJQwAAEChjAlRtDUXHNvpEgbV6Bsf7XQJg+rly+rz+XS91ukK+E18PtVVt+9rdfvvTpU1W9W7yVez1ekKtqQzAAAAhRIGAACgUMaEAAConWYFbLine truncated
|
||||
},
|
||||
"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": []
|
||||
}
|
||||
],
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
Reference in new issue
Block a user