Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

With the magic of transpose :marseytrans2: it still took me too long. Cleaned up my mess

with open('input08.txt') as file:
    lines = [[int(i) for i in list(line.strip())] for line in file]
t_lines = np.transpose(lines.copy())


#part a
count_vis = 0
height = len(lines)
width = len(lines[0])
for y in range(height):
    for x in range(width):
        value = lines[y][x]
        left = -1 if x==0 else max(lines[y][:x])
        right = -1 if x==width-1 else max(lines[y][x+1:])
        up = -1 if y==0 else max(t_lines[x][:y])
        down = -1 if y==height-1 else max(t_lines[x][y+1:])
        if value > left or value > right or value > up or value > down:
            count_vis += 1
print(count_vis)


#part b
def get_score(x, y, horizontal, vertical):
    height = horizontal[x]
    count_left = distance(height, np.flip(horizontal[:x]))
    count_right = distance(height, horizontal[x+1:])
    count_up = distance(height, np.flip(vertical[:y]))
    count_down = distance(height, vertical[y+1:])
    return count_left*count_right*count_up*count_down
    
def distance(height, slice):
    count = 0
    for tree in slice:
        count += 1
        if tree >= height:
            return count
    return count

best_score = 0
height = len(lines)
width = len(lines[0])
for y in range(height):
    for x in range(width):
        score = get_score(x,y,lines[y], t_lines[x])
        if score > best_score:
            best_score = score
print(best_score)
Jump in the discussion.

No email address required.



Now playing: Donkey Kong Country Theme (Sunderi Remix) (DKC).mp3

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