aoc2023/d03/part1.py

32 lines
629 B
Python
Raw Permalink Normal View History

2023-12-03 09:47:21 +00:00
import sys
G = [ list(l[:-1]) for l in sys.stdin.readlines()]
W = len(G[0])
H = len(G)
def get_G(x,y):
return '.' if x<0 or y<0 or x>W-1 or y>H-1 else G[y][x]
def is_sym(a:str) -> bool:
return a != '.' and not a.isdigit()
2023-12-06 22:41:41 +00:00
S = 0
2023-12-03 09:47:21 +00:00
c = 0 # current
valid = False
for y in range(H):
for x in range(W):
i = get_G(x,y)
if not i.isdigit():
if valid:
2023-12-06 22:41:41 +00:00
S += c
2023-12-03 09:47:21 +00:00
valid = False
c = 0
else:
c = c*10+int(i)
for dx in [-1,0,1]:
for dy in [-1,0,1]:
valid |= is_sym(get_G(x+dx,y+dy))
2023-12-06 22:41:41 +00:00
print(S)