Browse Source

cleanup

master
mid-kid 2 years ago
parent
commit
9591ff7f02
  1. 58
      d07.cc
  2. BIN
      d07_bigboy.txt.xz

58
d07.cc

@ -2,6 +2,15 @@
#include <vector>
#include <map>
//#define BIGBOY
#if defined(BIGBOY)
#define TOTAL_SPACE 3000000000
#define NEEDED_SPACE 700000000
#else
#define TOTAL_SPACE 70000000
#define NEEDED_SPACE 30000000
#endif
using namespace std;
enum FsNodeType {
@ -12,13 +21,26 @@ enum FsNodeType {
struct FsNode {
FsNodeType type;
string name;
unsigned size;
size_t size;
};
struct FsFile : FsNode {
};
struct FsDir : FsNode {
~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();
}
FsDir *parent;
map<string, FsNode *> entries;
};
@ -41,9 +63,9 @@ void print_node(FsNode *node, unsigned depth = 0)
}
}
unsigned calc_size(FsDir &dir)
size_t calc_size(FsDir &dir)
{
unsigned size = 0;
size_t size = 0;
for (auto e : dir.entries) {
FsNode *node = e.second;
@ -76,7 +98,7 @@ FsDir parse()
newdir->parent = dir;
dir->entries[name] = newdir;
} else {
unsigned size = stoi(first);
size_t size = stoll(first);
FsFile *newfile = new FsFile;
newfile->type = NODE_FILE;
newfile->name = name;
@ -110,9 +132,9 @@ FsDir parse()
return root;
}
unsigned p1(const FsDir &dir)
size_t p1(const FsDir &dir)
{
unsigned count = 0;
size_t count = 0;
if (dir.size <= 100000) count += dir.size;
for (auto e : dir.entries) {
FsNode *node = e.second;
@ -121,42 +143,26 @@ unsigned p1(const FsDir &dir)
return count;
}
unsigned p2(const FsDir &dir, unsigned target = 0)
size_t p2(const FsDir &dir, size_t target = 0)
{
if (!target) target = dir.size - 40000000;
if (!target) target = dir.size - (TOTAL_SPACE - NEEDED_SPACE);
unsigned smallest = 30000000;
size_t smallest = NEEDED_SPACE;
if (dir.size >= target && dir.size < smallest) smallest = dir.size;
for (auto e : dir.entries) {
FsNode *node = e.second;
if (node->type == NODE_DIR) {
unsigned size = p2(*(FsDir *)node, target);
size_t size = p2(*(FsDir *)node, target);
if (size >= target && size < smallest) smallest = size;
}
}
return smallest;
}
void free_input(FsDir &dir)
{
for (auto e : dir.entries) {
FsNode *node = e.second;
if (node->type == NODE_DIR) {
FsDir *dir = (FsDir *)node;
free_input(*dir);
delete dir;
} else if (node->type == NODE_FILE) {
FsFile *file = (FsFile *)node;
delete file;
}
}
}
int main()
{
auto input = parse();
cout << p1(input) << endl;
cout << p2(input) << endl;
free_input(input);
}

BIN
d07_bigboy.txt.xz

Binary file not shown.
Loading…
Cancel
Save