diff --git a/2024/04/solution.py b/2024/04/solution.py index 63827cb..b5e6af8 100644 --- a/2024/04/solution.py +++ b/2024/04/solution.py @@ -1,3 +1,91 @@ from aocd import get_data data = get_data(day=4, year=2024) +table = data.split() +table.reverse() +table = [list(row) for row in table] +def east(x1, y1): + try: + if table[y1][x1] == "X" and table[y1][x1 + 1] == "M" and table[y1][x1 + 2] == "A" and table[y1][x1 + 3] == "S": + print(table[y1][x1], table[y1][x1 + 1], table[y1][x1 + 2], table[y1][x1 + 3]) + return True + except IndexError: + return False + +def west(x1, y1): + try: + if table[y1][x1] == "X" and table[y1][x1 - 1] == "M" and table[y1][x1 - 2] == "A" and table[y1][x1 - 3] == "S": + print(table[y1][x1], table[y1][x1 - 1], table[y1][x1 - 2], table[y1][x1 - 3]) + return True + except IndexError: + return False + +def north(x1, y1): + try: + if table[y1][x1] == "X" and table[y1 + 1][x1] == "M" and table[y1 + 2][x1] == "A" and table[y1 + 3][x1] == "S": + print(table[y1][x1], table[y1 + 1][x1], table[y1 + 2][x1], table[y1 + 3][x1]) + return True + except IndexError: + return False + +def south(x1, y1): + try: + if table[y1][x1] == "X" and table[y1 - 1][x1] == "M" and table[y1 - 2][x1] == "A" and table[y1 - 3][x1] == "S": + print(table[y1][x1], table[y1 - 1][x1], table[y1 - 2][x1], table[y1 - 3][x1]) + return True + except IndexError: + return False + +def northeast(x1, y1): + try: + if table[y1][x1] == "X" and table[y1 + 1][x1 + 1] == "M" and table[y1 + 2][x1 + 2] == "A" and table[y1 + 3][x1 + 3] == "S": + print(table[y1][x1], table[y1 + 1][x1 + 1], table[y1 + 2][x1 + 2], table[y1 + 3][x1 + 3]) + return True + except IndexError: + return False + +def northwest(x1, y1): + try: + if table[y1][x1] == "X" and table[y1 + 1][x1 - 1] == "M" and table[y1 + 2][x1 - 2] == "A" and table[y1 + 3][x1 - 3] == "S": + print(table[y1][x1], table[y1 + 1][x1 - 1], table[y1 + 2][x1 - 2], table[y1 + 3][x1 - 3]) + return True + except IndexError: + return False + +def southeast(x1, y1): + try: + if table[y1][x1] == "X" and table[y1 - 1][x1 + 1] == "M" and table[y1 - 2][x1 + 2] == "A" and table[y1 - 3][x1 + 3] == "S": + print(table[y1][x1], table[y1 - 1][x1 + 1], table[y1 - 2][x1 + 2], table[y1 - 3][x1 + 3]) + return True + except IndexError: + return False + +def southwest(x1, y1): + try: + if table[y1][x1] == "X" and table[y1 - 1][x1 - 1] == "M" and table[y1 - 2][x1 - 2] == "A" and table[y1 - 3][x1 - 3] == "S": + print(table[y1][x1], table[y1 - 1][x1 - 1], table[y1 - 2][x1 - 2], table[y1 - 3][x1 - 3]) + return True + except IndexError: + return False + +total = 0 +for y in range(len(table)): + for x in range(len(table[y])): + if east(x, y): + total += 1 + if west(x, y): + total += 1 + if north(x, y): + total += 1 + if south(x, y): + total += 1 + if northeast(x, y): + total += 1 + if northwest(x, y): + total += 1 + if southeast(x, y): + total += 1 + if southwest(x, y): + total += 1 + +print(total) \ No newline at end of file diff --git a/2025/04/solution.py b/2025/04/solution.py new file mode 100644 index 0000000..89e2e90 --- /dev/null +++ b/2025/04/solution.py @@ -0,0 +1,72 @@ +from aocd import get_data + +data = get_data(day=4, year=2025) +table = [list(row) for row in data.split()] + +roll = '@' +empty = '.' + +count = 0 + +directions = [ + (0, 1), # east + (0, -1), # west + (1, 0), # north + (-1, 0), # south + (1, 1), # northeast + (1, -1), # northwest + (-1, 1), # southeast + (-1, -1) # southwest +] + +# pad the table with the empty character +padded_table = table.copy() +padded_table.insert(0, [empty] * len(table[0])) +padded_table.append([empty] * len(table[0])) +for i in range(len(padded_table)): + padded_table[i].insert(0, empty) + padded_table[i].append(empty) + +# check 8 directions, if there is fewer than four rolls, count it as a valid position and continue +for y in range(1, len(padded_table) - 1): + for x in range(1, len(padded_table[0]) - 1): + if padded_table[y][x] == roll: + roll_count = 0 + for direction in directions: + dx, dy = direction + try: + if padded_table[y + dy][x + dx] == roll: + roll_count += 1 + except IndexError: + continue + if roll_count < 4: + count += 1 +print(count) + +# part 2 +count_two = 0 + +def remove_rolls(count, table): + for y in range(1, len(table) - 1): + for x in range(1, len(table[0]) - 1): + if table[y][x] == roll: + roll_count = 0 + for direction in directions: + dx, dy = direction + try: + if table[y + dy][x + dx] == roll: + roll_count += 1 + except IndexError: + continue + if roll_count < 4: + count += 1 + table[y][x] = empty + return count, table + +prev = -1 +table_two = padded_table.copy() +while prev != count_two: + prev = count_two + count_two, table_two = remove_rolls(count_two, table_two) + print(count_two) +print(count_two) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 4d72bd8..90321a9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,5 +5,5 @@ description = "Add your description here" readme = "README.md" requires-python = ">=3.11" dependencies = [ - "advent-of-code-data>=2.0.4", + "advent-of-code-data>=2.1.0", ] diff --git a/uv.lock b/uv.lock index a1a4887..eae21f5 100644 --- a/uv.lock +++ b/uv.lock @@ -1,20 +1,21 @@ version = 1 +revision = 3 requires-python = ">=3.11" [[package]] name = "advent-of-code-data" -version = "2.0.4" +version = "2.1.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "aocd-example-parser" }, { name = "beautifulsoup4" }, { name = "pebble" }, - { name = "tzdata", marker = "platform_system == 'Windows'" }, + { name = "tzdata", marker = "sys_platform == 'win32'" }, { name = "urllib3" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/aa/05/bf8b2cf081ad28b31b1ba03f9252ac761e88e55dd27a1378d169ef437402/advent_of_code_data-2.0.4.tar.gz", hash = "sha256:d345dd14e4a123992297d5a22b364d161d4458def9ffb9481797f7aabf138ced", size = 47000 } +sdist = { url = "https://files.pythonhosted.org/packages/c2/ba/90939f2d091ef220a09d2a24e56b6296ac442428be1051bcdc9125a79054/advent_of_code_data-2.1.0.tar.gz", hash = "sha256:947e6583f581ed184873aac835b333fc39a5712cce606c67532f9d43253a98e2", size = 47624, upload-time = "2025-01-04T04:53:57.747Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/bf/5f/a9dbeba1e52aee3be263184ca2de9f2fdf1fd8419cb228245f08930618a1/advent_of_code_data-2.0.4-py3-none-any.whl", hash = "sha256:e3abc993ddc8436fae693b212b4b66b399b42b3c4d8b588c025e5b421c307b3d", size = 38888 }, + { url = "https://files.pythonhosted.org/packages/8f/fe/47de97c2473539c14a1041297ae9807539372be08ccaf3b645fa21af1930/advent_of_code_data-2.1.0-py3-none-any.whl", hash = "sha256:1e0457338a9572e610f72da8aad8b06f778311a69b0ec676bf2e5db8328f4d45", size = 39468, upload-time = "2025-01-04T04:53:55.321Z" }, ] [[package]] @@ -26,15 +27,15 @@ dependencies = [ ] [package.metadata] -requires-dist = [{ name = "advent-of-code-data", specifier = ">=2.0.4" }] +requires-dist = [{ name = "advent-of-code-data", specifier = ">=2.1.0" }] [[package]] name = "aocd-example-parser" -version = "2023.12.20" +version = "2025.12.4" source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/b9/1e/25ef659cacd321177d54238b3d4035fda044fa8b2f73e367455eac040802/aocd_example_parser-2023.12.20.tar.gz", hash = "sha256:753d336986dd0ebf00ee91a2e36ba5f51faece3bbcea8827b11d6c57435c7407", size = 14179 } +sdist = { url = "https://files.pythonhosted.org/packages/d3/c9/95240e094c82e240c5b4beb849755fec753e79f1cccfdb46a3467b38fc90/aocd_example_parser-2025.12.4.tar.gz", hash = "sha256:6f809bf1ba7220516d1d5bfda69e036df6aea5301878b33f13a8aa9e20b8671b", size = 14787, upload-time = "2025-12-04T05:27:19.877Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/4e/a5/716c50f2fdedc5916060cd48acf9045c0d13f2c8d3f7fccbc2fdbe3cda01/aocd_example_parser-2023.12.20-py3-none-any.whl", hash = "sha256:5244f01a54c4e18e69d53389410444ea8fde50b27a59101d968631ee078dd9c9", size = 12605 }, + { url = "https://files.pythonhosted.org/packages/04/d8/857e1a8bfeab9738e8cd686b56dcd3617c1e2caef03319ada644876703b2/aocd_example_parser-2025.12.4-py3-none-any.whl", hash = "sha256:e031f030940e9919f29b4236ec8800eb3ac6677da06de49d0ee5a3249b10cc7c", size = 13199, upload-time = "2025-12-04T05:27:18.98Z" }, ] [[package]] @@ -44,43 +45,43 @@ source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "soupsieve" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/b3/ca/824b1195773ce6166d388573fc106ce56d4a805bd7427b624e063596ec58/beautifulsoup4-4.12.3.tar.gz", hash = "sha256:74e3d1928edc070d21748185c46e3fb33490f22f52a3addee9aee0f4f7781051", size = 581181 } +sdist = { url = "https://files.pythonhosted.org/packages/b3/ca/824b1195773ce6166d388573fc106ce56d4a805bd7427b624e063596ec58/beautifulsoup4-4.12.3.tar.gz", hash = "sha256:74e3d1928edc070d21748185c46e3fb33490f22f52a3addee9aee0f4f7781051", size = 581181, upload-time = "2024-01-17T16:53:17.902Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/b1/fe/e8c672695b37eecc5cbf43e1d0638d88d66ba3a44c4d321c796f4e59167f/beautifulsoup4-4.12.3-py3-none-any.whl", hash = "sha256:b80878c9f40111313e55da8ba20bdba06d8fa3969fc68304167741bbf9e082ed", size = 147925 }, + { url = "https://files.pythonhosted.org/packages/b1/fe/e8c672695b37eecc5cbf43e1d0638d88d66ba3a44c4d321c796f4e59167f/beautifulsoup4-4.12.3-py3-none-any.whl", hash = "sha256:b80878c9f40111313e55da8ba20bdba06d8fa3969fc68304167741bbf9e082ed", size = 147925, upload-time = "2024-01-17T16:53:12.779Z" }, ] [[package]] name = "pebble" version = "5.1.0" source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/c6/b2/a83f1d951977e6392efdb21f4a901a3fd920b992450de9a464e59241d2ed/Pebble-5.1.0.tar.gz", hash = "sha256:5c30376f1827b21ecec4126ff90e7f22ad5501cac1ff2b32c86ff2601681f932", size = 35060 } +sdist = { url = "https://files.pythonhosted.org/packages/c6/b2/a83f1d951977e6392efdb21f4a901a3fd920b992450de9a464e59241d2ed/Pebble-5.1.0.tar.gz", hash = "sha256:5c30376f1827b21ecec4126ff90e7f22ad5501cac1ff2b32c86ff2601681f932", size = 35060, upload-time = "2024-11-27T20:51:07.722Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/31/69/0fa75151350a9d7b848bfde929673ad505346940ae2713f862647394f0f7/Pebble-5.1.0-py3-none-any.whl", hash = "sha256:530a398299ecd3a4ed1baf2e4b8045d8280b1e665560b0b409f8d8e58db60111", size = 36177 }, + { url = "https://files.pythonhosted.org/packages/31/69/0fa75151350a9d7b848bfde929673ad505346940ae2713f862647394f0f7/Pebble-5.1.0-py3-none-any.whl", hash = "sha256:530a398299ecd3a4ed1baf2e4b8045d8280b1e665560b0b409f8d8e58db60111", size = 36177, upload-time = "2024-11-27T20:51:05.829Z" }, ] [[package]] name = "soupsieve" version = "2.6" source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/d7/ce/fbaeed4f9fb8b2daa961f90591662df6a86c1abf25c548329a86920aedfb/soupsieve-2.6.tar.gz", hash = "sha256:e2e68417777af359ec65daac1057404a3c8a5455bb8abc36f1a9866ab1a51abb", size = 101569 } +sdist = { url = "https://files.pythonhosted.org/packages/d7/ce/fbaeed4f9fb8b2daa961f90591662df6a86c1abf25c548329a86920aedfb/soupsieve-2.6.tar.gz", hash = "sha256:e2e68417777af359ec65daac1057404a3c8a5455bb8abc36f1a9866ab1a51abb", size = 101569, upload-time = "2024-08-13T13:39:12.166Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/d1/c2/fe97d779f3ef3b15f05c94a2f1e3d21732574ed441687474db9d342a7315/soupsieve-2.6-py3-none-any.whl", hash = "sha256:e72c4ff06e4fb6e4b5a9f0f55fe6e81514581fca1515028625d0f299c602ccc9", size = 36186 }, + { url = "https://files.pythonhosted.org/packages/d1/c2/fe97d779f3ef3b15f05c94a2f1e3d21732574ed441687474db9d342a7315/soupsieve-2.6-py3-none-any.whl", hash = "sha256:e72c4ff06e4fb6e4b5a9f0f55fe6e81514581fca1515028625d0f299c602ccc9", size = 36186, upload-time = "2024-08-13T13:39:10.986Z" }, ] [[package]] name = "tzdata" version = "2024.2" source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/e1/34/943888654477a574a86a98e9896bae89c7aa15078ec29f490fef2f1e5384/tzdata-2024.2.tar.gz", hash = "sha256:7d85cc416e9382e69095b7bdf4afd9e3880418a2413feec7069d533d6b4e31cc", size = 193282 } +sdist = { url = "https://files.pythonhosted.org/packages/e1/34/943888654477a574a86a98e9896bae89c7aa15078ec29f490fef2f1e5384/tzdata-2024.2.tar.gz", hash = "sha256:7d85cc416e9382e69095b7bdf4afd9e3880418a2413feec7069d533d6b4e31cc", size = 193282, upload-time = "2024-09-23T18:56:46.89Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/a6/ab/7e5f53c3b9d14972843a647d8d7a853969a58aecc7559cb3267302c94774/tzdata-2024.2-py2.py3-none-any.whl", hash = "sha256:a48093786cdcde33cad18c2555e8532f34422074448fbc874186f0abd79565cd", size = 346586 }, + { url = "https://files.pythonhosted.org/packages/a6/ab/7e5f53c3b9d14972843a647d8d7a853969a58aecc7559cb3267302c94774/tzdata-2024.2-py2.py3-none-any.whl", hash = "sha256:a48093786cdcde33cad18c2555e8532f34422074448fbc874186f0abd79565cd", size = 346586, upload-time = "2024-09-23T18:56:45.478Z" }, ] [[package]] name = "urllib3" version = "2.2.3" source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/ed/63/22ba4ebfe7430b76388e7cd448d5478814d3032121827c12a2cc287e2260/urllib3-2.2.3.tar.gz", hash = "sha256:e7d814a81dad81e6caf2ec9fdedb284ecc9c73076b62654547cc64ccdcae26e9", size = 300677 } +sdist = { url = "https://files.pythonhosted.org/packages/ed/63/22ba4ebfe7430b76388e7cd448d5478814d3032121827c12a2cc287e2260/urllib3-2.2.3.tar.gz", hash = "sha256:e7d814a81dad81e6caf2ec9fdedb284ecc9c73076b62654547cc64ccdcae26e9", size = 300677, upload-time = "2024-09-12T10:52:18.401Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/ce/d9/5f4c13cecde62396b0d3fe530a50ccea91e7dfc1ccf0e09c228841bb5ba8/urllib3-2.2.3-py3-none-any.whl", hash = "sha256:ca899ca043dcb1bafa3e262d73aa25c465bfb49e0bd9dd5d59f1d0acba2f8fac", size = 126338 }, + { url = "https://files.pythonhosted.org/packages/ce/d9/5f4c13cecde62396b0d3fe530a50ccea91e7dfc1ccf0e09c228841bb5ba8/urllib3-2.2.3-py3-none-any.whl", hash = "sha256:ca899ca043dcb1bafa3e262d73aa25c465bfb49e0bd9dd5d59f1d0acba2f8fac", size = 126338, upload-time = "2024-09-12T10:52:16.589Z" }, ]