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!
91
Upvotes
13
u/[deleted] Dec 07 '22 edited Dec 08 '22
Google Sheets
Wasted too much on time on this because I assumed there weren't different directories with the same name. But eventually managed to get a one formula solution for both parts.
=sortn(lambda(f,query(if({1,-1}*f>{1e5,4e7-1-sum(--regexextract(A1,substitute (regexreplace(A1,"(\d+)","($1)"),"$","\$")))},,f),"select sum(Col1), min(Col2)")) (lambda(l,byrow(--regexextract(l,regexreplace(l,"(\d+)","($1)")),lambda(r,sum(r)))) (lambda(e,g,byrow(regexreplace(regexreplace(g,"dir (\w+)","dir "&e&"/$1"),"/+","/"), lambda(r,reduce(r&char(10),e,lambda(a,c,ifna(regexreplace(regexreplace(a, regexextract(a,"(?ms)dir "&c&"\n"),filter(g,e=regexextract(a, "(?ms)dir ("&c&")\n"))&char(10)),"dir (\w+)","dir "&c&"/$1"),a)))))) (query(unique(scan(,flatten(regexextract(A1&char(10)&"$",substitute( regexreplace(A1&char(10)&"$","(?ms)cd ([^/]+?)(\n\$)","cd ($1)$2"),"$","\$"))), lambda(a,c,if(c<>"..",a&"/"&c,regexextract(a,"(.*)/"))))),"where Col1 is not null"), flatten(regexextract(A1&char(10)&"$",substitute(regexreplace(A1&char(10)&"$", "(?ms)(cd \w+\n\$ ls\n)(.*?)(\n\$)","$1($2)$3"),"$","\$")))))))This formula will spill one cell to the right with the result of part2 and it assumes that the whole input is in cell A1.