Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <regex>
4 : : using namespace std;
5 : :
6 : 2 : int main() {
7 : 2 : ifstream infile("input.txt");
8 [ + + ]: 2 : if (!infile) {
9 : 1 : cerr << "Error opening file" << endl;
10 : 1 : return 1;
11 : : }
12 : :
13 : 1 : string content((istreambuf_iterator<char>(infile)), istreambuf_iterator<char>());
14 : 1 : regex rx(R"(Button A: X([+-]?\d+), Y([+-]?\d+)\nButton B: X([+-]?\d+), Y([+-]?\d+)\nPrize: X=([+-]?\d+), Y=([+-]?\d+))");
15 : :
16 : 1 : uint64_t res = 0;
17 [ + + ]: 321 : for (sregex_iterator it(content.begin(), content.end(), rx), end; it != end; ++it) {
18 : 320 : auto& m = *it;
19 : 320 : int64_t ax = stoi(m[1]);
20 : 320 : int64_t ay = stoi(m[2]);
21 : 320 : int64_t bx = stoi(m[3]);
22 : 320 : int64_t by = stoi(m[4]);
23 : 320 : int64_t X = stoll(m[5]);
24 : 320 : int64_t Y = stoll(m[6]);
25 : 320 : int64_t b = (Y * ax - X * ay) / (by * ax - bx * ay);
26 : 320 : int64_t a = (X - b * bx) / ax;
27 [ + + + + ]: 320 : if (a * ax + b * bx == X && a * ay + b * by == Y) {
28 : 173 : res += 3 * a + b;
29 : : }
30 : 1 : }
31 : :
32 : 1 : cout << res << endl;
33 [ + + ]: 3 : }
|