Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

Tried to do a repetitive version optimized to solve part 1 (only do a single pass of each row/col instead of calculating visibility for each coord independently), but that bit me slightly for part 2.

from collections import defaultdict

def import_input(name):
    with open(f'AdventOfCode2022/{name}.txt', 'r') as input:
        commands = input.read().split('\n')
        if commands[-1] == '':
            print(commands.pop())
        return commands

data = import_input('day_8_input')

length = len(data[0]) #cols
width = len(data) #rows

entries = defaultdict(lambda: 1)

def check_left(row):
    curr_height = int(data[row][0])
    if curr_height != 9:
        for col in range(1, length-1):
            if int(data[row][col]) > curr_height:
                curr_height = int(data[row][col])
                entries[(row, col)] # will initialize to 1 if doesnt exist, else does nothing to value
                if curr_height == 9:
                    break
            else:
                #entries[(row, col)] = 0
                pass

def check_right(row):
    curr_height = int(data[row][length-1])
    if curr_height != 9:
        for col in range(length-2, 0, -1):
            if int(data[row][col]) > curr_height:
                curr_height = int(data[row][col])
                entries[(row, col)] # will initialize to 1 if doesnt exist, else does nothing to value
                if curr_height == 9:
                    break
            else:
                #entries[(row, col)] = 0
                pass

def check_top(col):
    curr_height = int(data[0][col])
    if curr_height != 9:
        for row in range(1, width-1):
            if int(data[row][col]) > curr_height:
                curr_height = int(data[row][col])
                entries[(row, col)] # will initialize to 1 if doesnt exist, else does nothing to value
                if curr_height == 9:
                    break
            else:
                #entries[(row, col)] = 0
                pass

def check_bottom(col):
    curr_height = int(data[width-1][col])
    if curr_height != 9:
        for row in range(width-2, 0, -1):
            if int(data[row][col]) > curr_height:
                curr_height = int(data[row][col])
                entries[(row, col)] # will initialize to 1 if doesnt exist, else does nothing to value
                if curr_height == 9:
                    break
            else:
                #entries[(row, col)] = 0
                pass

for i in range(1, width-1):
    check_left(i)
    check_right(i)

for i in range(1, length-1):
    check_top(i)
    check_bottom(i)

taken_for_granted = 2 * width + 2 * length - 4 #double counting corners

print(sum([v for v in entries.values()]) + taken_for_granted)


#Part 2

def calc_scenic_score(row, col, data=data, length=length, width=width):
    tree_height = int(data[row][col])

    scores = defaultdict(int)

    for i in range(col-1, -1, -1):
        scores['left'] += 1
        if tree_height <= int(data[row][i]):
            break

    #check right
    for i in range(col+1, length, 1):
        scores['right'] += 1
        if tree_height <= int(data[row][i]):
            break

    #check up
    for i in range(row-1, -1, -1):
        scores['up'] += 1
        if tree_height <= int(data[i][col]):
            break

    #check down
    for i in range(row+1, width, 1):
        scores['down'] += 1
        if tree_height <= int(data[i][col]):
            break

    product = 1
    for value in scores.values():
        product = product * value
    return product

#i = row = width, j = col = length
print(max([calc_scenic_score(i, j) for i in range(1, width-1) for j in range(1, length-1)]))
Jump in the discussion.

No email address required.

Posts like this is why I do Heroine.

Jump in the discussion.

No email address required.

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