Files
advent-of-code-2025/day4/part2.py
2025-12-07 12:20:13 -08:00

38 lines
1.0 KiB
Python

FILE = "input.txt"
options = [
(-1, -1), (-1, 0), (0, -1), (1, 1), (1, 0), (0, 1), (-1, 1), (1, -1)
]
def main():
total = 0
grid = [list(line.strip()) for line in open(FILE)]
while True:
accessible, grid = removal_loop(grid)
if accessible == 0: break
total += accessible
return total
def removal_loop(grid: list[list[str]]) -> tuple[int, list[list[str]]]:
accessible = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] != '@': continue
nearby = [
(i + x, j + y)
for (x, y) in options
if 0 <= i+x < len(grid) and 0 <= j+y < len(grid[0])
]
nearbyRolls = [
grid[x][y]
for (x, y) in nearby
if grid[x][y] == '@'
]
if len(nearbyRolls) < 4:
accessible += 1
grid[i][j] = '.'
return accessible, grid
if __name__ == "__main__":
print(main())