Refactor 2024 day 4.
This commit is contained in:
parent
f0d8e156a9
commit
55cb5ed745
@ -1,45 +1,30 @@
|
|||||||
|
import itertools as it
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import numpy as np
|
lines = sys.stdin.read().strip().splitlines()
|
||||||
|
n = len(lines)
|
||||||
|
|
||||||
lines = np.array(list(map(list, sys.stdin.read().strip().splitlines())))
|
answer_1 = sum(
|
||||||
print(lines)
|
line.count("XMAS") + line.count("SAMX")
|
||||||
n = lines.shape[0]
|
for i in range(n)
|
||||||
|
for ri, rk, ro, ci, ck, cm in (
|
||||||
answer_1 = 0
|
(1, 0, 0, 0, 1, n),
|
||||||
for i in range(n):
|
(0, 1, 0, 1, 0, n),
|
||||||
line = "".join(lines[i, :])
|
(0, 1, 0, 1, 1, n - i),
|
||||||
answer_1 += line.count("XMAS") + line.count("SAMX")
|
(0, -1, -1, 1, 1, n - i),
|
||||||
|
(1, 1, 0, 0, 1, n - i if i != 0 else 0),
|
||||||
column = "".join(lines[:, i])
|
(-1, -1, -1, 0, 1, n - i if i != 0 else 0),
|
||||||
answer_1 += column.count("XMAS") + column.count("SAMX")
|
)
|
||||||
|
if (
|
||||||
diag = "".join(np.diagonal(lines, i))
|
line := "".join(lines[ri * i + rk * k + ro][ci * i + ck * k] for k in range(cm))
|
||||||
answer_1 += diag.count("XMAS") + diag.count("SAMX")
|
)
|
||||||
|
)
|
||||||
diag = "".join(np.diagonal(lines[::-1, :], i))
|
|
||||||
answer_1 += diag.count("XMAS") + diag.count("SAMX")
|
|
||||||
|
|
||||||
if i != 0:
|
|
||||||
diag = "".join(np.diagonal(lines, -i))
|
|
||||||
answer_1 += diag.count("XMAS") + diag.count("SAMX")
|
|
||||||
|
|
||||||
diag = "".join(np.diagonal(lines[::-1, :], -i))
|
|
||||||
answer_1 += diag.count("XMAS") + diag.count("SAMX")
|
|
||||||
|
|
||||||
answer_2 = sum(
|
answer_2 = sum(
|
||||||
"".join(
|
lines[i][j] == "A"
|
||||||
(
|
and "".join(lines[i + di][j + dj] for di, dj in it.product((-1, 1), (-1, 1)))
|
||||||
lines[i - 1, j - 1],
|
|
||||||
lines[i - 1, j + 1],
|
|
||||||
lines[i + 1, j - 1],
|
|
||||||
lines[i + 1, j + 1],
|
|
||||||
)
|
|
||||||
)
|
|
||||||
in {"MSMS", "SSMM", "MMSS", "SMSM"}
|
in {"MSMS", "SSMM", "MMSS", "SMSM"}
|
||||||
for i in range(1, n - 1)
|
for i, j in it.product(range(1, n - 1), range(1, n - 1))
|
||||||
for j in range(1, n - 1)
|
|
||||||
if lines[i, j] == "A"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
print(f"answer 1 is {answer_1}")
|
print(f"answer 1 is {answer_1}")
|
||||||
|
Loading…
Reference in New Issue
Block a user