diff options
| author | nekineki <nekineki@nekineki.net> | 2025-12-04 17:22:08 +0100 |
|---|---|---|
| committer | nekineki <nekineki@nekineki.net> | 2025-12-04 17:22:08 +0100 |
| commit | 11a4bbb72fc0eb9a9d54d1b2efc5abffb11b2c08 (patch) | |
| tree | 43c501a3e563dcf63e49a220fd6cc6041367588c | |
| parent | 5728ac294db4a017075d6cdecbff6098383b550e (diff) | |
day04 make nicer
| -rwxr-xr-x | 2025/day04.py | 32 |
1 files changed, 9 insertions, 23 deletions
diff --git a/2025/day04.py b/2025/day04.py index e69acd5..00ab048 100755 --- a/2025/day04.py +++ b/2025/day04.py @@ -18,20 +18,7 @@ for x,line in enumerate(lines): for y, c in enumerate(line): D[(x,y)] = c -for (x, y), c in D.items(): - if c != '@': - continue - n = 0 - for dx, dy in [(1, -1), (1, 0), (1, 1), (-1, -1), - (-1, 0), (-1, 1), (0, -1), (0, 1)]: - nx = x + dx - ny = y + dy - if (nx, ny) in D and D[(nx, ny)] == '@': - n += 1 - if n < 4: - res1 += 1 - -def remove(D): +def solve(D, remove=False): r = 0 for (x, y), c in D.items(): if c != '@': @@ -39,19 +26,18 @@ def remove(D): n = 0 for dx, dy in [(1, -1), (1, 0), (1, 1), (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1)]: - nx = x + dx - ny = y + dy - if (nx, ny) in D and D[(nx, ny)] == '@': + d = (x + dx, y + dy) + if d in D and D[d] == '@': n += 1 if n < 4: r += 1 - D[(x, y)] = '.' - return D, r + if remove == True: + D[(x, y)] = '.' + return r -D, r = remove(D) -res2 += r -while r > 0: - D, r = remove(D) +res1 = solve(D) + +while (r := solve(D, remove=True)) > 0: res2 += r print('res1:', res1) |
