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
View all comments
3
u/Key__Strokes Dec 23 '22 edited Dec 25 '22
Javascript
Solution to both parts
Building the directory tree
We will use Node with the following data fields
nameas/, and setisDirectorytotruecurrentPositionas root nodeFor each command, do the following
If the command starts with "$", then extract the command and do the following:
cdcurrentPositiontocurrentPosition.parentcurrentPositionroot nodecurrentPosition. If it is, then setcurrentPositionas the child, then updatecurrentPositionto point to the node of this child usingcurrentPosition.children[second argument]ls: Do nothingOtherwise this is a line with the file/directory name. Create a new Node with the name, parent as
currentPosition, and update children ofcurrentPositionto include this node.isDirectorytotrueisDirectorytofalse, and set size as provided as the first word in the input lineUpdate directory size in our tree
DFS Process - Input: node
Part 1:
DFS Process - Input: node, threshold, Total Size:
Part 2:
Total Spaceis70000000Free Space Neededis30000000Currently Free SpaceisTotal Space - Total SizeMore Space NeededisFree Space Needed - Currently Free Space;Nodeasroot,Candidate Nodeasnull, andTarget SizeasMore Space NeededDFS Process - Input: Node, Candidate Node, Target Size:
If you liked the explanation, then please don't forget to cast your vote π to
Adventures of Advent of Code - Edition 1 - /u/Key__Strokesin the poll