From 2139a012520c51139a5dc68c772858d0bd6406eb Mon Sep 17 00:00:00 2001 From: David Vereb Date: Thu, 8 Dec 2022 23:17:56 -0500 Subject: [PATCH] Refactored #7 out into class files like I should have to begin with. --- 2022/7/File.cpp | 21 +++++++++ 2022/7/File.h | 19 ++++++++ 2022/7/Folder.cpp | 58 +++++++++++++++++++++++ 2022/7/Folder.h | 22 +++++++++ 2022/7/Makefile | 3 +- 2022/7/SplitStr.h | 22 +++++++++ 2022/7/main.cpp | 116 +++++----------------------------------------- 7 files changed, 155 insertions(+), 106 deletions(-) create mode 100644 2022/7/File.cpp create mode 100644 2022/7/File.h create mode 100644 2022/7/Folder.cpp create mode 100644 2022/7/Folder.h mode change 120000 => 100644 2022/7/Makefile create mode 100644 2022/7/SplitStr.h diff --git a/2022/7/File.cpp b/2022/7/File.cpp new file mode 100644 index 0000000..acea9c6 --- /dev/null +++ b/2022/7/File.cpp @@ -0,0 +1,21 @@ +#include "File.h" + +File::File(const std::string &set_filename, unsigned long set_size) +{ + this->size = set_size; + this->filename = set_filename; +} + +File::~File() +{ +} + +const std::string& File::Filename() const +{ + return filename; +} + +unsigned long File::Size() const +{ + return size; +} diff --git a/2022/7/File.h b/2022/7/File.h new file mode 100644 index 0000000..2df2462 --- /dev/null +++ b/2022/7/File.h @@ -0,0 +1,19 @@ +#ifndef ADVENT_DVEREB_FILE_H +#define ADVENT_DVEREB_FILE_H + +#include + +class File { +public: + File(const std::string &set_filename, unsigned long set_size); + virtual ~File(); + + const std::string& Filename() const; + virtual unsigned long Size() const; + +protected: + unsigned long size; + std::string filename; +}; + +#endif diff --git a/2022/7/Folder.cpp b/2022/7/Folder.cpp new file mode 100644 index 0000000..130e874 --- /dev/null +++ b/2022/7/Folder.cpp @@ -0,0 +1,58 @@ +#include "Folder.h" + +#include +#include + +Folder::Folder(const std::string &filename) + : File(filename, 0) +{ +} + +unsigned long Folder::Size() const +{ + unsigned long size = 0; + for(const auto *file : files) + size += file->Size(); + return size; +} + +void Folder::AddFile(const std::string &filename, + unsigned long size) +{ + // make sure the file doesn't already exist: + if(std::find_if(files.begin(), files.end(), + [&](const File *file) { + return file->Filename() == filename; + }) != files.end()) + { + std::cerr << "Folder \"" << this->Filename() << "\" already contains " + << "the file \"" << filename << "\"." << std::endl; + exit(-1); + } + + // increment this folder's size total: + this->size += size; + + // add the file: + files.push_back(new File(filename, size)); +} + +void Folder::AddFolder(const std::string &name) +{ + if(std::find_if(files.begin(), files.end(), + [&](const File *file) { + return file->Filename() == name; + }) != files.end()) + { + std::cout << "NOTE: Folder \"" << this->Filename() << "\" already contains " + << "the subfolder \"" << name << "\"." << std::endl; + // exit(-1); + } + + files.push_back(new Folder(name)); +} + +const std::vector& Folder::Files() const +{ + return files; +} diff --git a/2022/7/Folder.h b/2022/7/Folder.h new file mode 100644 index 0000000..dcc87e6 --- /dev/null +++ b/2022/7/Folder.h @@ -0,0 +1,22 @@ +#ifndef ADVENT_DVEREB_FOLDER_H +#define ADVENT_DVEREB_FOLDER_H + +#include "File.h" + +#include +#include + +class Folder : public File { +public: + Folder(const std::string &filename); + + unsigned long Size() const override; + void AddFile(const std::string &filename, unsigned long size); + void AddFolder(const std::string &name); + const std::vector& Files() const; + +private: + std::vector files; +}; + +#endif diff --git a/2022/7/Makefile b/2022/7/Makefile deleted file mode 120000 index 02e7391..0000000 --- a/2022/7/Makefile +++ /dev/null @@ -1 +0,0 @@ -../starter/Makefile \ No newline at end of file diff --git a/2022/7/Makefile b/2022/7/Makefile new file mode 100644 index 0000000..53e8e43 --- /dev/null +++ b/2022/7/Makefile @@ -0,0 +1,2 @@ +a.out: main.cpp File.h File.cpp Folder.h Folder.cpp SplitStr.h + clang++ -std=c++2b -g -O0 main.cpp File.cpp Folder.cpp diff --git a/2022/7/SplitStr.h b/2022/7/SplitStr.h new file mode 100644 index 0000000..0aa6023 --- /dev/null +++ b/2022/7/SplitStr.h @@ -0,0 +1,22 @@ +#ifndef ADVENT_DVEREB_SPLITSTR_H +#define ADVENT_DVEREB_SPLITSTR_H + +#include +#include + +std::vector SplitStr(std::string str, const std::string &delim) +{ + std::vector rtn; + + for(auto split = str.find(delim); split != std::string::npos; split = str.find(delim)) + { + rtn.push_back(str.substr(0, split)); + str = str.substr(split + delim.size()); // chop off beginning + } + if(str.size()) + rtn.push_back(str); + + return rtn; +} + +#endif diff --git a/2022/7/main.cpp b/2022/7/main.cpp index c24a047..ba35b76 100644 --- a/2022/7/main.cpp +++ b/2022/7/main.cpp @@ -1,112 +1,13 @@ +#include "Folder.h" +#include "SplitStr.h" + #include #include #include -#include #include #include #include -std::vector SplitStr(std::string str, const std::string &delim) -{ - std::vector rtn; - - for(auto split = str.find(delim); split != std::string::npos; split = str.find(delim)) - { - rtn.push_back(str.substr(0, split)); - str = str.substr(split + delim.size()); // chop off beginning - } - if(str.size()) - rtn.push_back(str); - - return rtn; -} - -class File { -public: - File(const std::string &set_filename, unsigned long set_size) - { - this->size = set_size; - this->filename = set_filename; - } - - virtual ~File() - { - } - - const std::string& Filename() const - { - return filename; - } - virtual unsigned long Size() const - { - return size; - } - -protected: - unsigned long size; - std::string filename; -}; - -class Folder : public File { -public: - Folder(const std::string &filename) - : File(filename, 0) - { - } - - unsigned long Size() const override - { - unsigned long size = 0; - for(const auto *file : files) - size += file->Size(); - return size; - } - - void AddFile(const std::string &filename, - unsigned long size) - { - // make sure the file doesn't already exist: - if(std::find_if(files.begin(), files.end(), - [&](const File *file) { - return file->Filename() == filename; - }) != files.end()) - { - std::cerr << "Folder \"" << this->Filename() << "\" already contains " - << "the file \"" << filename << "\"." << std::endl; - exit(-1); - } - - // increment this folder's size total: - this->size += size; - - // add the file: - files.push_back(new File(filename, size)); - } - - void AddFolder(const std::string &name) - { - if(std::find_if(files.begin(), files.end(), - [&](const File *file) { - return file->Filename() == name; - }) != files.end()) - { - std::cout << "NOTE: Folder \"" << this->Filename() << "\" already contains " - << "the subfolder \"" << name << "\"." << std::endl; - // exit(-1); - } - - files.push_back(new Folder(name)); - } - - const std::vector& Files() const - { - return files; - } - -private: - std::vector files; -}; - void DebugOutput(File *file) { static int indent = -1; @@ -169,16 +70,15 @@ void PartTwo(File *file, unsigned long at_least, File *&smallest) return; } -int main() +void Parse(Folder &root) { std::ifstream ifs("data.txt"); if(!ifs.is_open()) { std::cerr << "Missing data.txt." << std::endl; - return -1; + exit(-1); } - Folder root("/"); std::stack dir_stack; dir_stack.push(&root); @@ -238,6 +138,12 @@ int main() dir_stack.top()->AddFile(values[1], std::atol(values[0].c_str())); } } +} + +int main() +{ + Folder root("/"); + Parse(root); // Step 1.5: Debug output: DebugOutput(&root);