summaryrefslogtreecommitdiff
path: root/2022/Cpp/Day7/main.cpp
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-04-09 03:09:19 +0200
committeromagdy7 <omar.professional8777@gmail.com>2023-04-09 03:09:19 +0200
commitc827efeeef0cdc63c5366e9d945fa53c09988491 (patch)
treee33361fa13742f5fd295e1e8525f388fca7fdda4 /2022/Cpp/Day7/main.cpp
parentd5e8f8cb89c24e02898eed760ea22f0522e59f44 (diff)
downloadaoc-c827efeeef0cdc63c5366e9d945fa53c09988491.tar.xz
aoc-c827efeeef0cdc63c5366e9d945fa53c09988491.zip
Solve day7 in rust
Diffstat (limited to '2022/Cpp/Day7/main.cpp')
-rwxr-xr-x2022/Cpp/Day7/main.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/2022/Cpp/Day7/main.cpp b/2022/Cpp/Day7/main.cpp
new file mode 100755
index 0000000..893691c
--- /dev/null
+++ b/2022/Cpp/Day7/main.cpp
@@ -0,0 +1,122 @@
+#include<bits/stdc++.h>
+
+using namespace std;
+
+//pretty printing
+template<typename K, typename V>
+void printm(const map<K, V> &mp) {
+ cerr << "{" << endl;
+ for (auto p : mp) {
+ cerr << " { " << p.first << " : " << p.second << " }\n";
+ }
+ cerr << "}" << endl;
+}
+template<typename T>
+void printv(const vector<T> &v) {
+ cerr << "[";
+ for (int i = 0; i < v.size(); i++) {
+ if (i == v.size() - 1) {
+ cerr << v[i];
+ } else {
+ cerr << v[i] << ", ";
+ }
+ }
+ cerr << "]\n";
+}
+
+template<typename T>
+void printvv(const vector<vector<T>> &v) {
+ cerr << "[\n";
+ for (auto &vec : v) {
+ cout << " ";
+ printv(vec);
+ }
+ cerr << "]\n";
+}
+void print() {
+ cerr << "\n";
+}
+
+template<typename T, typename... TS>
+void print(T val, TS... vals) {
+ cerr << val << " ";
+ print(vals...);
+}
+
+bool isDir(string &s) {
+ return s[0] == 'd';
+}
+
+bool isFile(string &s) {
+ return s[0] >= '0' && s[0] <= '9';
+}
+
+long long sumDir(string fileName, map<string, vector<string>> &dirContent) {
+ if (isFile(fileName)) {
+ int firstSpace = fileName.find(' ');
+ cout << fileName.substr(0, firstSpace) << '\n';
+ return stoi(fileName.substr(0, firstSpace));
+ } else {
+ long long sum = 0;
+ for (auto subfile : dirContent[fileName]) {
+ // cout << subfile << '\n';
+ if (isFile(subfile)) {
+ sum += sumDir(subfile, dirContent);
+ } else {
+ sum += sumDir(subfile, dirContent);
+ }
+ }
+ return sum;
+ }
+}
+
+int main() {
+ string s;
+ vector<string> dirs;
+ map<string, long long> mp;
+ map<string, vector<string>> dirContent;
+ string cwd = "/";
+ vector<string> lines;
+
+ while(getline(cin, s)) {
+ lines.push_back(s);
+ }
+
+ for (int i = 0; i < lines.size(); i++) {
+ s = lines[i];
+ if (s[0] == '$') {
+ // it's a command
+ if (s[2] == 'l') {
+ // ls command
+ string prevline = lines[i - 1];
+ string tmp = prevline.substr(prevline.find(' ', 2) + 1);
+ cwd = tmp;
+ // cout << cwd << '\n';
+ }
+ } else {
+ // it's a file or a dir
+ string prevline = lines[i - 1];
+ if (isDir(s)) {
+ string tmp = s.substr(s.find(' ') + 1);
+ dirContent[cwd].push_back(tmp);
+ } else if (isFile(s)) {
+ dirContent[cwd].push_back(s);
+ // cout << "file size: " << fsize << '\n';
+ // mp[cwd] += fsize;
+ }
+ }
+ }
+
+ long long ans = 0;
+ for (auto [dir, ls] : dirContent) {
+ cout << "cwd: " << dir << '\n';
+ cout << "\t\t";
+ printv(ls);
+ // long long sum = sumDir(dir, dirContent);
+ // if (sum <= 100000) {
+ // ans += sum;
+ // }
+ }
+ cout << ans << '\n';
+}
+