214 lines
5.4 KiB
C++
214 lines
5.4 KiB
C++
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
class Directory;
|
|
|
|
struct File{
|
|
string name;
|
|
long size;
|
|
};
|
|
|
|
class Directory{
|
|
public:
|
|
Directory* parent;
|
|
string name;
|
|
vector<Directory*> subdirectories;
|
|
vector<File*> files;
|
|
long container_size;
|
|
|
|
~Directory(){
|
|
for(auto d : subdirectories){
|
|
delete d;
|
|
}
|
|
subdirectories.clear();
|
|
for(auto f : files){
|
|
delete f;
|
|
}
|
|
files.clear();
|
|
}
|
|
|
|
Directory (Directory* parent, const string& name) : parent(parent), name(name){
|
|
subdirectories.clear();
|
|
files.clear();
|
|
container_size = 0;
|
|
}
|
|
|
|
Directory* getDirectoryPointerFromSubdirectories(const string& s) const{
|
|
for(auto d : subdirectories){
|
|
if(d->name == s){
|
|
return d;
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
long getContainerSize(){
|
|
if(container_size > 0){
|
|
// Risky assumption, but then again this is just playground code right
|
|
return container_size;
|
|
}
|
|
|
|
long result = 0;
|
|
for(auto f : files){
|
|
result += f->size;
|
|
}
|
|
for(auto s : subdirectories){
|
|
result += s->getContainerSize();
|
|
}
|
|
|
|
container_size = result;
|
|
return result;
|
|
}
|
|
|
|
long getAnswer(long max_size){
|
|
long result = 0;
|
|
long c = getContainerSize();
|
|
|
|
if(c <= max_size){
|
|
result += c;
|
|
}
|
|
for(auto d : subdirectories){
|
|
result += d->getAnswer(max_size);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
long getAnswerPartTwo(long min_size_needed){
|
|
long result = getContainerSize();
|
|
if(result >= min_size_needed){
|
|
for(auto sub : subdirectories){
|
|
long sub_result = sub->getAnswerPartTwo(min_size_needed);
|
|
|
|
if(sub_result < result){
|
|
result = sub_result;
|
|
}
|
|
}
|
|
return result;
|
|
} else{
|
|
return 700000000;
|
|
}
|
|
}
|
|
};
|
|
|
|
using ChangeDirectory = string;
|
|
using List = vector<string>;
|
|
using Command = variant<ChangeDirectory, List>;
|
|
|
|
int main(){
|
|
ifstream input_file("input.txt");
|
|
string line;
|
|
|
|
vector<Command> commands;
|
|
List current_list;
|
|
bool reading_list = false;
|
|
|
|
// Parsing the input so it is nicer to handle later on
|
|
while(getline(input_file, line)){
|
|
if(line[0] == '$'){
|
|
if(reading_list){
|
|
commands.push_back(current_list);
|
|
current_list.clear();
|
|
reading_list = false;
|
|
}
|
|
|
|
stringstream ss(line);
|
|
string _, comm;
|
|
ss >> _ >> comm;
|
|
|
|
if(comm == "cd"){
|
|
ChangeDirectory dir;
|
|
ss >> dir;
|
|
commands.push_back(dir);
|
|
} else{
|
|
reading_list = true;
|
|
}
|
|
} else{
|
|
current_list.push_back(line);
|
|
}
|
|
}
|
|
|
|
if(reading_list){
|
|
commands.push_back(current_list);
|
|
reading_list = false;
|
|
}
|
|
|
|
// Setting up initial, empty directory
|
|
Directory main_directory(nullptr, "root");
|
|
Directory* cur_dir = &main_directory;
|
|
|
|
// Mapping out the file directory from all the commands
|
|
for(auto c : commands){
|
|
if(c.index() == 0){ // So it is cd command
|
|
ChangeDirectory dir = get<0>(c);
|
|
if(dir == "/"){
|
|
continue;
|
|
} else if(dir == ".."){
|
|
if(cur_dir->name != "root"){
|
|
cur_dir = cur_dir->parent;
|
|
}
|
|
} else{
|
|
auto p = cur_dir->getDirectoryPointerFromSubdirectories(dir);
|
|
if(p){
|
|
cur_dir = p;
|
|
}
|
|
}
|
|
|
|
} else{ // So it is a ls command
|
|
List& listing = get<1>(c);
|
|
for(auto s : listing){
|
|
stringstream ss(s);
|
|
string first, second;
|
|
ss >> first >> second;
|
|
|
|
if(first == "dir"){
|
|
Directory* child = new Directory(cur_dir, second);
|
|
cur_dir->subdirectories.push_back(child);
|
|
} else{
|
|
File* f = new File{second, stol(first)};
|
|
cur_dir->files.push_back(f);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// long result = main_directory.getContainerSize();
|
|
|
|
long result = main_directory.getAnswer(100000);
|
|
|
|
cout << "Sum of directories smaller than 100000: " << result << endl;
|
|
|
|
long unused_space = 70000000 - main_directory.getContainerSize();
|
|
long min_space_needed = 30000000 - unused_space;
|
|
result = main_directory.getAnswerPartTwo(min_space_needed);
|
|
|
|
cout << "Smallest directory larger than " << min_space_needed << ": " << result << endl;
|
|
|
|
// Printing parsed commands
|
|
|
|
// for(auto c : commands){
|
|
// if(c.index() == 0){
|
|
// cout << "$ cd " << get<0>(c) << '\n';
|
|
// } else{
|
|
// cout << "$ ls\n";
|
|
// for(auto s : get<1>(c)){
|
|
// cout << s << '\n';
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// Printing generated tree
|
|
|
|
// cout << "- / (dir)" << endl;
|
|
// for(auto d : main_directory.subdirectories){
|
|
// cout << "\t- " << d->name << "(dir)\n";
|
|
// }
|
|
// for(auto f : main_directory.files){
|
|
// cout << "\t- " << f->name << " (file, size=" << f->size << ")\n";
|
|
// }
|
|
|
|
// delete cur_dir;
|
|
return 0;
|
|
} |