Advent of Code 2022 day 8

Took too long on part 2 again. Didn’t realise you could still see the tree even if it was taller, hence all of the additional {var} += 1. I’m getting speedier at part 1 which is good to see. Embarrassingly, I didn’t realise the keyword I wanted is called break and not continue. Oops.

with open("8.txt") as f:
    lines = f.read().splitlines()

board = [[x for x in line] for line in lines]

def is_visible(board, y, x):
    if all(board[y][i] < board[y][x] for i in range(x)):
        return True
    if all(board[j][x] < board[y][x] for j in range(y)):
        return True
    if all(board[y][i] < board[y][x] for i in range(x+1, len(board[y]))):
        return True
    if all(board[j][x] < board[y][x] for j in range(y+1, len(board))):
        return True
    return False

count = 0
for y in range(len(board)):
    for x in range(len(board[y])):
        if is_visible(board, y, x):
            count += 1
print(count)

# part 2
def scenic_score(board, y, x):
    a = 0
    for i in range(x-1, -1, -1):
        if board[y][i] < board[y][x]:
            a += 1
        elif board[y][i] == board[y][x]:
            a += 1
            break
        else:
            a += 1
            break
    b = 0
    for j in range(y-1, -1, -1):
        if board[j][x] < board[y][x]:
            b += 1
        elif board[j][x] == board[y][x]:
            b += 1
            break
        else:
            b += 1
            break
    c = 0
    for i in range(x+1, len(board[y])):
        if board[y][i] < board[y][x]:
            c += 1
        elif board[y][i] == board[y][x]:
            c += 1
            break
        else:
            c += 1
            break
    d = 0
    for j in range(y+1, len(board)):
        if board[j][x] < board[y][x]:
            d += 1
        elif board[j][x] == board[y][x]:
            d += 1
            break
        else:
            d += 1
            break
    return a*b*c*d

max_score = 0
for y in range(len(board)):
    for x in range(len(board[y])):
        if (score:=scenic_score(board, y, x)) > max_score:
            max_score = score
print(max_score)