#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; }