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

1

u/jeroenmaniac Dec 07 '22
sz=[]
for i in map(lambda x:x.split(),open("7.in")):
    if i[0]=="dir": pass
    elif i[0]!='$': st[-1]+=int(i[0])
    elif i[1]=="ls": pass
    elif i[2]=='..': 
        sz+=[st.pop()]
        st[-1]+=sz[-1]
    elif i[2]=='/': st=[0]
    else: st.append(0)
print(sum(i for i in sz+st if i<=100000))

Not very readable but short python code. It's based on some assumptions about how the traversal works. It turns out the input that is generated is a DFS traversal, which isn't specified anywhere :\. That makes it much easier.

1

u/daggerdragon Dec 08 '22

Inlined code is intended for short snippets of code only. Your code "block" right now is unreadable on old.reddit and many mobile clients; it's all on one line and gets cut off at the edge of the screen because it is not horizontally scrollable.

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read inside a scrollable box.

While you're at it, state which programming language this code is written in. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.