Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

There's probably some way to simplify the visibility search instead of a separate for loop for each direction. I'm too lazy to find it

file = open("input.txt")
grid = []

for line in file:
    grid.append(line.rstrip())

def check_visibility(i,j):
    score = 1
    v = 0
    global grid
    score_tmp = 0
    for l in range(j-1, -1, -1):
        score_tmp += 1
        if int(grid[i][j]) <=  int(grid[i][l]):        
            v += 1
            break
    score *= score_tmp
    score_tmp = 0
    for l in range(j+1, len(grid[i])):
        score_tmp += 1
        if int(grid[i][j]) <=  int(grid[i][l]):  
            v += 1
            break
    score *= score_tmp
    score_tmp = 0
    for l in range(i-1, -1, -1):
        score_tmp += 1
        if int(grid[i][j]) <=  int(grid[l][j]): 
            v += 1
            break
    score *= score_tmp
    score_tmp = 0
    for l in range(i+1, len(grid)):
        score_tmp += 1
        if int(grid[i][j]) <=  int(grid[l][j]):     
            v += 1
            break
    if (i == 0) or (j == 0) or (i == len(grid)-1) or (j == len(grid[i])-1):
        v = 0
    score *= score_tmp
    return score, v


v_c = 0
maxScore = 0
for line in range(0,len(grid)):
    for tree in range(0,len(grid[line])):
        visible = False
        t_s=0
        v = False
        if (line == 0) or (tree == 0) or (line == len(grid)-1) or (tree == len(grid[line])-1):
            continue
        t_s,v  = check_visibility(line, tree)
        if t_s > maxScore: 
            maxScore = t_s
        if v != 4:
            v_c +=1
        
            
print("Max Visibility Score: " + str(maxScore))
print("Number of Visible Trees: " + str(v_c))
Jump in the discussion.

No email address required.

If only you could put that energy into your relationships

Jump in the discussion.

No email address required.



Now playing: King K. Rool Returns (DKC2).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.