From e8b377e008a3423bd1768c598e1d45394795f2b5 Mon Sep 17 00:00:00 2001 From: Marcus Penate Date: Wed, 7 Dec 2022 01:38:31 -0500 Subject: [PATCH] Day 7 --- Day7/part1.cpp | 119 +++++++++++++++++++++++++++++++++++++++++++++++++ Day7/part2.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 231 insertions(+) create mode 100644 Day7/part1.cpp create mode 100644 Day7/part2.cpp diff --git a/Day7/part1.cpp b/Day7/part1.cpp new file mode 100644 index 0000000..cc87d12 --- /dev/null +++ b/Day7/part1.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct Dir +{ + Dir(string name) : name{name},subdir{vector()},files{unordered_map()},parent{nullptr} {} + + string name; + vector subdir; + unordered_map files; + Dir* parent; +}; + +int get_sum_small_dirs(Dir dir); + +int get_dir_size(Dir dir); + +int main(int argc, char* argv[]) +{ + Dir root_dir("/"); + + Dir* current_dir = &root_dir; + + string line; + getline(cin, line); + getline(cin, line); + while (line.size() != 0) + { + if (line.substr(2,2).compare("cd") == 0) + { + if (line.substr(5).compare("..") == 0) + { + current_dir = current_dir->parent; + } + else + { + for (vector::iterator it = current_dir->subdir.begin(); it != current_dir->subdir.end(); ++it) + { + if (it->name.compare(line.substr(5)) == 0) + { + current_dir = &(*it); + break; + } + } + } + + getline(cin, line); + continue; + } + else if (line.substr(2,2).compare("ls") == 0) + { + while(true) + { + getline(cin, line); + + if (line.size() == 0 || line[0] == '$') + { + break; + } + + if (line.substr(0,3).compare("dir") == 0) + { + Dir subdir(line.substr(4)); + subdir.parent = current_dir; + current_dir->subdir.push_back(subdir); + } + else + { + string::size_type n = line.find(' '); + int file_size = stoi(line.substr(0,n)); + string file_name = line.substr(n+1); + current_dir->files[file_name] = file_size; + } + } + } + } + + cout << get_sum_small_dirs(root_dir) << endl; +} + +int get_sum_small_dirs(Dir dir) +{ + int total_size = 0; + + int dir_size = get_dir_size(dir); + + if (dir_size < 100000) + { + total_size += dir_size; + } + + for(unsigned int i = 0; i < dir.subdir.size(); i++) + { + total_size += get_sum_small_dirs(dir.subdir[i]); + } + + return total_size; +} + +int get_dir_size(Dir dir) +{ + int size = 0; + for(auto &file_it : dir.files) + { + size += file_it.second; + } + + for(unsigned int i = 0; i < dir.subdir.size(); i++) + { + size += get_dir_size(dir.subdir[i]); + } + + return size; +} \ No newline at end of file diff --git a/Day7/part2.cpp b/Day7/part2.cpp new file mode 100644 index 0000000..3583888 --- /dev/null +++ b/Day7/part2.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct Dir +{ + Dir(string name) : name{name},subdir{vector()},files{unordered_map()},parent{nullptr} {} + + string name; + vector subdir; + unordered_map files; + Dir* parent; +}; + +vector dir_sizes; + +int get_dir_size(Dir dir); + +int main(int argc, char* argv[]) +{ + Dir root_dir("/"); + + Dir* current_dir = &root_dir; + + string line; + getline(cin, line); + getline(cin, line); + while (line.size() != 0) + { + if (line.substr(2,2).compare("cd") == 0) + { + if (line.substr(5).compare("..") == 0) + { + current_dir = current_dir->parent; + } + else + { + for (vector::iterator it = current_dir->subdir.begin(); it != current_dir->subdir.end(); ++it) + { + if (it->name.compare(line.substr(5)) == 0) + { + current_dir = &(*it); + break; + } + } + } + + getline(cin, line); + continue; + } + else if (line.substr(2,2).compare("ls") == 0) + { + while(true) + { + getline(cin, line); + + if (line.size() == 0 || line[0] == '$') + { + break; + } + + if (line.substr(0,3).compare("dir") == 0) + { + Dir subdir(line.substr(4)); + subdir.parent = current_dir; + current_dir->subdir.push_back(subdir); + } + else + { + string::size_type n = line.find(' '); + int file_size = stoi(line.substr(0,n)); + string file_name = line.substr(n+1); + current_dir->files[file_name] = file_size; + } + } + } + } + + int space_needed = 30000000 - 70000000 + get_dir_size(root_dir); + + sort(dir_sizes.begin(), dir_sizes.end()); + + for(unsigned int i = 0; i < dir_sizes.size(); i++) + { + if (dir_sizes[i] >= space_needed) + { + cout << dir_sizes[i] << endl; + break; + } + } +} + +int get_dir_size(Dir dir) +{ + int size = 0; + for(auto &file_it : dir.files) + { + size += file_it.second; + } + + for(unsigned int i = 0; i < dir.subdir.size(); i++) + { + size += get_dir_size(dir.subdir[i]); + } + + dir_sizes.push_back(size); + return size; +} \ No newline at end of file