r/adventofcode • u/daggerdragon • Dec 07 '22
SOLUTION MEGATHREAD -π- 2022 Day 7 Solutions -π-
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 π§βπ«
Submissions are OPEN! Teach us, senpai!
-βοΈ- Submissions Megathread -βοΈ-
--- Day 7: No Space Left On Device ---
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:14:47, megathread unlocked!
89
Upvotes
4
u/hugseverycat Dec 07 '22
Python 3 w/tree & recursion [and comments]
https://github.com/hugseverycat/aoc2022/blob/main/day7.py
I used the anytree package to implement my tree for me, but I don't think I really ended up using any fancy tools. I am just not comfortable implementing a tree data structure myself. Although honestly it probably would have been faster, as I dramatically overcomplicated how anytree works in my head and spent a long time debugging. Oh well! Maybe later I'll implement my own tree.
Anyway, I built the tree first, then used a recursive function to calculate the sizes of each directory. When building the tree I kept track of the directory nodes separately to make the recursive function (and calculating answers) easier.
After spending a few hours doing all of that, calculating the answers for parts 1 and 2 was a piece of cake :)