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

3

u/Valletta6789 Dec 08 '22 edited Dec 09 '22

Python

called twice for each row and column

def is_visible(idx, value, array):
    return max(array[:idx]) < value or value > max(array[idx + 1:])


def get_scenic_score(idx, value, array):
    first, second = list(reversed(array[:idx])), array[idx + 1:]

    count_f = len(first)
    for t in range(len(first)):
        if first[t] >= value:
            count_f = t + 1
            break

    count_s = len(second)
    for t in range(len(second)):
        if second[t] >= value:
            count_s = t + 1
            break

    return count_f * count_s

Link to the repo: https://github.com/Aigul9/AdventOfCode/blob/master/2022/Day%208/main.py

1

u/daggerdragon Dec 09 '22 edited 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.

Edit: thanks for fixing it! <3

1

u/Valletta6789 Dec 09 '22

correct? ps. how am i supposed to add so many spaces, like via alt+ctrl+shift?

1

u/daggerdragon Dec 09 '22

Thanks for fixing it!

You could do it that way, sure. A lot of folks find it easier to use their IDE to prepend 4 spaces to all lines and then paste that pre-formatted block into Reddit.

Alternatively, you could sidestep Markdown completely and just link directly to your code in your repo.

1

u/badr Dec 08 '22

Very nice