Day 4: Part 1

This commit is contained in:
2025-12-05 06:56:02 +01:00
parent 71217a3d4c
commit 5057e2e8eb
3 changed files with 163 additions and 0 deletions

24
day4/main.py Normal file
View File

@@ -0,0 +1,24 @@
with open("day4/input.txt") as file:
paperrolls = [str(line.strip("\n")) for line in file.readlines()]
h = len(paperrolls)
w = len(paperrolls[0]) if h>0 else 0
dirs = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
def neighbors_count(r,c):
cnt = 0
for dr,dc in dirs:
rr,cc = r+dr, c+dc
if 0 <= rr < h and 0 <= cc < w and paperrolls[rr][cc] == '@':
cnt += 1
return cnt
accessible = 0
for r in range(h):
for c in range(w):
if paperrolls[r][c] == '@' and neighbors_count(r,c) < 4:
accessible += 1
print(accessible)