day 4, basic grid processing

This commit is contained in:
setop 2024-12-04 10:11:37 +01:00
parent 9638fb3ee5
commit 1153fac5fe
2 changed files with 42 additions and 0 deletions

26
d04/part1.py Normal file
View File

@ -0,0 +1,26 @@
import sys, os
L = sys.stdin.read().strip().split("\n")
G = [list(l) for l in L]
H = len(G)
W = len(G[0])
M4 = [(0,1),(0,-1),(1,0),(-1,0)]
D4 = [(1,1),(-1,-1),(1,-1),(-1,1)]
M8 = M4 + D4
S = 0
for i in range(H): # rows
for j in range(W): # cols
if G[i][j] == "X":
for (dx,dy) in M8:
xi = i
yj = j
for l in 'MAS':
xi += dx
yj += dy
if xi<0 or xi>=H or yj<0 or yj>=W or G[xi][yj]!=l:
break
else: # if the inner loop wasn't broken.
S += 1
print(S)

16
d04/part2.py Normal file
View File

@ -0,0 +1,16 @@
import sys, os
L = sys.stdin.read().strip().split("\n")
G = [list(l) for l in L]
H = len(G)
W = len(G[0])
S = 0
for i in range(1,H-1): # rows
for j in range(1, W-1): # cols
if G[i][j] == "A":
for P in ["MMSS", "SMMS", "SSMM", "MSSM"]:
if G[i-1][j-1] == P[0] and G[i-1][j+1] == P[1] \
and G[i+1][j+1] == P[2] and G[i+1][j-1] == P[3]:
S+=1
print(S)