-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_07a.cpp
148 lines (139 loc) · 4.44 KB
/
day_07a.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
struct File {
std::string name;
long long size;
};
struct Directory {
std::string name;
std::vector<std::unique_ptr<Directory>> dirs;
std::vector<std::unique_ptr<File>> files;
Directory * parent;
long long size;
};
void traverse(Directory * main_dir, int level) {
for (int i = 0; i < level; i++) {
std::cout << " ";
}
std::cout << "- " << main_dir->name << "(dir)" << '\n';
for (const auto& dir : main_dir->dirs) {
traverse(dir.get(), level + 1);
}
for (const auto& file : main_dir->files) {
for (int i = 0; i < level+1; i++) {
std::cout << " ";
}
std::cout << "- " << file->name << "(file, size=" << file->size << ')' << '\n';
}
}
long long update_sizes(Directory * main_dir) {
for (const auto& dir : main_dir->dirs) {
main_dir->size += update_sizes(dir.get());
}
for (const auto& file : main_dir->files) {
main_dir->size += file->size;
}
return main_dir->size;
}
std::tuple<long long, long long> count_dirs_lte_threshold(Directory * main_dir, long long threshold) {
long long sum = 0;
long long count = 0;
for (const auto& dir : main_dir->dirs) {
if (dir->size <= threshold) {
std::cout << dir->name << '\n';
count++;
sum += dir->size;
}
const auto ans = count_dirs_lte_threshold(dir.get(), threshold);
count += std::get<0>(ans);
sum += std::get<1>(ans);
}
return {count, sum};
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_07_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::unique_ptr<Directory> file_system = std::make_unique<Directory>();
file_system->name = '/';
file_system->parent = file_system.get();
auto current = file_system.get();
bool skip = false;
while(skip || std::getline(file, line)) {
skip = false;
if (line[0] == '$') {
const auto i = line.find(' ', 2);
const auto cmd = line.substr(2, i - 2);
if (cmd == "cd") {
const auto next = line.substr(i+1, line.size() - 1 - i);
if (next == "..") current = current->parent;
else if (next == "/") current = file_system.get();
else {
bool found = false;
for (const auto& dir : current->dirs) {
if (dir->name == next) {
found = true;
current = dir.get();
std::cout << "Current dir is now: " << current->name << '\n';
break;
}
}
if (!found) {
std::cout << "Adding dir: " << next << '\n';
current->dirs.emplace_back();
current->dirs.back()->name = next;
current->dirs.back()->parent = current;
current = current->dirs.back().get();
std::cout << "Current dir is now: " << current->name << '\n';
}
}
} else if (cmd == "ls") {
while (std::getline(file, line)) {
if (line.substr(0, 3) == "dir") {
const auto dir_name = line.substr(4, line.size() - 4);
bool found = false;
for (const auto& dir : current->dirs) {
if (dir->name == dir_name) {
found = true;
}
}
if (!found) {
std::cout << "Adding dir: " << dir_name << '\n';
current->dirs.push_back(std::make_unique<Directory>());
current->dirs.back()->parent = current;
current->dirs.back()->name = dir_name;
}
} else if (line[0] != '$') {
const auto i = line.find(' ');
const auto file_size = line.substr(0, i);
const auto file_name = line.substr(i + 1, line.size() - i - 1);
std::cout << "Adding file: " << file_name << " with size " << file_size << '\n';
current->files.push_back(std::make_unique<File>());
current->files.back()->size = std::stoll(file_size);
current->files.back()->name = file_name;
} else {
skip = true;
break;
}
}
}
}
}
traverse(file_system.get(), 0);
update_sizes(file_system.get());
const long long threshold = 100000;
auto [count, sum] = count_dirs_lte_threshold(file_system.get(), threshold);
if (file_system->size <= threshold) {
count += 1;
sum += file_system->size;
}
std::cout << count << ' ' << sum << '\n';
return 0;
}