1.py (681B) [raw]
1 #!/usr/bin/env python3 2 import sys 3 4 G = [] 5 6 def get(i, j): 7 if i >= 0 and i < len(G) and j >= 0 and j < len(G[0]): 8 return G[i][j] 9 return "" 10 11 12 # Read the puzzle 13 for l in sys.stdin: 14 letts = [c for c in l.strip()] 15 G.append(letts) 16 17 c = 0 18 for i in range(len(G)): 19 for j in range(len(G[0])): 20 chk = lambda di, dj: get(i+di,j+dj) == "M" and get(i+2*di,j+2*dj) == "A" and get(i+3*di,j+3*dj) == "S" 21 if G[i][j] == "X": 22 c += chk(1, 1) 23 c += chk(1, 0) 24 c += chk(1, -1) 25 c += chk(0, 1) 26 c += chk(0, -1) 27 c += chk(-1, 1) 28 c += chk(-1, 0) 29 c += chk(-1, -1) 30 31 print(c)