Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

got tripped up for a long time by the dumbest stuff on this one. for the first part i was giving tlm() the wrong slices (row[i+1:] instead of row[x+1:] etc) then in the second part i was reversing the wrong slices, which annoyingly gave me the right answer for the example input

with open("day8input.txt") as f:
    input = f.read()

rows = input.split("\n")
columns = list(zip(*[row for row in rows]))

# part 1

visible = 0

def tlm(tree, slice):
    for t in slice:
        if int(t) >= int(tree):
            return False
    return True

for i, row in enumerate(rows):
    for x, column in enumerate(columns):
        tree = column[i]
        if i == 0 or x == 0 or i == len(rows) - 1 or x == len(columns) - 1:
            visible += 1
        elif tlm(tree, row[x+1:]) or tlm(tree, row[:x]) or tlm(tree, column[i+1:]) or tlm(tree, column[:i]):
            visible += 1

print(visible)

# part 2

def score(tree, slice):
    if not slice: return 0
    for i, t in enumerate(slice):
        if int(t) >= int(tree) or i == len(slice) - 1:
            return int(i) + 1


best_score = 0
for i, row in enumerate(rows):
    for x, column in enumerate(columns):
        tree = row[x]
        keffals = score(tree, row[x+1:]) * score(tree, list(reversed(row[:x]))) * score(tree, column[i+1:]) * score(tree, list(reversed(column[:i])))
        if keffals > best_score:
            best_score = keffals

print(best_score)
Jump in the discussion.

No email address required.

first i was fricking up column definitions when looping, which made me think i was fricking up slicing. then i was fricking up view distances by feeding the wrong arrays into the wrong variables. then i was fricking up view distances both in making infinite loops and in appropriately scoring views.

:#marseycry:

Jump in the discussion.

No email address required.

:#marseycheerup: it's so easy for your brain to turn into mush on this stuff

Jump in the discussion.

No email address required.

>turn into

:#marseyconfused:

Jump in the discussion.

No email address required.

Sorry ma'am, looks like his delusions have gotten worse. We'll have to admit him.

Jump in the discussion.

No email address required.

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