Advertisement
Guest User

AOC-2022-D7

a guest
Dec 8th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     string name;
  6.     long long sz = 0;
  7.     Node* parent;
  8.     map<string,Node*> children;
  9.     Node(string _n) : name(_n), sz(0LL), parent(nullptr), children() {}
  10. };
  11.  
  12. void print(Node *node, int dep = 0) {
  13.     if(!node) {
  14.         return;
  15.     }
  16.     for(int i = 0; i < dep; i++) {
  17.         cout << "--";
  18.     }
  19.     cout << '{' << node->name << ',' << node->sz << "}\n";
  20.     for(pair<string,Node*> child : node->children) {
  21.         print(child.second, dep + 1);
  22.     }
  23. }
  24.  
  25. map<Node*,long long> total_sz;
  26.  
  27. void dfs(Node *node) {
  28.     if(!node) {
  29.         return;
  30.     }
  31.     total_sz[node] = node->sz;
  32.     for(pair<string,Node*> child : node->children) {
  33.         dfs(child.second);
  34.         total_sz[node] += total_sz[child.second];
  35.     }
  36. }
  37.  
  38. void clear(Node *node) {
  39.     if(!node) {
  40.         return;
  41.     }
  42.     for(pair<string,Node*> child : node->children) {
  43.         clear(child.second);
  44.     }
  45.     free(node);
  46. }
  47.  
  48. bool isNumber(string &s) {
  49.     for(char ch : s) {
  50.         if(ch < '0' || ch > '9') {
  51.             return false;
  52.         }
  53.     }
  54.     return true;
  55. }
  56.  
  57. int main() {
  58.     ios::sync_with_stdio(0);
  59.     cin.tie(0);
  60.     string line;
  61.     Node *curr = nullptr;
  62.     Node *head = nullptr;
  63.     while(getline(cin, line)) {
  64.         stringstream x(line);
  65.         vector<string> words;
  66.         string word;
  67.         while(x.good()) {
  68.             x >> word;
  69.             words.push_back(word);
  70.         }
  71.         if(words[0] == "$") {
  72.             if(words[1] == "cd") {
  73.                 if(curr == nullptr) {
  74.                     curr = new Node(words[2]);
  75.                     head = curr;
  76.                 }
  77.                 else if(words[2] == "..") {
  78.                     curr = curr->parent;
  79.                 }
  80.                 else {
  81.                     if(curr->children[words[2]]) {
  82.                         curr = curr->children[words[2]];
  83.                     }
  84.                     else {
  85.                         Node *new_node = new Node(words[2]);
  86.                         curr->children[new_node->name] = new_node;
  87.                         new_node->parent = curr;
  88.                         curr = curr->children[words[2]];
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.         else if(isNumber(words[0])) {
  94.             curr->sz += stoll(words[0]);
  95.         }
  96.     }
  97.     //print(head);
  98.     dfs(head);
  99.     //for(pair<Node*,long long> node : total_sz) {
  100.     //  cout << '{' << node.first->name << ',' << node.second << "}\n";
  101.     //}
  102.     long long ans = 0LL;
  103.     const long long MX = 100000LL;
  104.     map<string, long long> score;
  105.     for(pair<Node*,long long> node : total_sz) {
  106.         score[node.first->name] = node.second;
  107.         if(node.second <= MX) {
  108.             ans += node.second;
  109.         }
  110.     }  
  111.     //for(auto p : score) {
  112.     //  cerr << p.first << ": " << p.second << '\n';
  113.     //}
  114.     cout << "part1: ";
  115.     cout << ans << '\n';
  116.     long long req = 30000000LL - (70000000LL - total_sz[head]);
  117.     long long mn = 1e18;
  118.     for(pair<Node*,long long> node : total_sz) {
  119.         if(node.second >= req) {
  120.             mn = min(mn, node.second);
  121.         }
  122.     }  
  123.     cout << "part2: ";
  124.     cout << mn << '\n';
  125.     clear(head);
  126.     return 0;
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement