Solve day4
This commit is contained in:
37
day4/part2.py
Normal file
37
day4/part2.py
Normal 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())
|
||||
Reference in New Issue
Block a user