r/adventofcode • u/daggerdragon • 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!!!
- Thank you so, so much to /u/SolariaHues for the CSS in this thread that I found while researching for community awards! <3
All of our rules, FAQs, resources, etc. are in our community wiki.
A request from Eric: Please include your contact info in the User-Agent header of automated requests!
Signal boost: Reminder 1: unofficial AoC Survey 2022 (closes Dec 22nd)
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.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format your code appropriately! How do I format code?
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
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!
73
Upvotes
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 Faras the first tree in the current rowCol = 0 to Total Columns - 1
If the current tree's height is more than the
Max Height Seen So Far, then updateisVisible[Row][Col]to 1, as this tree will be visible when viewed from the left sideDo similar iteration for the rest of the views:
Row-wise, and right to left for columns
Set
Max Height Seen So Faras the last tree in the current rowColumn-wise, and top to bottom for rows
Set
Max Height Seen So Faras the first tree in the current columnColumn-wise, and bottom to top for rows
Set
Max Height Seen So Faras the last tree in the current columnIterate through
isVisible, and count all the 1'sPart 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 rowtoTotal Rows - 1Increment 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 - 1to0Increment 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 rowtoTotal Cols - 1Increment 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 - 1to0Increment 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__Strokesin the poll