1.py (1051B) [raw]
1 #!/usr/bin/env python3 2 import sys 3 4 G = [] 5 for l in sys.stdin: 6 chars = [c for c in l.strip()] 7 G.append(chars) 8 9 def issym(c): 10 return c is not None and c != '.' and not c.isdigit() 11 12 def safeget(i, j): 13 if i > 0 and j > 0 and i < len(G) and j < len(G[0]): 14 return G[i][j] 15 return None 16 17 def nearsym(i, j): 18 return ( 19 issym(safeget(i-1,j-1)) or 20 issym(safeget(i-1,j)) or 21 issym(safeget(i-1,j+1)) or 22 issym(safeget(i,j-1)) or 23 issym(safeget(i,j)) or 24 issym(safeget(i,j+1)) or 25 issym(safeget(i+1,j-1)) or 26 issym(safeget(i+1,j)) or 27 issym(safeget(i+1,j+1)) 28 ) 29 30 tot = 0 31 for i in range(len(G)): 32 numstr = '' 33 sym = False 34 for j in range(len(G[0])): 35 if G[i][j].isdigit(): 36 numstr += G[i][j] 37 if not sym: 38 sym = nearsym(i, j) 39 else: 40 if numstr and sym: 41 tot += int(numstr) 42 numstr = '' 43 sym = False 44 if numstr and sym: 45 tot += int(numstr) 46 47 print(tot)