|
|
@ -4,7 +4,7 @@ Result Day07::Task1() { |
|
|
|
Dir* tree = parseTree(); |
|
|
|
Dir* tree = parseTree(); |
|
|
|
|
|
|
|
|
|
|
|
set<Dir*> dirsBelow = tree->getAllDirsBelowSize(100000); |
|
|
|
set<Dir*> dirsBelow = tree->getAllDirsBelowSize(100000); |
|
|
|
uint64_t sum = accumulate(dirsBelow.begin(), dirsBelow.end(), 0, [](uint64_t s, Dir* dir){ |
|
|
|
uint64 sum = accumulate(dirsBelow.begin(), dirsBelow.end(), 0, [](uint64 s, Dir* dir){ |
|
|
|
return s + dir->getSize(); |
|
|
|
return s + dir->getSize(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -16,14 +16,14 @@ Result Day07::Task1() { |
|
|
|
Result Day07::Task2() { |
|
|
|
Result Day07::Task2() { |
|
|
|
Dir* tree = parseTree(); |
|
|
|
Dir* tree = parseTree(); |
|
|
|
|
|
|
|
|
|
|
|
uint64_t toBeFreed = 30000000 - (70000000 - tree->getSize()); |
|
|
|
uint64 toBeFreed = 30000000 - (70000000 - tree->getSize()); |
|
|
|
|
|
|
|
|
|
|
|
set<Dir*> dirsAbove = tree->getAllDirsAboveSize(toBeFreed); |
|
|
|
set<Dir*> dirsAbove = tree->getAllDirsAboveSize(toBeFreed); |
|
|
|
set<uint64_t> sizes; |
|
|
|
set<uint64> sizes; |
|
|
|
std::transform(dirsAbove.begin(), dirsAbove.end(), inserter(sizes, sizes.begin()), [](Dir* dir){ |
|
|
|
std::transform(dirsAbove.begin(), dirsAbove.end(), inserter(sizes, sizes.begin()), [](Dir* dir){ |
|
|
|
return dir->getSize(); |
|
|
|
return dir->getSize(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
uint64_t result = *std::min_element(sizes.begin(), sizes.end()); |
|
|
|
uint64 result = *std::min_element(sizes.begin(), sizes.end()); |
|
|
|
|
|
|
|
|
|
|
|
delete tree; |
|
|
|
delete tree; |
|
|
|
|
|
|
|
|
|
|
@ -45,7 +45,7 @@ Dir* Day07::parseTree() { |
|
|
|
cwd->items.insert(new Dir(name, cwd)); |
|
|
|
cwd->items.insert(new Dir(name, cwd)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
size_t ws = pos->find(' '); |
|
|
|
size_t ws = pos->find(' '); |
|
|
|
uint64_t size = stoi(pos->substr(0, ws)); |
|
|
|
uint64 size = stoi(pos->substr(0, ws)); |
|
|
|
string name = pos->substr(ws + 1); |
|
|
|
string name = pos->substr(ws + 1); |
|
|
|
cwd->items.insert(new File(name, cwd, size)); |
|
|
|
cwd->items.insert(new File(name, cwd, size)); |
|
|
|
} |
|
|
|
} |
|
|
@ -83,8 +83,8 @@ Dir *Dir::findRoot() { |
|
|
|
return parent->findRoot(); |
|
|
|
return parent->findRoot(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint64_t Dir::getSize() { |
|
|
|
uint64 Dir::getSize() { |
|
|
|
return accumulate(items.begin(), items.end(), 0, [](uint64_t s, Item* item){ |
|
|
|
return accumulate(items.begin(), items.end(), 0, [](uint64 s, Item* item){ |
|
|
|
return s + item->getSize(); |
|
|
|
return s + item->getSize(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
@ -98,7 +98,7 @@ set<Dir*> Dir::getDirs() { |
|
|
|
return dirs; |
|
|
|
return dirs; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
set<Dir*> Dir::getAllDirsBelowSize(uint64_t maxSize) { |
|
|
|
set<Dir*> Dir::getAllDirsBelowSize(uint64 maxSize) { |
|
|
|
set<Dir*> result; |
|
|
|
set<Dir*> result; |
|
|
|
|
|
|
|
|
|
|
|
for (Dir* dir : getDirs()){ |
|
|
|
for (Dir* dir : getDirs()){ |
|
|
@ -111,7 +111,7 @@ set<Dir*> Dir::getAllDirsBelowSize(uint64_t maxSize) { |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
set<Dir *> Dir::getAllDirsAboveSize(uint64_t minSize) { |
|
|
|
set<Dir *> Dir::getAllDirsAboveSize(uint64 minSize) { |
|
|
|
set<Dir*> result; |
|
|
|
set<Dir*> result; |
|
|
|
|
|
|
|
|
|
|
|
for (Dir* dir : getDirs()){ |
|
|
|
for (Dir* dir : getDirs()){ |
|
|
@ -124,11 +124,11 @@ set<Dir *> Dir::getAllDirsAboveSize(uint64_t minSize) { |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint64_t File::getSize() { |
|
|
|
uint64 File::getSize() { |
|
|
|
return size; |
|
|
|
return size; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint64_t Item::getSize() { |
|
|
|
uint64 Item::getSize() { |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|