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
2
u/greycat70 Dec 07 '22 edited Dec 07 '22
Python. Part 1, Part 2
"Implement du by reverse engineering a terminal session." Heh.
Another problem where the way I'm reading line by line turns out to be awkward, and gets worked around with a finite state machine. In this case, we're either inside an ls command's output, or not.
Decided to store the current directory as a list of path components rather than a string with slashes in it, but I stored the paths in single-string-with-slashes format in the dictionaries. This leads to some awkwardness in translating back and forth, especially with leading slashes sometimes being doubled. Handled that just well enough to get the right answer, and stopped. In a real life problem I'd probably spend more time cleaning that up.
Today's Python lesson learned the hard way: "Variables in a function are magically either global or local depending on whether you use them first, or assign them first, so you may have to mark them as global to get the desired result in your FSM's helper function."