-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7a.cpp
126 lines (112 loc) · 2.69 KB
/
day7a.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "utils.hpp"
int file_sum = 0;
class folder
{
private:
std::string _name;
std::vector<std::pair<std::string, int>> _files;
std::vector<folder> _child_folders;
folder *_parent_folder;
public:
folder(std::string name, folder *parent_folder);
std::string get_name();
void add_file(std::string name, int size);
void add_child_folder(std::string);
folder *get_parent_folder();
folder *move_to_folder(std::string name);
int total_size();
~folder();
};
folder::folder(std::string name, folder *parent_folder)
{
_name = name;
_parent_folder = parent_folder;
}
folder::~folder()
{
}
std::string folder::get_name()
{
return _name;
}
void folder::add_file(std::string name, int size)
{
_files.push_back(std::make_pair(name, size));
}
void folder::add_child_folder(std::string name)
{
auto new_folder = folder(name, this);
_child_folders.push_back(new_folder);
}
folder *folder::get_parent_folder()
{
return _parent_folder;
}
folder *folder::move_to_folder(std::string name)
{
for (auto &&x : _child_folders)
{
if (x.get_name() == name)
{
return &x;
}
}
std::cout << "oops"
<< "\n";
exit(1);
}
int folder::total_size()
{
int size = 0;
for (auto &&x : _child_folders)
{
std::cout << x.get_name() << "\n";
size += x.total_size();
}
for (auto &&y : _files)
{
std::cout << y.first << " : " << y.second << "\n";
size += y.second;
}
// We only hit this once for each folder, so when we do just
// add to a global.
if (size <= 100000)
{
file_sum += size;
}
return size;
}
int main(int argc, char *argv[])
{
auto input = read_input("day7_input");
auto root_folder = folder("/", nullptr);
auto active_folder = &root_folder;
for (auto &&x : input)
{
if (x.find("$ ls") != std::string::npos)
{
continue;
}
else if (x.find("dir") != std::string::npos)
{
active_folder->add_child_folder(x.substr(4));
}
else if (x.find("$ cd ..") != std::string::npos)
{
active_folder = active_folder->get_parent_folder();
}
else if (x.find("$ cd") != std::string::npos)
{
active_folder = active_folder->move_to_folder(x.substr(5));
}
else
{
auto delimeter = " ";
auto size = x.substr(0, x.find(delimeter));
auto filename = x.substr(x.find(delimeter) + 1);
active_folder->add_file(filename, std::stoi(size));
}
}
std::cout << root_folder.total_size() << "\n";
std::cout << file_sum << "\n";
}