Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

this one was crazy, mostly because I kept misunderstanding the problem and making really pretty but incorrect solutions :marseyraging:

import numpy as np

lines = []
with open("8.input", "r") as f:
    lines = f.readlines()

forest = np.array([[int(j) for j in i.strip()] for i in lines])

def process_line(line):
    left_vis = np.zeros(shape=line.shape,dtype=int)
    right_vis = np.zeros(shape=line.shape,dtype=int)
    for i in range(len(line)):
        left_index = i
        right_index = len(line)-1-i
        left_vis[left_index] = int(len(np.where(line[:left_index] >= line[left_index])[0]) == 0)
        right_vis[right_index] = int(len(np.where(line[right_index+1:] >= line[right_index])[0]) == 0)
        
    return left_vis|right_vis
        
def process_line_2(line):
    left_score = np.ones(shape=line.shape,dtype=int)
    right_score = np.ones(shape=line.shape,dtype=int)
    for i in range(len(line)):
        left_index = i
        right_index = len(line)-1-i
        left_score[left_index] = left_index - max(list(np.where(line[0:left_index] >= line[left_index])[0]) + [0])
        right_score[right_index] = min([right_index+1+j for j in list(np.where(line[right_index+1:] >= line[right_index])[0])] + [len(line) - 1]) - right_index
    return left_score*right_score
        
def construct_matrix_x(processor, input):
    to_return = np.ones(shape = input.shape, dtype=int)
    for y in range(input.shape[1]):
        to_return[:,y] = processor(input[:,y])
    return to_return

def construct_matrix_y(processor, input):
    to_return = np.ones(shape = input.shape, dtype=int)
    for x in range(input.shape[0]):
        to_return[x,:] = processor(input[x,:])
    return to_return

part_1 = len(np.where(construct_matrix_y(process_line, forest)|construct_matrix_x(process_line, forest) == 1)[0])
part_2 = np.max(construct_matrix_x(process_line_2, forest) * construct_matrix_y(process_line_2, forest))
print(part_1)
print(part_2)
assert part_1 == 1713
assert part_2 == 268464
Jump in the discussion.

No email address required.

Good job friend :marseysadpat:

Jump in the discussion.

No email address required.

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