-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.cpp
70 lines (65 loc) · 1.74 KB
/
7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "common.h"
namespace {
struct Dir {
std::string name;
long size;
};
#define ASSERT_SCAN(expr, err) \
if ((expr) == 0) \
throw std::runtime_error((err));
constexpr size_t MaxSize{1024};
Dir solve(size_t &i, const auto &input, auto &dirs) {
Dir d{};
char s[MaxSize];
ASSERT_SCAN(sscanf(input[i++].c_str(), "$ cd %s", s), "No cd");
d.name = s;
ASSERT_EXPR(input[i++] == "$ ls", "No ls");
while (i < input.size() && input[i][0] != '$') {
long sz{};
if (input[i].find("dir") == std::string::npos)
ASSERT_SCAN(sscanf(input[i].c_str(), "%ld ", &sz), "no file");
d.size += sz;
++i;
}
for (bool parsing = i < input.size(); parsing;
parsing = parsing && (i < input.size())) {
ASSERT_SCAN(sscanf(input[i].c_str(), "$ cd %s", s), "No cd X");
if (strcmp(s, "..") == 0) {
++i;
parsing = false;
} else {
const auto &cd = solve(i, input, dirs);
d.size += cd.size;
}
}
dirs.emplace_back(d);
return d;
}
long p1(const auto &input) {
size_t i{0};
std::vector<Dir> dirs;
long best{0};
solve(i, input, dirs);
for (const Dir &d : dirs) {
best += (d.size <= 100000) ? d.size : 0;
}
return best;
}
long p2(const auto &input) {
size_t i{0};
std::vector<Dir> dirs;
Dir root = solve(i, input, dirs);
long req = 30000000 - (70000000 - root.size);
long best{root.size};
for (Dir d : dirs) {
if (d.size >= req)
best = std::min(best, d.size);
}
return best;
}
} // namespace
int main() {
const auto &input = gb::advent2021::readIn();
gb::advent2021::writeOut(std::to_string(p1(input)));
gb::advent2021::writeOut(std::to_string(p2(input)));
}