Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

Just messily brute forced this one, there's probably a far better way of doing it. Took about 20 minutes overall.

foo = []
with open('day8_input.txt', 'r') as inp:
    foo = [[*i] for i in inp.read().split('\n')]


def visible_check(y, x):
    flag = True
    for n in range(x):
        if int(foo[y][n]) >= int(foo[y][x]):
            flag = False
            break
    if flag:
        return True
    flag = True
    for n in range(x+1, len(foo[y])):
        if int(foo[y][n]) >= int(foo[y][x]):
            flag = False
            break
    if flag:
        return True
    flag = True
    for n in range(y):
        if int(foo[n][x]) >= int(foo[y][x]):
            flag = False
            break
    if flag:
        return True
    flag = True
    for n in range(y+1, len(foo)):
        if int(foo[n][x]) >= int(foo[y][x]):
            flag = False
            break
    return flag


def scenic_score(y, x):
    left = 0
    right = 0
    up = 0
    down = 0
    for n in range(x-1, -1, -1):
        left += 1
        if int(foo[y][n]) >= int(foo[y][x]):
            break
    for n in range(x+1, len(foo[y])):
        right += 1
        if int(foo[y][n]) >= int(foo[y][x]):
            break
    for n in range(y-1, -1, -1):
        up += 1
        if int(foo[n][x]) >= int(foo[y][x]):
            break
    for n in range(y+1, len(foo)):
        down += 1
        if int(foo[n][x]) >= int(foo[y][x]):
            break    
    return left*right*up*down


visible = len(foo)*2 + (len(foo[0])-2)*2
max_score = 0
for y in range(1, len(foo)-1):
    for x in range(1, len(foo[y])-1):
        if visible_check(y, x):
            visible += 1
        score = scenic_score(y, x)
        if score > max_score:
            max_score = score
print(visible, max_score)
Jump in the discussion.

No email address required.

I don't have enough spoons to read this shit

Jump in the discussion.

No email address required.



Now playing: Aquatic Ambience (A Hint of Blue remix) (DKC).mp3

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