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)