From f751b3b8d53d3a900a333a34a7313f2d26fa835e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Wed, 4 Dec 2024 11:04:42 +0100 Subject: [PATCH] Solve 2024:4 p1-2 "Ceres Search" Not gonna lie, This is not my gig skillwise. I had much fun though! A matrix would most likely make the code more readable. I relied massively on test cases here, so I share them here instead of in the source code: assert 1 == solve(""" X..... .M.... ..A... ...S.. ..X... """.strip())[0] assert 2 == solve(""" XS.... .MA... ..AM.. ...SX. ..X... """.strip())[0] assert 2 == solve(""" ..S...... ...A..... ....M.... .....X... ..X...M.. .......A. ........S """.strip())[0] assert 4 == solve(""" X.SS M.AA A.MM S.XX ...M ...A ...S """.strip())[0] assert 4 == solve(""" ...X... ...M... .X.A... XMASAMX .A..... .S..... """.strip())[0] assert 1 == solve(""" ....X ...M. ..A.. .S... """.strip())[0] assert 2 == solve(""" ...SX ..AM. .MA.. XS... """.strip())[0] assert 2 == solve(""" ......X .....M. ....A.. ...S... ..A.... .M..... X...... """.strip())[0] assert 1 == solve(""" M.S .A. M.S """.strip())[1] assert 1 == solve(""" M.M .A. S.S """.strip())[1] assert 1 == solve(""" S.M .A. S.M """.strip())[1] assert 1 == solve(""" S.S .A. M.M """.strip())[1] assert 1 == solve(""" S.S .A. M.M """.strip())[1] assert 1 == solve(""" .A. M.M .A. S.S """.strip())[1] assert 1 == solve(""" M.M. .A.A S.S. """.strip())[1] assert 1 == solve(""" M.M .A. S.S .A. """.strip())[1] assert 1 == solve(""" .M.M A.A. .S.S """.strip())[1] --- 2024-python/aoc.py | 2 +- 2024-python/output/day_04.py | 92 ++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 2024-python/output/day_04.py 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)