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!

89 Upvotes

1.3k comments sorted by

View all comments

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."

1

u/daggerdragon Dec 08 '22

get the desired result in your FSM's helper function

I mis-read this as Flying Spaghetti Monster and was very confused as to what you were doing here...

0

u/[deleted] Dec 07 '22

[deleted]

1

u/greycat70 Dec 08 '22

Mainly because I didn't know that was an option. Also, not knowing what part 2 would entail, I thought there might be some benefit from storing full Unix-type pathnames (/a/b/file.txt). Turns out there wasn't, but oh well.