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]
This commit is contained in:
parent
cb622409f9
commit
f751b3b8d5
2 changed files with 93 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
92
2024-python/output/day_04.py
Normal file
92
2024-python/output/day_04.py
Normal file
|
|
@ -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)
|
||||
Loading…
Add table
Reference in a new issue