Solve day4

This commit is contained in:
2025-12-07 12:20:13 -08:00
parent 953d4e4df8
commit 802506d860
4 changed files with 214 additions and 0 deletions

37
day4/part2.py Normal file
View File

@@ -0,0 +1,37 @@
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())