diff --git a/2024-python/aoc.py b/2024-python/aoc.py index c84d089..8adeee7 100644 --- a/2024-python/aoc.py +++ b/2024-python/aoc.py @@ -4,7 +4,7 @@ from pathlib import Path def headline(n): """Print day number and name, followed by a ruler. Used by the answer decorator""" - print(f"\n--- Day {n} ---\n") + print(f"\nDay {int(n)} - https://adventofcode.com/{year}/day/{int(n)}\n") year = 2024 diff --git a/2024-python/output/day_04.py b/2024-python/output/day_04.py new file mode 100644 index 0000000..17b6c84 --- /dev/null +++ b/2024-python/output/day_04.py @@ -0,0 +1,92 @@ +import re + + +def solve(data): + row_count = len(data.split()) + col_count = len(data.split()[0]) + grid = [c for r in data.split() for c in r] + grid_rotated = [c for r in zip(*data.split()[::-1]) for c in r] + needle = r"(?=(XMAS|SAMX))" + + p1 = len(re.findall(needle, data)) + p1 += len( + re.findall( + needle, + "\n".join(["".join(r) for r in list(zip(*data.split()))]), + ) + ) + + for cells, o in [ + (grid, col_count), + (grid[::-1], col_count), + (grid_rotated, row_count), + (grid_rotated[::-1], row_count), + ]: + p1 += sum( + all( + [ + i % o < (o - 3), + cells[i] == "X", + cells[i + o + 1] == "M", + cells[i + 2 * (o + 1)] == "A", + cells[i + 3 * (o + 1)] == "S", + ] + ) + for i in range(len(cells) - 3 * (o + 1)) + ) + + p2 = sum( + [ + 1 <= (i % col_count) < col_count - 1 + and grid[i] == "A" + and any( + [ + all( + [ + grid[i - col_count - 1] == "M", + grid[i - col_count + 1] == "M", + grid[i + col_count - 1] == "S", + grid[i + col_count + 1] == "S", + ] + ), + all( + [ + grid[i - col_count - 1] == "S", + grid[i - col_count + 1] == "S", + grid[i + col_count - 1] == "M", + grid[i + col_count + 1] == "M", + ] + ), + all( + [ + grid[i - col_count - 1] == "M", + grid[i - col_count + 1] == "S", + grid[i + col_count - 1] == "M", + grid[i + col_count + 1] == "S", + ] + ), + all( + [ + grid[i - col_count - 1] == "S", + grid[i - col_count + 1] == "M", + grid[i + col_count - 1] == "S", + grid[i + col_count + 1] == "M", + ] + ), + ] + ) + for i in range(col_count + 1, len(grid) - col_count - 1) + ] + ) + + return p1, p2 + + +if __name__ == "__main__": + with open("./input/04.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2)