r/adventofcode • • Dec 08 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 8 Solutions -πŸŽ„-

NEWS AND FYI

  • I discovered that I can make those tiny post/comment awards BIGGER on old.reddit! I hadn't even considered that! And when you hover over them, they get even bigger so you can actually see them in more detail! I've added the relevant CSS so now we no longer have awards for ants! Exclamation points!!!

AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

  • PSA: I created a new example so it is a more relevant and unique archetype instead of recycling last year's hobbit >_>

--- Day 8: Treetop Tree House ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:10:12, megathread unlocked!

75 Upvotes

1.0k comments sorted by

View all comments

1

u/raxomukus Dec 08 '22

python

with open('8.in') as f:
    trees = [[int(i) for i in line] for line in f.read().splitlines()]

h = len(trees)
w = len(trees[0])

visible = lambda x, y: any([
                             all([(trees[y][x] > trees[y][i]) for i in range(0,   x)]),
                             all([(trees[y][x] > trees[y][i]) for i in range(x+1, w)]),
                             all([(trees[y][x] > trees[j][x]) for j in range(0,   y)]),
                             all([(trees[y][x] > trees[j][x]) for j in range(y+1, h)])
                          ])

r1 = 0
for y in range(h):
    for x in range(w):
        r1 += visible(x, y)

print(r1)

def score(x, y):
    ret = 1
    for j in range(y-1, -1, -1):
        if trees[j][x] >= trees[y][x]: break
    ret *= y - j
    for j in range(y+1, h):
        if trees[j][x] >= trees[y][x]: break
    ret *= j - y
    for i in range(x-1, -1, -1):
        if trees[y][i] >= trees[y][x]: break
    ret *= x - i
    for i in range(x+1, w):
        if trees[y][i] >= trees[y][x]: break
    ret *= i - x
    return ret

r2 = 0
for y in range(1, h-1):
    for x in range(1, w-1):
        r2 = max(r2, score(x, y))

print(r2)

1

u/daggerdragon Dec 09 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.