aoc2023/d03/part1.py

33 lines
711 B
Python

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()
c = 0 # current
valid = False
Ns = [] # all part numbers
for y in range(H):
for x in range(W):
i = get_G(x,y)
if not i.isdigit():
# appent current if valid and reset
if valid:
Ns.append(c)
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))
print(sum(Ns))