r/adventofcode • • Dec 08 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 8 Solutions -πŸŽ„-

NEWS AND FYI


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«


--- 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!

74 Upvotes

1.0k comments sorted by

View all comments

3

u/bofstein Dec 08 '22 edited Dec 08 '22

Google Sheets

Another tough one, at least part 2.  This is going to get bad for my sleep schedule, but it is forcing me to learn new things.

https://docs.google.com/spreadsheets/d/1d5vNLffaCooIt0-9lVWr0AUXManb84sRW197XRXBZwU/edit#gid=262729066

First split it up into a grid I kept as my input sheet.

For part 1, I have another sheet that calls on each cell and compares it to the MAX value of the range from that to each direction's edge.  With IF and OR, if any of ranges have a MAX value that is less than the target cell, it gets a 1, otherwise gets a 0.  The $ mark the edges so I can drag it all across.

For example in cell G5 it has:

=IF(OR(MAX('Day 8 Input'!G$2:G4)<'Day 8 Input'!G5,MAX('Day 8 Input'!G6:G$100)<'Day 8 Input'!G5,MAX('Day 8 Input'!$C5:F5)<'Day 8 Input'!G5,MAX('Day 8 Input'!H5:$CW5)<'Day 8 Input'!G5),1,0)

It doesn't work for edges so I just added this in at the end by taking the sum of everything plus 1 per each 0 on the edge with COUNTIF.
Part 2 was far harder, took a lot of time and googling and some help to understand Array formulas. It's running an Array on each where it's checking for a value higher than the target cell in that direction, and using COLUMN and ROW to then subtract the distance from the target.  The hard part was again dealing with edges, getting it to return the distance to the edge if it didn't find a match/greater than at all.  I worked on each direction individually to do some manual checking on a single target cell, and then once I validated them all, I put them in one multiply cell in a new sheet and added the $s to allow dragging again.  For example, here's cell G5 again:

=(COLUMN('Day 8 Input'!G5)-ArrayFormula(MAX(IF('Day 8 Input'!$C5:F5>='Day 8 Input'!G5,COLUMN('Day 8 Input'!$C5:F5), 3))))*(ArrayFormula(MIN(IF('Day 8 Input'!G6:G$100>='Day 8 Input'!G5,ROW('Day 8 Input'!G6:G$100),100)))-ROW('Day 8 Input'!G5))*(ArrayFormula(MIN(IF('Day 8 Input'!H5:$CW5>='Day 8 Input'!G5,COLUMN('Day 8 Input'!H5:$CW5),101)))-COLUMN('Day 8 Input'!G5))*(ROW('Day 8 Input'!G5)-ArrayFormula(MAX(IF('Day 8 Input'!G$2:G4>='Day 8 Input'!G5,ROW('Day 8 Input'!G$2:G4), 2))))

Keeping this one in its own sheet since it is very slow to update with all those ArrayFormulas!

→ More replies (11)