add niceties to day 2

This commit is contained in:
setop 2023-12-06 23:41:41 +01:00
parent 69c8d0f72c
commit 6860e71dbd
2 changed files with 6 additions and 9 deletions

View File

@ -11,16 +11,15 @@ def get_G(x,y):
def is_sym(a:str) -> bool: def is_sym(a:str) -> bool:
return a != '.' and not a.isdigit() return a != '.' and not a.isdigit()
S = 0
c = 0 # current c = 0 # current
valid = False valid = False
Ns = [] # all part numbers
for y in range(H): for y in range(H):
for x in range(W): for x in range(W):
i = get_G(x,y) i = get_G(x,y)
if not i.isdigit(): if not i.isdigit():
# appent current if valid and reset
if valid: if valid:
Ns.append(c) S += c
valid = False valid = False
c = 0 c = 0
else: else:
@ -29,4 +28,4 @@ for y in range(H):
for dy in [-1,0,1]: for dy in [-1,0,1]:
valid |= is_sym(get_G(x+dx,y+dy)) valid |= is_sym(get_G(x+dx,y+dy))
print(sum(Ns)) print(S)

View File

@ -28,8 +28,6 @@ for y in range(H):
if '*' == get_G(x+dx,y+dy): if '*' == get_G(x+dx,y+dy):
valid = (x+dx,y+dy) valid = (x+dx,y+dy)
S = 0 print(
for _,v in Rs.items(): sum( v[0] * v[1] for _,v in Rs.items() if len(v)==2)
if len(v)==2: )
S += v[0] * v[1]
print(S)