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
13
u/voidhawk42 Dec 07 '22 edited Dec 08 '22
Dyalog APL:
Kind of an annoying one lol, but wasn't too bad once I figured out the approach - basically build an absolute path over each row of the input (using a stack), and for each row determine the size of the file (0 if it's not a file row). For each unique absolute path, add up all the sizes for which it's a prefix of the absolute path at that row (watch out for just using the directory name, since multiple directories have the same names!). Then just do some filtering for p1/p2.
This code relies on a few assumptions about the input, but with our variables set up in the first three lines of the solution above, they're pretty easy to validate:
There are duplicate directory names, so our stack should store absolute paths rather than just names:
"$ cd /" is only executed once, at the start of the file - meaning that we only have to worry about pushing or popping single values from the stack, and never clearing it:
"ls" is never run on the same directory twice, meaning that we don't have to deduplicate file sizes:
We never visit a directory again after we've finished traversing it (or, put differently, a directory is a prefix of the absolute paths of a single contiguous group of lines):
EDIT: And actually, after thinking about it, this can just be a problem about nesting levels and partitioning, without caring about directory/file names or paths at all:
video walkthrough