Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <vector>
4 : : #include <string>
5 : : #include <unordered_map>
6 : : #include <numeric>
7 : : #include <cstdint>
8 : :
9 : : using namespace std;
10 : :
11 : 2 : int main() {
12 : 2 : ifstream inputFile("input.txt");
13 [ + + ]: 2 : if (!inputFile) {
14 : 1 : cerr << "Error: input.txt not found or inaccessible." << endl;
15 : 1 : return 1;
16 : : }
17 : :
18 : 1 : unordered_map<uint64_t, uint64_t> stones;
19 : 1 : uint_fast8_t blinks = 25;
20 : :
21 [ + + ]: 9 : for (int num; inputFile >> num; ++stones[num]);
22 : :
23 : 1 : unordered_map<uint64_t, uint64_t> next;
24 [ + + ]: 26 : while (blinks--) {
25 : 25 : next.clear();
26 : :
27 [ + + ]: 2558 : for (const auto& [stone, count] : stones) {
28 [ + + ]: 2533 : if (stone == 0) {
29 : 21 : next[1] += count;
30 [ + + ]: 2512 : } else if (to_string(stone).size() % 2 == 0) {
31 : 1558 : string s = to_string(stone);
32 : 1558 : int mid = static_cast<int>(s.size()) / 2;
33 : 1558 : next[(uint64_t)stoll(s.substr(0, mid))] += count;
34 : 1558 : next[(uint64_t)stoll(s.substr(mid))] += count;
35 : 1558 : } else {
36 : 954 : next[stone * 2024] += count;
37 : : }
38 : : }
39 : :
40 : 25 : stones = std::move(next);
41 : : }
42 : :
43 : 315 : cout << accumulate(stones.begin(), stones.end(), 0LL, [](long long sum, const auto& p) { return sum + p.second; }) << endl;
44 [ + + ]: 3 : }
|