Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <vector>
4 : : #include <cstdint>
5 : : #include <iterator>
6 : : #include <algorithm>
7 : : #include <array>
8 : : using namespace std;
9 : :
10 : : constexpr uint_fast32_t MOD_MASK = (1ULL << 24) - 1;
11 : : constexpr size_t STATE_SPACE_SIZE = 19 * 19 * 19 * 19;
12 : :
13 : 2 : int main() {
14 : 2 : ifstream inputFile("input.txt");
15 [ + + ]: 2 : if (!inputFile) {
16 : 1 : cerr << "Error: Could not open input file.\n";
17 : 1 : return 1;
18 : : }
19 : 1 : vector<uint_fast32_t> buyerSecrets((istream_iterator<uint_fast32_t>(inputFile)), istream_iterator<uint_fast32_t>());
20 : 1 : array<int_fast16_t, STATE_SPACE_SIZE> sequenceContributions = {0};
21 : 1 : array<uint_fast16_t, STATE_SPACE_SIZE> sequenceLastBuyer = {0};
22 : 1 : int_fast16_t maxContributedValue = 0;
23 : 1 : uint_fast16_t currentBuyerId = 0;
24 : 1 : array<int_fast8_t, 2001> priceHistory = {0};
25 : :
26 [ + + ]: 2364 : for (uint_fast32_t secret : buyerSecrets) {
27 : 2363 : ++currentBuyerId;
28 : :
29 [ + + ]: 4730726 : for (uint_fast16_t iteration = 0; iteration < 2001; ++iteration) {
30 : 4728363 : int_fast8_t currentPrice = secret % 10;
31 : 4728363 : priceHistory[iteration] = currentPrice;
32 : 4728363 : secret ^= (secret << 6) & MOD_MASK;
33 : 4728363 : secret ^= (secret >> 5);
34 : 4728363 : secret ^= (secret << 11) & MOD_MASK;
35 : :
36 [ + + ]: 4728363 : if (iteration >= 4) {
37 : 4718911 : int_fast32_t delta1 = priceHistory[iteration - 4] - priceHistory[iteration - 3];
38 : 4718911 : int_fast32_t delta2 = priceHistory[iteration - 3] - priceHistory[iteration - 2];
39 : 4718911 : int_fast32_t delta3 = priceHistory[iteration - 2] - priceHistory[iteration - 1];
40 : 4718911 : int_fast32_t delta4 = priceHistory[iteration - 1] - currentPrice;
41 : 4718911 : uint_fast32_t stateIndex = (((delta1 + 9) * 19 + (delta2 + 9)) * 19 + (delta3 + 9)) * 19 + (delta4 + 9);
42 : :
43 [ + + ]: 4718911 : if (sequenceLastBuyer[stateIndex] != currentBuyerId) {
44 : 4562615 : sequenceLastBuyer[stateIndex] = currentBuyerId;
45 : 4562615 : sequenceContributions[stateIndex] += currentPrice;
46 : 4562615 : maxContributedValue = max(maxContributedValue, sequenceContributions[stateIndex]);
47 : : }
48 : : }
49 : : }
50 : : }
51 : :
52 : 1 : cout << maxContributedValue << endl;
53 [ + + ]: 3 : }
|