Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <sstream>
4 : : #include <vector>
5 : : #include <cstdint>
6 : : #include <cmath>
7 : : #include <iterator>
8 : :
9 : : using namespace std;
10 : :
11 : 18831 : bool isValid(uint64_t target, const vector<uint64_t>& nums, size_t idx) {
12 [ + + ]: 18831 : if (idx == 0) {
13 : 4612 : return nums[0] == target;
14 : : }
15 : :
16 : 14219 : uint64_t curr = nums[idx];
17 : 14219 : uint64_t divisor = pow(10, (int) (log10(curr) + 1));
18 : :
19 [ + + + + ]: 14219 : return (target % curr == 0 && isValid(target / curr, nums, idx - 1)) ||
20 [ + + + + ]: 40642 : (target > curr && isValid(target - curr, nums, idx - 1)) ||
21 [ + + + + ]: 26473 : (target % divisor == curr && isValid(target / divisor, nums, idx - 1));
22 : : }
23 : :
24 : 2 : int main() {
25 : 2 : ifstream in("input.txt");
26 [ + + ]: 2 : if (!in) {
27 : 1 : cerr << "Error: Could not open input file.\n";
28 : 1 : return 1;
29 : : }
30 : 1 : uint64_t total = 0;
31 : :
32 [ + + ]: 851 : for (string line; getline(in, line); ) {
33 : 850 : stringstream ss(line);
34 : : uint64_t target;
35 : : char colon;
36 : 850 : ss >> target >> colon;
37 : 1700 : vector<uint64_t> nums{istream_iterator<uint64_t>{ss}, {}};
38 [ + + ]: 850 : total += isValid(target, nums, nums.size() - 1) ? target : 0;
39 : 851 : }
40 : :
41 : 1 : cout << total << endl;
42 [ + + ]: 3 : }
|