From ce7229c13c1fadf0bad4e4dc9a15e9bbfa4cb87d Mon Sep 17 00:00:00 2001 From: David Vereb Date: Thu, 8 Dec 2022 22:52:31 -0500 Subject: [PATCH] #7 part 1 works on the first try. --- 2022/7/main.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/2022/7/main.cpp b/2022/7/main.cpp index a23b551..1b5327c 100644 --- a/2022/7/main.cpp +++ b/2022/7/main.cpp @@ -107,6 +107,49 @@ private: std::vector files; }; +void DebugOutput(File *file) +{ + static int indent = -1; + ++indent; + for(auto i = 0; i < indent; ++i) + std::cout << " "; + + Folder *folder = dynamic_cast(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(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 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; }