aoc2025/bob/day04/day04-p2.py

55 lines
1.2 KiB
Python

def get_neighbours(layout, x, y):
neighbours = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
if dx == dy == 0:
continue
# Skip if we are outside of bounds
if not 0 <= y+dy < len(layout):
continue
if not 0 <= x+dx < len(layout[y+dy]):
continue
if layout[y+dy][x+dx] != ".":
neighbours += 1
return neighbours
def walk_map(layout):
free_roles = 0
for y in range(0,len(layout)):
for x in range(0, len(layout[y])):
if layout[y][x] != ".":
neighbours = get_neighbours(layout, x, y)
if neighbours < 4:
free_roles += 1
layout[y][x] = "."
return free_roles
def read_input(filename):
layout = []
for line in open(filename).read().splitlines():
layout.append(list(line))
return layout
# filename = "day04/example_input" # 43
filename = "day04/input" # 8013
layout = read_input(filename)
rolls = 1
removed_rolls = 0
while rolls > 0:
rolls = walk_map(layout)
removed_rolls += rolls
print(f"Total removed rolls: {removed_rolls}")