-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc07-00.cpp
72 lines (61 loc) · 1.31 KB
/
aoc07-00.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
71
72
#include <iostream>
#include <string>
#include <unordered_map>
#include <set>
using namespace std;
const unsigned MAX_SIZE = 100000;
string op, curDir = "";
unordered_map< string, set<string> > dir;
unordered_map< string, unsigned > sz;
inline void cd() {
string der;
cin>>der;
if (der == "..") {
curDir.pop_back();
while (curDir.back() != '/') curDir.pop_back();
} else {
if (der == "/") curDir = "/";
else curDir += der + "/";
}
}
void der() {
string der;
cin>>der;
der = curDir + der + "/";
dir[curDir].insert(der);
}
void file() {
unsigned size = stoi(op);
string file;
cin>>file;
file = curDir + file;
dir[curDir].insert(file);
sz[file] = size;
}
unsigned ans = 0;
int dfs(string der) {
unsigned size = 0;
for (auto obj : dir[der]) {
if (obj.back() == '/') size += dfs(obj);
else size += sz[obj];
}
sz[der] = size;
if (size <= MAX_SIZE) ans += size;
return size;
}
int main() {
freopen("07.in", "r", stdin);
freopen("07.out", "w", stdout);
while (cin>>op) {
if (op == "$") {
cin>>op;
if (op == "cd") cd();
} else {
if (op == "dir") der();
else file();
}
}
dfs("/");
cout<<ans<<endl;
return 0;
}