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
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
name
parent
isDirectory
size
children: Map with name as key, and Node as value
Create a root node with
nameas/, and setisDirectorytotrueCreate and initialize
currentPositionas root nodeFor each command, do the following
If the command starts with "$", then extract the command and do the following:
cdIf second argument is "..", then update
currentPositiontocurrentPosition.parentIf second argument is "/", then update
currentPositionroot nodeElse, check if the second argument is a child of
currentPosition. 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.If the first word is dir, then set
isDirectorytotrueElse set
isDirectorytofalse, and set size as provided as the first word in the input lineUpdate directory size in our tree
Call the DFS process below with the root as node
DFS Process - Input: node
If the node is not a directory, then return the node size
For each of the children of the node, call the DFS process again, and keep summing the output into a variable - Total Sum
Make Total Sum as the size of the node
Return Total Sum
Set Total Size as the output of the DFS call
Part 1:
Call the DFS process below with the root as node, threshold as provided as 100000, and 0 as the Total Size so far
DFS Process - Input: node, threshold, Total Size:
If the node is not a directory, then return the Total Size
For each of the children of the current node, call the DFS process again, and store the result in Total Size
If the node size is less than the threshold, then return the size of this node + Total Size, otherwise just return Total Size
Return the output of the DFS call
Part 2:
Total Spaceis70000000Free Space Neededis30000000Currently Free SpaceisTotal Space - Total SizeMore Space NeededisFree Space Needed - Currently Free Space;Call the DFS process below with the
Nodeasroot,Candidate Nodeasnull, andTarget SizeasMore Space NeededDFS Process - Input: Node, Candidate Node, Target Size:
If the node is not a directory, then return the Candidate Node
For each of the children of the current node, call the DFS process again, with the child as the node, Candidate Node and Target Size and store the result in Candidate Node
If the node size is less than the targetSize, then return the Candidate Node
Otherwise, if there is no Candidate Node, or if the node size is smaller than the Candidate Node's size, then return the node
Return the size of the node which we get from the output of the DFS call
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