Finished day 7 aoc, part 1
This commit is contained in:
@@ -2,7 +2,12 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct File;
|
class Directory;
|
||||||
|
|
||||||
|
struct File{
|
||||||
|
string name;
|
||||||
|
long size;
|
||||||
|
};
|
||||||
|
|
||||||
class Directory{
|
class Directory{
|
||||||
public:
|
public:
|
||||||
@@ -12,17 +17,68 @@ public:
|
|||||||
vector<File*> files;
|
vector<File*> files;
|
||||||
long container_size;
|
long container_size;
|
||||||
|
|
||||||
Directory (Directory* parent, string name) : parent(parent), name(name){
|
~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();
|
subdirectories.clear();
|
||||||
files.clear();
|
files.clear();
|
||||||
container_size = 0;
|
container_size = 0;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
struct File{
|
Directory* getDirectoryPointerFromSubdirectories(const string& s) const{
|
||||||
string name;
|
for(auto d : subdirectories){
|
||||||
long size;
|
if(d->name == s){
|
||||||
Directory* parent;
|
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 max_size){
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
using ChangeDirectory = string;
|
using ChangeDirectory = string;
|
||||||
@@ -30,7 +86,7 @@ using List = vector<string>;
|
|||||||
using Command = variant<ChangeDirectory, List>;
|
using Command = variant<ChangeDirectory, List>;
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
ifstream input_file("test.txt");
|
ifstream input_file("input.txt");
|
||||||
string line;
|
string line;
|
||||||
|
|
||||||
vector<Command> commands;
|
vector<Command> commands;
|
||||||
@@ -68,20 +124,50 @@ int main(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Setting up initial, empty directory
|
// Setting up initial, empty directory
|
||||||
Directory
|
Directory main_directory(nullptr, "root");
|
||||||
|
Directory* cur_dir = &main_directory;
|
||||||
|
|
||||||
// Mapping out the file directory from all the commands
|
// Mapping out the file directory from all the commands
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for(auto c : commands){
|
for(auto c : commands){
|
||||||
if(c.index() == 0){
|
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{
|
} 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 << result << endl;
|
||||||
|
|
||||||
// Printing parsed commands
|
// Printing parsed commands
|
||||||
|
|
||||||
// for(auto c : commands){
|
// for(auto c : commands){
|
||||||
@@ -95,5 +181,16 @@ int main(){
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// 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;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user