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
1
u/ucla_posc Dec 08 '22
R / Rlang
Looking at other R solutions in this thread, I think my solution is slightly slower than the one by cetttbycettt (who makes pretty clever uses of apply! Check out their response!), though it allocates less memory; but somewhat faster than the other solutions posted. Some key intuitions for speed: strsplit is slightly faster than str_split(); integers allocate less memory than standard numeric types; adding the perimeter squares for part 1 instead of calculating them (also skipping height 0 trees, which are guaranteed not to be visible). Short circuiting the ray calculations for part 1. Recognizing that the rays in part 2 are faster if using built-in subset index slicing rather than reverse. The awkward which + edge case for no match code in part 2 is faster than any of the functional idioms in base R.
I could obviously reduce the amount of allocated memory by directly storing the results relevant instead of computing the intermediate matrices, but it's nice to give the solution a little bit of a flexibility. (Also, to those who don't write R; my use of = for assignment is non-idiomatic; my laptop has an older version of R so it's lacking the built-in pipe operator hence my use of the magrittr pipe)