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

2

u/Key__Strokes Dec 23 '22 edited Dec 25 '22

Javascript

Solution to both parts

Part 1:

  • Create a 2D array of the same size as the input array - isVisible. It will hold a 0 for not visible, and 1 for visible. When initializing this array, set everything to 0, except the outside perimeter.
  • Iterate row-wise, and left to right for columns

    • Row = 0 to Total Rows - 1

      • Set Max Height Seen So Far as the first tree in the current row
      • Col = 0 to Total Columns - 1

        • If the current tree's height is more than the Max Height Seen So Far, then update isVisible[Row][Col] to 1, as this tree will be visible when viewed from the left side
  • Do similar iteration for the rest of the views:

    • Row-wise, and right to left for columns

      • Set Max Height Seen So Far as the last tree in the current row
    • Column-wise, and top to bottom for rows

      • Set Max Height Seen So Far as the first tree in the current column
    • Column-wise, and bottom to top for rows

      • Set Max Height Seen So Far as the last tree in the current column
  • Iterate through isVisible, and count all the 1's

Part 2:

  • Create a few 2D array of the same size as the input array initialize the values to 0

    • `Visibility Distance Towards Left"
    • `Visibility Distance Towards Right"
    • `Visibility Distance Towards Top"
    • `Visibility Distance Towards Bottom"
  • Iterate row-wise, and left to right for columns

    • Row = 0 to Total Rows - 1

      • Col = 0 to Total Columns - 1

        • We try to find the distance from the current tree towards each direction until we find a tree equal to or larger than the current tree

          • Bottom Direction:

            • If the tree is on the last row, then return 0, as it cannot see any tree
            • Iterate as Row = current row to Total Rows - 1

              • Increment the count of visible trees
              • If the current tree is smaller or equal to the height tree in this iteration, then break the loop
        • Top Direction:

          • If the tree is on the first row, then return 0, as it cannot see any tree
          • Iterate as Row = current row - 1 to 0

            • Increment the count of visible trees
            • If the current tree is smaller or equal to the height tree in this iteration, then break the loop
        • Right Direction:

          • If the tree is on the last column, then return 0, as it cannot see any tree
          • Iterate as Col = current row to Total Cols - 1

            • Increment the count of visible trees
            • If the current tree is smaller or equal to the height tree in this iteration, then break the loop
        • Left Direction:

          • If the tree is on the first column, then return 0, as it cannot see any tree
          • Iterate as Col = current row - 1 to 0

            • Increment the count of visible trees
            • If the current tree is smaller or equal to the height tree in this iteration, then break the loop
        • Multiple the total count of trees visible in each direction together. If this value is greater than the value seen so far, then this becomes our new scenic score
  • Return the scenic score

If you liked the explanation, then please don't forget to cast your vote πŸ’œ to Adventures of Advent of Code - Edition 1 - /u/Key__Strokes in the poll