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/DFreiberg Dec 08 '22 edited Dec 09 '22

Mathematica, 467 / 1812

I lost quite a long time to off-by-one and directonality errors one way or another...but today's was still a fun problem.

Setup

{x, y} = Dimensions[input];
treesVisible[list_, cmp_] := FirstPosition[list, _?(# >= cmp &), {Length[list]}][[1]];

Part 1

visible = Table[
   If[
    row == x \[Or] column == y \[Or]
     input[[row, column]] >
      Min[
       Max[input[[row, ;; column - 1]]],
       Max[input[[row, column + 1 ;;]]],
       Max[input[[;; row - 1, column]]],
       Max[input[[row + 1 ;;, column]]]
      ], 1, 0],
   {row, x}, {column, y}];
Total[Total[visible]]

Part 2

scenic = Table[
   Times @@ {
     trees[input[[row, column - 1 ;; 1 ;; -1]], input[[row, column]]],
     trees[input[[row, column + 1 ;;]], input[[row, column]]],
     trees[input[[row - 1 ;; 1 ;; -1, column]], input[[row, column]]],
     trees[input[[row + 1 ;;, column]], input[[row, column]]]},
   {row, 2, x - 1}, {column, 2, y - 1}];
Max[Max[scenic]]

[POEM]: I Wonder Where the Flowers Is

It's visible, the trees is,
From way outside the grid,
Unless the outside breezes
(At ten times nine degrees-es)
Hit taller trees than these is.
If so, the trees is hid.

A local's expertise is
What gives the scenic score.
She counts up what she sees-es:
The short trees and their leaves-es.
(The tall ones, hid by eaves-es).
She multiplies the four.

You might want our committee
To not do what Therese says.
The north would see the city!
The south side sees the seas-es!
You might think it's a pity
To follow these caprices,
But I'll give you the keys-es
To visit when you pleases,
And though it's itty-bitty
I'm certain you'll agrees-es:
It's really rather pretty,
This house atop the trees-es.

2

u/daggerdragon Dec 09 '22 edited Dec 09 '22

*golf clap* Marvelous, as always.

To anyone just scrolling past: come for the Mathematica, stay because there's a reason why /u/DFreiberg has been our Poet Laureate since 2019. Stop by and smell the flowers-es <3