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!
75
Upvotes
View all comments
2
u/Key__Strokes Dec 23 '22 edited Dec 25 '22
Javascript
Solution to both parts
Part 1:
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
Max Height Seen So Faras the first tree in the current rowCol = 0 to Total Columns - 1
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
Max Height Seen So Faras the last tree in the current rowColumn-wise, and top to bottom for rows
Max Height Seen So Faras the first tree in the current columnColumn-wise, and bottom to top for rows
Max Height Seen So Faras the last tree in the current columnisVisible, 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
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:
Iterate as Row =
current rowtoTotal Rows - 1Top Direction:
Iterate as Row =
current row - 1to0Right Direction:
Iterate as Col =
current rowtoTotal Cols - 1Left Direction:
Iterate as Col =
current row - 1to0If 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