#7 part 1 works on the first try.

This commit is contained in:
David Vereb 2022-12-08 22:52:31 -05:00
parent fff4541aca
commit ce7229c13c

View File

@ -107,6 +107,49 @@ private:
std::vector<File*> files;
};
void DebugOutput(File *file)
{
static int indent = -1;
++indent;
for(auto i = 0; i < indent; ++i)
std::cout << " ";
Folder *folder = dynamic_cast<Folder*>(file);
if(folder)
{
std::cout << "Contents of folder: " << folder->Filename()
<< ", size: " << folder->Size()
<< std::endl;
for(File *file : folder->Files())
DebugOutput(file);
}
else
std::cout << "- " << file->Filename()
<< ", size: " << file->Size()
<< std::endl;
--indent;
}
unsigned long PartOne(File *file, unsigned long &grand_total)
{
unsigned long rtn = 0;
Folder *folder = dynamic_cast<Folder*>(file);
if(folder)
{
for(File *file : folder->Files())
rtn += PartOne(file, grand_total);
if(rtn <= 100000)
{
grand_total += rtn;
std::cout << "Folder \"" << folder->Filename() << "\" has a size of " << rtn
<< std::endl;
}
}
else
rtn += file->Size();
return rtn;
}
int main()
{
std::ifstream ifs("data.txt");
@ -120,6 +163,7 @@ int main()
std::stack<Folder*> dir_stack;
dir_stack.push(&root);
// Step 1: Parse:
for(std::string line; std::getline(ifs, line); )
{
if(line == "")
@ -176,5 +220,13 @@ int main()
}
}
// Step 1.5: Debug output:
DebugOutput(&root);
unsigned long grand_total = 0;
PartOne(&root, grand_total);
std::cout << "Grand Total: " << grand_total << std::endl;
return 0;
}