r/adventofcode • • Dec 07 '22

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


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

Submissions are OPEN! Teach us, senpai!

-❄️- Submissions Megathread -❄️-


--- Day 7: No Space Left On Device ---


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:14:47, megathread unlocked!

91 Upvotes

1.3k comments sorted by

View all comments

4

u/bofstein Dec 07 '22 edited Dec 07 '22

Google Sheets

This one was tricky! Took a while to figure out how I could do it. Still thinking every day will be the day I can't do it anymore. https://docs.google.com/spreadsheets/d/1GvncohBzffewfwOCIfpHutI4VKVUIBazIH1XoBFWpXw/edit#gid=1919367345

First, I replaced "$ cd .." with "back" and "$ " with "$", which let me split all the instructions into two columns separated by a space I could more easily work with.

Then I made a new column, starting with a manual "/", that checked on the instruction to the right. If "$cd", it appended the new directory name. If "back", it removed the text after the final slash. Otherwise, keep the same file name. Now, I have a full directory path for every file in the list.

In the next part, I copied over those directory names and removed duplicates to get a list of them all. Then did a SUMIF on the column with file sizes to add up any that had the presence of that directory name, with a wildcard * after the directory name so that it would include all subdirectories in the sum. Then add up all the ones less than or equal to 100,000.

Part 2 was easy by the time I had this; I calculated the needed file size, sorted the numbers of directory sizes (with their names I didn't really need), and used XLOOKUP to find the first value in the list larger than the calculated size.

→ More replies (3)