Solve 2024 day 4

This commit is contained in:
2024-12-04 00:29:03 -05:00
parent 0fb75687d1
commit 24174b8cb9
2 changed files with 46 additions and 2 deletions

44
2024/d4.py Normal file
View File

@@ -0,0 +1,44 @@
from lib import get_data
from lib import Grid2D
data = get_data(__file__)
g = Grid2D(data)
DIRS = {
"N": (-1, 0),
"E": (0, 1),
"S": (1, 0),
"W": (0, -1),
"NE": (-1, 1),
"SE": (1, 1),
"SW": (1, -1),
"NW": (-1, -1),
}
t1, t2 = 0, 0
for row in range(g.n_rows):
for col in range(g.n_cols):
for d in DIRS.values():
coords = [(row + d[0] * i, col + d[1] * i) for i in range(len("XMAS"))]
for (r, c), z in zip(coords, "XMAS"):
if not (0 <= r < g.n_rows and 0 <= c < g.n_cols and g[(r, c)] == z):
break
else:
t1 += 1
coords = [
(row - 1, col - 1),
(row - 1, col + 1),
(row, col),
(row + 1, col - 1),
(row + 1, col + 1),
]
for s in ["MMASS", "MSAMS", "SSAMM", "SMASM"]:
for (r, c), z in zip(coords, s):
if not (0 <= r < g.n_rows and 0 <= c < g.n_cols and g[(r, c)] == z):
break
else:
t2 += 1
print(t1)
print(t2)

View File

@@ -294,5 +294,5 @@ and focus. Of course, learning more algorithms and techniques helps.
- Day 1: `00:01:30 124 0 00:02:49 141 0`
- Day 2: `00:05:34 686 0 00:08:21 531 0`
- Day 3: `00:01:48 165 0 00:02:40 56 45`
- Day 4:
- Day 4: `00:07:47 1101 0 00:24:07 2410 0`
- Day 5: