-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec7.cpp
More file actions
97 lines (73 loc) · 2.09 KB
/
Dec7.cpp
File metadata and controls
97 lines (73 loc) · 2.09 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
int createstack(std::ifstream& file,std::vector<int>& dir_size){
std::string str{};
int file_size{};
long long stack_size{};
while (getline(file,str)){
std::stringstream ss{str};
char dummy{};
std::string name{};
if (str[0]=='$'){
if (str[2]=='c'){
ss>>dummy>>dummy>>dummy;
ss>>name;
if (name[0]!='.'){
stack_size += createstack(file,dir_size);
}
else{
dir_size.push_back(stack_size);
return stack_size;
}
}else continue;
}else{
if (str[0]=='d') continue;
else{
ss>>file_size;
stack_size += file_size;
}
}
}
dir_size.push_back(stack_size);
return stack_size;
}
int main(){
std::ifstream file("data7.txt");
std::string str{};
std::vector<int> dir_size{};
while (getline(file,str)){
std::stringstream ss{str};
char dummy{};
std::string name{};
if (str[0]=='$'){
if (str[2]=='c'){
ss>>dummy>>dummy>>dummy;
ss>>name;
createstack(file,dir_size);
}
}
}
int total{};
for (auto it=dir_size.begin();it<dir_size.end();++it){
if (*it>100000) continue;
else total += *it;
}
std::cout<<total<<'\n';
//PART TWO
constexpr int totalspace{70000000};
constexpr int minspace{30000000};
const int usedspace{*std::max_element(dir_size.begin(),dir_size.end())};
const int unusedspace{totalspace-usedspace};
const int requiredspace{minspace-unusedspace};
int currentchoice{INT_MAX};
for (auto it=dir_size.begin();it<dir_size.end();++it){
if (*it>=requiredspace){
currentchoice = std::min(currentchoice,*it);
}
}
std::cout<<"Eliminate "<<currentchoice<<'\n';
return 0;
}