Advent of code d4

I enjoyed this one.

import traceback

def has_symbol(x, y, grid, symbol):
    return grid[y][x] == symbol

def get_neighbour_count(x, y, grid):
    neighbours = 0
    row_count = len(grid)
    col_count = len(grid[0])
    if y > 0 and has_symbol(x, y - 1, grid, "@"): # above
        neighbours += 1
    if y > 0  and x > 0 and has_symbol(x - 1, y - 1, grid, "@"): # above left
        neighbours += 1
    if y > 0  and x < col_count - 1 and has_symbol(x + 1, y - 1, grid, "@"): # above right
        neighbours += 1
    if y < row_count - 1 and has_symbol(x, y + 1, grid, "@"): # below
        neighbours += 1
    if y < row_count - 1 and x > 0 and has_symbol(x - 1, y + 1, grid, "@"): # below left
        neighbours += 1
    if y < row_count - 1 and x < col_count - 1 and has_symbol(x + 1, y + 1, grid, "@"): # below right
        neighbours += 1
    if x > 0 and has_symbol(x - 1, y, grid, "@"): # left
        neighbours += 1
    if x < col_count - 1 and has_symbol(x + 1, y, grid, "@"): # right
        neighbours += 1
    return neighbours

def calc_accessible_rolls(grid):
    rolls = 0
    for row in range(len(grid)):
        for col in range(len(grid[row])):
            if has_symbol(col, row, grid, "@") and get_neighbour_count(col, row, grid) < 4:
                rolls += 1
    return rolls

def calc_accessible_rolls_phase2(grid):
    rolls = 0
    for row in range(len(grid)):
        for col in range(len(grid[row])):
            if has_symbol(col, row, grid, "@") and get_neighbour_count(col, row, grid) < 4:
                grid[row][col] = "x"
                rolls += 1
    return rolls, grid

result = 0
try:
    with open("d4.txt", "r") as f:
        grid = []
        for line in f.readlines():
            line = line.strip()
            if not line:
                continue
            grid.append([v for v in line])
        rolls, updated_grid = calc_accessible_rolls_phase2(grid)
        while rolls != 0:
            result += rolls
            rolls, updated_grid = calc_accessible_rolls_phase2(updated_grid)
except Exception as e:
    traceback.print_exc()

print(result)

Until next time,

Brian