11
votes
Day 8: Treetop Tree House
Today's problem description: https://adventofcode.com/2022/day/8
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
My Python solutions, which are surely not optimal but they do the
trick. Between yesterday and today, the problems are starting to get fun!
Part 1
Part 2
Could you share more about how you arrived at this solution? You allege that it is not optimal, which I can't say one way or the other, but I do find it elegant and concise - and something I never would have come up with even if I had days to work on it.
For every tree in the forest, I make a list of the trees above it, below it, to the left, and to the right. For part 1 I check if the tree of interest is taller than the tallest tree in at least one of those four lists. If it is, add it to a tally of "visible" trees. For part 2 I count how far into each of those four lists I have to go before the tree of interest is no longer sufficiently tall. Multiply the four directional counts to get a score and check if it is the largest such score we've seen.
I say it's not optimal because it probably does a lot of redundant computation. There is no caching of e.g. how many trees of a certain height exist in a particular direction. The visibility of a tree and and adjacent neighbor are going to be somewhat similar, but I ignore this and calculate each's visibility from scratch.
Okay, I think I see the main difference in how we approached the problem. You looked at even the first part from the tree perspective, whereas I looked at that from a row and column wise perspective. Even with that I still think I would have made a bunch of loops rather than mapping things into lambdas, but I think that's more an issue of not actually spending that much time in Python and not really having a good handle on functional approaches than anything else.
I had problem with electricity (almost all day) and work. But I still made it in this day, yahoo!
Python
Only one sad thing: I made it as two tasks, not combined. Maybe later I will try to make it as one.
I'm using Rust this year, and trying to keep it
std
-only throughout the month. Like yesterday I didn't know how to avoid allocation on this one, but at least it's all up front today!Part 1
Part 2
Elixir
Since these kinds of character-grid based puzzle inputs are so common, I created a
CharGrid
module in previous years that simplifies parsing and lookups. I only needed to add one newlines_of_cells/3
function (plus a couple related convenience functions) for this puzzle—you give it a set of target coordinates, and it returns a list of lists of the cells radiating out from that location in each direction.After that, I just needed to write code that counts the number of cells that satisfy the conditions for each puzzle part.
CharGrid
moduleOnly including the relevant parts for this puzzle.
Solution
Edit: Made the solution cleaner (but slightly slower) by removing code that ignores the grid perimeter.
Rust
I'm not very happy with my code today. Feels like I violated DRY a lot. Ah well.
Rust
Well I certainly repeated myself in this code, and Python's "don't modify while iterating" concept is not one I am fond of, but it works.
Part 1 and 2, Python
Not particularly elegant but it works. :P
Part 1
Part 2
Horrendously slow because I was absolutely convinced that I should count visible trees between the treehouse and the final blocking tree. Of course the sample data gives the same answer.
when the data are too large to deal with by eyeballing. Even harder when you are looking for answers using the wrong question. Also, horrendously verbose code :(
Utilities
My forest is a nested list.
Part 1
Old lispers know a neat to tranpose nested lists:
(apply map list xs))
Happy to explain it if anyone asks.
Part 2
Part 2 is a similar approach, tranposing the forest, but it was easier to find the viewing distance by converting to a vector.
Ruby
Part 1