Vaya coniazo de parsing.. pog.
edit: estoy en el tren haciendo todos los que me faltan... full cerdo coding.
D11 p1 c++
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>
#include <numeric>
#include <functional>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <array>
#include <iostream>
#include <sstream>
#include <string>
#include <optional>
using namespace std;
struct Func{
char op;
optional<int> a;
optional<int> b;
int divisible;
int t_true;
int t_false;
};
struct Monkey
{
int id;
unordered_multiset<int> items;
Func f;
};
int main()
{
auto parseList = [](){
string raw;
getline(cin, raw);
stringstream ss(raw);
unordered_multiset<int> items;
string temp;
int i;
while(!ss.eof())
{
ss >> temp;
if(stringstream(temp) >> i)
items.emplace(i);
temp = "";
}
return items;
};
auto parseOperator = [](string s) -> optional<int>
{
try
{
int num = stoi(s);
return num;
} catch(...)
{
}
return {};
};
auto parseFunc = [&]()
{
Func f;
string raw;
getline(cin, raw);
string a(5,' ');
string b(5,' ');
string c(5,' ');
sscanf(raw.c_str(), " Operation: new = %s %s %s", &a[0], &b[0], &c[0]);
f.a = parseOperator(a);
f.op = b[0];
f.b = parseOperator(c);
getline(cin, raw);
sscanf(raw.c_str(), " Test: divisible by %d", &f.divisible);
getline(cin, raw);
sscanf(raw.c_str()," If true: throw to monkey %d", &f.t_true);
getline(cin, raw);
sscanf(raw.c_str()," If false: throw to monkey %d", &f.t_false);
return f;
};
auto parseMonkey = [&]()
{
Monkey m;
string raw;
getline(cin, raw);
sscanf(raw.c_str(), "Monkey %i:", &m.id);
m.items = parseList();
m.f = parseFunc();
return m;
};
auto parse = [&]()
{
unordered_map<int, Monkey> ms;
try{
while(true)
{
auto m = parseMonkey();
ms[m.id] = move(m);
string r;
if(!getline(cin, r)) // clear
throw exception();
}
} catch(...)
{}
return ms;
};
auto evalOp = [](Func& f, int item) -> pair<int,int>{
int a = [&](){
switch(f.op)
{
case '*': return (f.a.value_or(item) * f.b.value_or(item))/3;
case '/': return (f.a.value_or(item) / f.b.value_or(item))/3;
case '+': return (f.a.value_or(item) + f.b.value_or(item))/3;
case '-': return (f.a.value_or(item) - f.b.value_or(item))/3;
}
return -1;
}();
return {a, a% f.divisible == 0 ? f.t_true : f.t_false};
};
auto eval = [&](auto& m, int i){
auto uglycopy = m[i].items;
for(auto& item: uglycopy)
{
auto [worry,new_monkey] = evalOp(m[i].f, item);
m[i].items.erase(item);
m[new_monkey].items.emplace(worry);
}
return uglycopy.size();
};
auto solve = [&]()
{
auto monkeys = parse();
unordered_map<int, int> counter;
for(int i = 0; i < 20; ++i)
{
for(int j = 0 ; j < monkeys.size(); ++j)
counter[j] += eval(monkeys, j);
}
priority_queue<int, vector<int>, greater<int>> pq;
for(auto&[k,v]: counter)
{
pq.push(v);
if(pq.size()>2)
pq.pop();
}
int a = pq.top(); pq.pop();
int b = pq.top(); pq.pop();
cout << a << endl;
cout << b << endl;
cout << a*b << endl;
};
solve();
return 0;
}
D4 c++
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>
#include <numeric>
#include <functional>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <array>
#include <iostream>
#include <sstream>
#include <string>
#include <optional>
#include <stack>
using namespace std;
int main()
{
//1 // 5 // 9
auto parseStack = [&]() -> vector<char>
{
char a,b,c;
string s;
getline(cin, s);
if(isdigit(s[1]))
throw exception();
int cols = (s.size()+1)/4;
vector<char> ret;
for(int i = 0;i < cols; ++i)
ret.emplace_back(s[i*4 + 1]);
return ret;
};
auto parseOp = [&]() -> tuple<int,int,int>
{
int quan, from, to;
string raw;
if(!(getline(cin, raw)))
throw exception();
sscanf(raw.c_str(), "move %d from %d to %d", &quan, &from, &to);
return {quan, from-1, to-1};
};
auto solve = [&](int question)
{
vector<deque<char>> stacks(100, deque<char>());
try{
while(true)
{
auto v = parseStack();
for(int i = 0 ; i < v.size(); ++i)
{
if(v[i] != ' ')
stacks[i].push_front(v[i]);
}
}
} catch(...)
{}
try{
string s;
getline(cin, s);
while(true)
{
auto [quantity, from, to] = parseOp();
if(question == 2)
{
stack<char> qu;
while(quantity--)
{
if(!stacks[from].empty())
{
qu.push(stacks[from].back());
stacks[from].pop_back();
}
}
while(!qu.empty())
{
stacks[to].push_back(qu.top());
qu.pop();
}
}
else
{
while(quantity--)
{
if(!stacks[from].empty())
{
stacks[to].push_back(stacks[from].back());
stacks[from].pop_back();
}
}
}
}
} catch(...)
{}
for(int i = 0 ; i < stacks.size(); ++i)
if(!stacks[i].empty())
cout << stacks[i].back();
else
cout << " ";
};
solve(2);
return 0;
}
D5 c++
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>
#include <numeric>
#include <functional>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <array>
#include <iostream>
#include <sstream>
#include <string>
#include <optional>
#include <stack>
using namespace std;
int main()
{
//1 // 5 // 9
auto parseStack = [&]() -> vector<char>
{
char a,b,c;
string s;
getline(cin, s);
if(isdigit(s[1]))
throw exception();
int cols = (s.size()+1)/4;
vector<char> ret;
for(int i = 0;i < cols; ++i)
ret.emplace_back(s[i*4 + 1]);
return ret;
};
auto parseOp = [&]() -> tuple<int,int,int>
{
int quan, from, to;
string raw;
if(!(getline(cin, raw)))
throw exception();
sscanf(raw.c_str(), "move %d from %d to %d", &quan, &from, &to);
return {quan, from-1, to-1};
};
auto solve = [&](int question)
{
vector<deque<char>> stacks(100, deque<char>());
try{
while(true)
{
auto v = parseStack();
for(int i = 0 ; i < v.size(); ++i)
{
if(v[i] != ' ')
stacks[i].push_front(v[i]);
}
}
} catch(...)
{}
try{
string s;
getline(cin, s);
while(true)
{
auto [quantity, from, to] = parseOp();
if(question == 2)
{
stack<char> qu;
while(quantity--)
{
if(!stacks[from].empty())
{
qu.push(stacks[from].back());
stacks[from].pop_back();
}
}
while(!qu.empty())
{
stacks[to].push_back(qu.top());
qu.pop();
}
}
else
{
while(quantity--)
{
if(!stacks[from].empty())
{
stacks[to].push_back(stacks[from].back());
stacks[from].pop_back();
}
}
}
}
} catch(...)
{}
for(int i = 0 ; i < stacks.size(); ++i)
if(!stacks[i].empty())
cout << stacks[i].back();
else
cout << " ";
};
solve(2);
return 0;
}