1
0
Fork 0
This commit is contained in:
Marcus Penate 2022-12-07 01:38:31 -05:00
parent 666fdea266
commit e8b377e008
2 changed files with 231 additions and 0 deletions

119
Day7/part1.cpp Normal file
View File

@ -0,0 +1,119 @@
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
struct Dir
{
Dir(string name) : name{name},subdir{vector<Dir>()},files{unordered_map<string,int>()},parent{nullptr} {}
string name;
vector<Dir> subdir;
unordered_map<string, int> 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<Dir>::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;
}

112
Day7/part2.cpp Normal file
View File

@ -0,0 +1,112 @@
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
struct Dir
{
Dir(string name) : name{name},subdir{vector<Dir>()},files{unordered_map<string,int>()},parent{nullptr} {}
string name;
vector<Dir> subdir;
unordered_map<string, int> files;
Dir* parent;
};
vector<int> 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<Dir>::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;
}