Skip to content

Instantly share code, notes, and snippets.

@dkaraush
Last active December 6, 2025 11:15
Show Gist options
  • Select an option

  • Save dkaraush/60a1edf68717d13d83c1abd6c2ad6811 to your computer and use it in GitHub Desktop.

Select an option

Save dkaraush/60a1edf68717d13d83c1abd6c2ad6811 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <sstream>
int main() {
std::ifstream file("input.txt");
std::string A, B, C, D, O;
std::getline(file, A);
std::getline(file, B);
std::getline(file, C);
std::getline(file, D);
std::getline(file, O);
unsigned long len = std::max({ A.size(), B.size(), C.size(), D.size(), O.size() }) + 1;
A.resize(len, ' ');
B.resize(len, ' ');
C.resize(len, ' ');
D.resize(len, ' ');
O.resize(len, ' ');
long long result1 = 0, result2 = 0;
long long a1, b1, c1, d1;
std::vector<long long> a2;
char o;
for (int i = 0; i < len; ++i) {
if (A[i] == ' ' && B[i] == ' ' && C[i] == ' ' && D[i] == ' ') {
if (o == '*') {
result1 += a1 * b1 * c1 * d1;
long long x = 1;
for (int j = 0; j < a2.size(); ++j)
x *= a2[j];
result2 += x;
} else if (o == '+') {
result1 += a1 + b1 + c1 + d1;
for (int j = 0; j < a2.size(); ++j)
result2 += a2[j];
}
continue;
}
if (O[i] != ' ') {
a1 = b1 = c1 = d1 = 0;
a2.clear();
o = O[i];
}
char ca = A[i], cb = B[i], cc = C[i], cd = D[i];
long long x2 = 0;
if (ca >= '0' && ca <= '9') {
a1 = (ca - '0') + 10 * a1;
x2 = (ca - '0') + 10 * x2;
}
if (cb >= '0' && cb <= '9') {
b1 = (cb - '0') + 10 * b1;
x2 = (cb - '0') + 10 * x2;
}
if (cc >= '0' && cc <= '9') {
c1 = (cc - '0') + 10 * c1;
x2 = (cc - '0') + 10 * x2;
}
if (cd >= '0' && cd <= '9') {
d1 = (cd - '0') + 10 * d1;
x2 = (cd - '0') + 10 * x2;
}
a2.push_back(x2);
}
std::cout << result1 << std::endl;
std::cout << result2 << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment