-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2207.cc
91 lines (78 loc) · 1.43 KB
/
aoc2207.cc
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
#include "iostream"
#include "sstream"
#include "vector"
#include "map"
using namespace std;
bool is_number(string &);
int main()
{
map<string, int>::iterator it;
map<string, int> mp;
vector<string> a;
vector<string> lv; // current lvl
string s, sls = "/";
int i, np; // res;
int ii;
long long r;
long long ll, togo;
long long top=4*(int)1e7;
while (getline(cin, s))
a.push_back(s);
r = 0;
for (string& l: a)
{
stringstream ss(l);
string n;
ss >> n;
if (n == "$")
{
ss >> n;
if (n != "cd")
continue ;
ss >> n;
if (n == "..")
lv.pop_back();
else
lv.push_back(n);
}
else if (is_number(n))
{
ll = stoll(n);
i = 0;
mp[sls] += ll;
// cout << lv.size() << " levels" << endl;
while (++i < (int) lv.size())
{
string tmp = sls;
ii = -1;
while (++ii < i + 1)
tmp += lv[ii] + sls;
// cout << "composed: " << tmp << endl;
mp[tmp] += ll;
}
}
}
long long r2 = (int) 1e9;
togo = mp[sls] - top;
it = mp.begin();
while (it != mp.end())
{
// cout << it->first << endl;
if (it->second < 100000)
r += it->second;
if (it->second > togo)
r2 = it->second < r2 ? it->second : r2;
it++;
}
// cout << "\n";
cout << "Star 1: " << r << endl;
cout << "Star 2: " << r2 << endl;
}
bool is_number(string &s)
{
string::iterator it;
it = s.begin();
while (it != s.end() && isdigit(* it))
++it;
return ( !s.empty() && it == s.end());
}