aoc2025/brechtje/4/4-2.py

46 lines
1.1 KiB
Python

raw_input = open('input.txt', 'r').read().splitlines()
input = []
for line in raw_input:
input.append(list(line))
surrounding_spaces = [
{'x': -1, 'y': -1},
{'x': 0, 'y': -1},
{'x': 1, 'y': -1},
{'x': -1, 'y': 0},
{'x': 1, 'y': 0},
{'x': -1, 'y': 1},
{'x': 0, 'y': 1},
{'x': 1, 'y': 1}
]
prev_answer = 0
answer = 0
x_max = len(input[0]) - 1
y_max = len(input) - 1
while True:
for y, line in enumerate(input):
for x, char in enumerate(line):
if char == "@":
rolls = 0
for space in surrounding_spaces:
current_x = x + space['x']
current_y = y + space['y']
if current_x < 0 or current_y < 0 or current_x > x_max or current_y > y_max:
continue
elif input[current_y][current_x] == "@":
rolls += 1
if rolls > 3:
break
if rolls < 4:
answer += 1
input[y][x] = "."
if answer == prev_answer:
break
prev_answer = answer
print(answer)