Light Clean up
This commit is contained in:
1 parent
69bf933f03
commit
77c5f531f0
11 files changed
+403
-2394
No files matched your search
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"cells": [],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
+28
-72
@@ -2,21 +2,20 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 336,
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"input = r'''0 1 10 99 999'''\n",
|
||||
"input = r'''125 17'''\n",
|
||||
"#input = r'''0 1 10 99 999'''\n",
|
||||
"#input = r'''125 17'''\n",
|
||||
"input = r'''475449 2599064 213 0 2 65 5755 51149'''\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"stones = {int(x):1 for x in input.split()}\n"
|
||||
"stones = {int(x):1 for x in input.split()}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -40,17 +39,29 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"193269"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for i in range(75):\n",
|
||||
" stones=step(stones)\n"
|
||||
"for i in range(25):\n",
|
||||
" stones=step(stones)\n",
|
||||
"sum(stones.values())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 269,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -59,77 +70,22 @@
|
||||
"228449040027793"
|
||||
]
|
||||
},
|
||||
"execution_count": 269,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"stones = {int(x):1 for x in input.split()}\n",
|
||||
"for i in range(75):\n",
|
||||
" stones=step(stones)\n",
|
||||
"sum(stones.values())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 459,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"defaultdict(<class 'int'>, {6072: 673206953446361827286, 4048: 2734962356528935214636, 14168: 1343031271242210938014, 12144: 2300399947749196772879, 32772608: 1863779202753698640740, 40: 1800617350854067786011, 48: 1963004354915731679509, 2024: 1901242329177217918652, 80: 2071514762314577986161, 96: 1909127758252913749300, 16192: 2830947128757671518126, 18216: 1268972356388161972299, 8096: 2899816988343294077917, 10120: 569045306257576076768, 1: 2887855190943409245055, 36869184: 835429087093576428724, 24579456: 1514487337123235977781, 28: 545640724430717426974, 67: 383253720369045854463, 60: 826473730693175762336, 32: 1191095444078885640165, 24: 1908139147880629965130, 57: 656448851778621933102, 94: 656448851778621838017, 56: 656448851778627848108, 3: 1022537248590681705286, 2: 4154202543052785522877, 7: 2039928642914626319453, 6: 3494049803451761028009, 77: 807841723709838053671, 26: 807841723709836079846, 8: 4299935420757677600191, 36: 362115363446705336479, 86: 362115363446702092621, 91: 362115363446699634033, 84: 362115363446704267359, 20: 1414077300163673644677, 0: 4024270438576582418162, 4: 4404541520680163316089, 9: 1927418059972380388541, 72: 443220010324135438919, 28676032: 884207592768322392473, 3277: 1227019168257705602169, 2608: 1227019168257705617679, 20482880: 374644389308688664777, 3686: 550006170363052763461, 9184: 550006170363052763461, 2457: 997052327100450699998, 9456: 997052327100450699998, 5: 864321261489050567651, 2867: 582104108096953186703, 6032: 582104108096953405172, 2048: 246636878600168153652, 2880: 246636878600168153652, 291: 1789758, 151: 342634, 849: 342634, 472: 1844850, 7644: 902465, 6480: 902465, 8717: 902465, 7728: 902465, 7827: 1013758, 104: 2102093, 277518444544: 1013758, 485: 1329398, 760: 2384941, 848: 1206102, 3490282752: 24893, 582: 978569, 912: 1136525, 383: 139159, 177: 64484, 332: 546023, 736: 924747, 514096: 1532150, 902704: 709670, 1068672: 1538929, 1036288: 1746323, 7296: 827680, 3176: 685472, 355910522880: 685472, 36459: 399206, 52640: 399206, 22940: 4557788, 82560: 4557788, 26750: 1000893, 64128: 1000893, 30806: 1830556, 25152: 1830556, 6964: 1212535, 7864: 1212535, 4825: 1661335, 2160: 1423211, 276: 80970, 338: 2639160, 630: 80970, 656: 886237, 327: 439800, 463: 309842, 899: 309842, 136: 1139416, 4294: 961302, 9280: 961302, 5336: 961302, 8832: 961302, 1190112: 1118783, 1991616: 2000995, 1866128: 1680826, 1910656: 1888411, 7097: 179997, 3584: 179997, 282893152256: 179997, 1256904: 253530, 744832: 856504, 26545: 894907, 81248: 894907, 2318662016: 617107, 2949534720: 2453816, 4576: 1106822, 8712: 253523, 409: 711184, 133: 51132, 238: 342218, 272: 215231, 316: 381339, 714: 826842, 483: 436766, 712: 2471469, 7511: 198201, 8736: 501195, 400874541056: 198201, 6235: 1342286, 1344: 1342286, 5090: 1342286, 7648: 1342286, 28757: 184489, 96352: 184489, 1341912: 1767369, 1764928: 4344696, 21384: 506160, 12672: 506160, 785312: 313055, 1230592: 447840, 4593: 258062, 4680: 258062, 30478: 608194, 52544: 608194, 34083: 656657, 51232: 656657, 1260952: 384633, 793408: 1223060, 21097: 255457, 36640: 255457, 817696: 773910, 1619200: 1207773, 1477520: 388830, 1343936: 696267, 1165824: 528848, 1700160: 2254190, 941160: 371454, 1052480: 1580061, 24251: 815059, 72992: 815059, 763: 471883, 827: 522166, 816: 1780515, 2982307328: 2142927, 4170: 376772, 4520: 376772, 318418659328: 376772, 8092: 398269, 3568: 398269, 238322405376: 398269, 378: 181581, 195: 1336377, 896: 1362239, 320: 665404, 2162992128: 1323657, 249: 1118147, 71: 3838249, 820: 476981, 800: 1415292, 623: 452973, 392: 1439963, 920: 2335213, 3219908736: 355285, 2031901696: 1539696, 26422: 118500, 91520: 118500, 26873: 551159, 53856: 551159, 4958: 92533, 1928: 92533, 214857218048: 92533, 39204: 108810, 23232: 108810, 1441088: 2111239, 906752: 475091, 1789216: 601144, 987712: 1556273, 752928: 750103, 841984: 852123, 530288: 633355, 1426920: 526219, 455400: 544669, 1473472: 2494680, 29495: 2095582, 34720: 2095582, 22285: 4328598, 37344: 4328598, 1853984: 1459066, 1540264: 1731186, 394: 1392627, 39378944: 314596, 347: 405899, 258: 420481, 554: 566990, 368: 1009121, 6840: 560668, 3104: 912076, 308062515200: 560668, 6566: 706360, 6656: 706360, 361285230592: 706360, 5737: 41638, 6352: 41638, 6191: 41638, 8208: 385856, 1720400: 121360, 40638: 493941, 3392: 695861, 7462: 115095, 2856: 115095, 3724: 115095, 1600: 115095, 5638: 2326812, 544: 6922743, 293642567680: 2326812, 4692: 383261, 8464: 383261, 254053257216: 383261, 5969: 1537165, 7880: 1537165, 7027: 1537165, 3280: 1558662, 22695: 351408, 23596: 387076, 27776: 387076, 1993640: 85057, 1392512: 4265707, 32117: 764042, 15584: 764042, 1513952: 12930, 1781120: 1280411, 1605032: 896373, 825792: 1413315, 6168: 446442, 7472: 446442, 215250489344: 446442, 82: 4305448, 23: 2720733, 27: 3133528, 65: 6737772, 59: 4095116, 61: 4788411, 327463: 361498, 899136: 361498, 1453232: 589783, 37278: 315239, 84160: 315239, 36049: 938929, 86880: 938929, 2228537344: 5074874, 293: 2126708, 642: 1Line truncated
|
||||
"3811\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(stones)\n",
|
||||
"stones=step(stones)\n",
|
||||
" \n",
|
||||
"print(len(stones))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 271,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"defaultdict(<class 'int'>, {2: 1, 0: 1})\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from collections import defaultdict\n",
|
||||
"\n",
|
||||
"def step(stones):\n",
|
||||
" output = defaultdict(int) # Use defaultdict with int, which defaults to 0\n",
|
||||
" for stone, count in stones.items(): # Iterate over the dictionary of stones with their counts\n",
|
||||
" if stone == 0:\n",
|
||||
" output[1] += count # Zero turns into 1, add the count to the 1 key\n",
|
||||
" elif (tx_stone := str(stone)) and len(tx_stone) % 2 == 0:\n",
|
||||
" half = len(tx_stone) // 2\n",
|
||||
" left = int(tx_stone[:half])\n",
|
||||
" right = int(tx_stone[half:])\n",
|
||||
" output[left] += count # Add the count to the left split part\n",
|
||||
" output[right] += count # Add the count to the right split part\n",
|
||||
" else:\n",
|
||||
" output[stone * 2024] += count # Multiply by 2024 and add to the count for that stone\n",
|
||||
" return output\n",
|
||||
"\n",
|
||||
"# Example usage:\n",
|
||||
"stones = {20: 1} # Example input: stone values and their counts\n",
|
||||
"result = step(stones)\n",
|
||||
"print(result)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "pdf",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
@@ -143,7 +99,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.7"
|
||||
"version": "3.8.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 87,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#read lines from input and store each list of space separeted integers into left and right lists respectively.\n",
|
||||
"with open('4/input', 'r') as f:\n",
|
||||
"with open('input', 'r') as f:\n",
|
||||
" data=f.readlines()\n",
|
||||
"\n",
|
||||
"data = [line.strip() for line in data]\n"
|
||||
@@ -15,7 +15,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 88,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -24,7 +24,7 @@
|
||||
"421"
|
||||
]
|
||||
},
|
||||
"execution_count": 88,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -36,7 +36,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 89,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -45,7 +45,7 @@
|
||||
"463"
|
||||
]
|
||||
},
|
||||
"execution_count": 89,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -57,7 +57,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -66,7 +66,7 @@
|
||||
"1640"
|
||||
]
|
||||
},
|
||||
"execution_count": 90,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -89,7 +89,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 91,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -98,7 +98,7 @@
|
||||
"2524"
|
||||
]
|
||||
},
|
||||
"execution_count": 91,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -109,7 +109,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -122,29 +122,16 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[ True, False, True],\n",
|
||||
" [False, True, False],\n",
|
||||
" [ True, False, True]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 106,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"mask = pattern != '.'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 129,
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -153,7 +140,7 @@
|
||||
"1873"
|
||||
]
|
||||
},
|
||||
"execution_count": 129,
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -173,9 +160,21 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.8.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
+1
-8
@@ -1,12 +1,5 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
@@ -11050,7 +11043,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.8"
|
||||
"version": "3.8.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -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/SxHrmEpDjbo6dW1NyK41GW0SjAKbROmoqFFRZ2oZoxF1U01npSbdxCTtfNVRWrGuoqih6mQXxdhKokCaiVzBAe095/sHerdXMD3ohfPx8348Mu8ZzucczuflPeZ6n/f1+rw/jVar1QoAAFCcIZ0uAAAA6AxhAAAACiUMAABAoYQBAAAolDAAAACFEgYAAKBQwgAAABRKGAAAgEIJAwAAUChhAAAACiUMAABABf3iF7/I5z73uYwaNSrDhw/PYYcdlieffLL/+T/5kz9Jo9EYsP7gD/5gm86x02AXDQAAfDC//vWvM3Xq1Hzyk5/Mvffem3322ScvvPBC9txzzwGv+4M/+IPccccd/Y+7urq26TzCAAAAVMx1112Xnp6eAT/oT5gwYYvXdXV1ZcyYMe/7PMaEAACgYn784x/n6KOPzplnnpl99903RxxxRG6//fYtXvfQQw9l3333zcc//vF86Utfyq9+9attOk+j1Wq1BqtoAADotI0bN+bNN9/sdBlb1Wq10mg0Bhzr6uraYrxn2LBhSZI5c+bkzDPPzLJly3LhhRdm4cKFOfvss5Mkd999d3bddddMmDAhL774Yv7sz/4su+++ex577LEMHTq0rXqEAQAAamPjxo2ZMH73rF7b1+lStmr33XfP+vXrBxybN29eLr/88gHHdtlllxx99NF59NFH+49dcMEFWbZsWR577LGtvvfPfvazHHDAAXnwwQdz4okntlWPawYAAKiNN998M6vX9uVfnvpIukdUayK+9/Vmxh/1z1m1alW6u7v7j2/tot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/l8o9HIlVdemSuvvPJ9n8M1AwAAUCidAQAAaqfKuwlVSfUqAgAAdghhAAAACmVMCACA2unLtu3esyP0dbqArdAZAACAQgkDAABQKGNCAADUjt2E2lO9igAAgB1CGAAAgEIZEwIAoHb6WkPSV7GxnKrVk+gMAABAsYQBAAAolDEhAABqp5VGmhW76VirYvUkOgMAAFAsYQAAAAplTAgAgNqxm1B7qlcRAACwQwgDAABQKGNCAADUTrPVSLNVrd17qlZPojMAAADFEgYAAKBQxoQAAKidvgxJX8V+7121ehKdAQAAKJYwAAAAhTImBABA7dhNqD06AwAAUChhAAAACmVMCACA2mlmSJoV+7131epJdAYAAKBYwgAAABTKmBAAALXT12qkr2K791StnkRnAAAAiiUMAABAoYwJAQBQO2461h6dAQAAKJQwAAAAhTImBABA7bRaQ9JsVev33q2K1ZPoDAAAQLGEAQAAKJQxIQAAaqcvjfSlWrv3VK2eRGcAAACKJQwAAEChjAlRtDUXHNvpEgbV6Bsf7XQJg+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