Advent of Code 2022 - 2nd attempt in c++
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.0 KiB

2 years ago
#include <iostream>
#include <vector>
#include <map>
2 years ago
//#define BIGBOY
#if defined(BIGBOY)
#define TOTAL_SPACE 3000000000
#define NEEDED_SPACE 700000000
#else
#define TOTAL_SPACE 70000000
#define NEEDED_SPACE 30000000
#endif
2 years ago
using namespace std;
enum FsNodeType {
NODE_DIR,
NODE_FILE
};
struct FsNode {
FsNodeType type;
string name;
2 years ago
size_t size;
2 years ago
};
struct FsFile : FsNode {
};
struct FsDir : FsNode {
2 years ago
~FsDir() {
for (auto e : this->entries) {
FsNode *node = e.second;
if (node->type == NODE_DIR) {
FsDir *dir = (FsDir *)node;
delete dir;
} else if (node->type == NODE_FILE) {
FsFile *file = (FsFile *)node;
delete file;
}
}
this->entries.clear();
}
2 years ago
FsDir *parent;
map<string, FsNode *> entries;
};
void print_node(FsNode *node, unsigned depth = 0)
{
for (unsigned i = 0; i < depth; i++) clog << " ";
clog << "- " << node->name << " (";
switch (node->type) {
case NODE_DIR: clog << "dir"; break;
case NODE_FILE: clog << "file"; break;
}
clog << ", ";
clog << "size=" << node->size;
clog << ")" << endl;
if (node->type == NODE_DIR) {
FsDir *dir = (FsDir *)node;
for (auto e : dir->entries) print_node(e.second, depth + 1);
}
}
2 years ago
size_t calc_size(FsDir &dir)
2 years ago
{
2 years ago
size_t size = 0;
2 years ago
for (auto e : dir.entries) {
FsNode *node = e.second;
if (!node->size && node->type == NODE_DIR) calc_size(*(FsDir *)node);
size += node->size;
}
dir.size = size;
return size;
}
FsDir parse()
{
FsDir root;
root.type = NODE_DIR;
root.name = "/";
root.parent = NULL;
FsDir *dir = &root;
for (string line; getline(cin, line);) {
if (line.at(0) != '$' || line.at(1) != ' ') {
unsigned split = line.find(' ');
string first = line.substr(0, split);
string name = line.substr(split + 1);
if (first == "dir") {
FsDir *newdir = new FsDir;
newdir->type = NODE_DIR;
newdir->name = name;
newdir->size = 0;
newdir->parent = dir;
dir->entries[name] = newdir;
} else {
2 years ago
size_t size = stoll(first);
2 years ago
FsFile *newfile = new FsFile;
newfile->type = NODE_FILE;
newfile->name = name;
newfile->size = size;
dir->entries[name] = newfile;
}
}
if (line.substr(2, 3) == "ls") {
// derp?
} else if (line.substr(2, 3) == "cd ") {
string name = line.substr(5);
if (name == "/") {
dir = &root;
} else if (name == "..") {
if (dir->parent) dir = dir->parent;
} else {
auto it = dir->entries.find(name);
if (it != dir->entries.end()) {
FsNode *node = it->second;
if (node->type == NODE_DIR) {
dir = (FsDir *)node;
}
}
}
}
}
calc_size(root);
//print_node(&root);
return root;
}
2 years ago
size_t p1(const FsDir &dir)
2 years ago
{
2 years ago
size_t count = 0;
2 years ago
if (dir.size <= 100000) count += dir.size;
for (auto e : dir.entries) {
FsNode *node = e.second;
if (node->type == NODE_DIR) count += p1(*(FsDir *)node);
}
return count;
}
2 years ago
size_t p2(const FsDir &dir, size_t target = 0)
2 years ago
{
2 years ago
if (!target) target = dir.size - (TOTAL_SPACE - NEEDED_SPACE);
2 years ago
2 years ago
size_t smallest = NEEDED_SPACE;
2 years ago
if (dir.size >= target && dir.size < smallest) smallest = dir.size;
for (auto e : dir.entries) {
FsNode *node = e.second;
if (node->type == NODE_DIR) {
2 years ago
size_t size = p2(*(FsDir *)node, target);
2 years ago
if (size >= target && size < smallest) smallest = size;
}
}
return smallest;
}
int main()
{
auto input = parse();
cout << p1(input) << endl;
cout << p2(input) << endl;
}