From 6860e71dbd974fa41e49ef86b6a2cc0d57a26bec Mon Sep 17 00:00:00 2001 From: setop Date: Wed, 6 Dec 2023 23:41:41 +0100 Subject: [PATCH] add niceties to day 2 --- d03/part1.py | 7 +++---- d03/part2.py | 8 +++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/d03/part1.py b/d03/part1.py index 1c6bdd4..16f5c5d 100644 --- a/d03/part1.py +++ b/d03/part1.py @@ -11,16 +11,15 @@ def get_G(x,y): def is_sym(a:str) -> bool: return a != '.' and not a.isdigit() +S = 0 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) + S += c valid = False c = 0 else: @@ -29,4 +28,4 @@ for y in range(H): for dy in [-1,0,1]: valid |= is_sym(get_G(x+dx,y+dy)) -print(sum(Ns)) +print(S) diff --git a/d03/part2.py b/d03/part2.py index c5ce513..aa08320 100644 --- a/d03/part2.py +++ b/d03/part2.py @@ -28,8 +28,6 @@ for y in range(H): if '*' == get_G(x+dx,y+dy): valid = (x+dx,y+dy) -S = 0 -for _,v in Rs.items(): - if len(v)==2: - S += v[0] * v[1] -print(S) +print( + sum( v[0] * v[1] for _,v in Rs.items() if len(v)==2) +)