diff --git a/day 22/day_22_felix.cpp b/day 22/day_22_felix.cpp index 1c503dfe59fe1b37ab60ed870ce3f8fb2206c5b4..10bdfaf114773bce1f4b96af12c2afab77e34cea 100644 --- a/day 22/day_22_felix.cpp +++ b/day 22/day_22_felix.cpp @@ -14,6 +14,8 @@ bool DEBUG = false; // Standard definitions for many problems #define coord pair <int, int> #define ll long long +#define sqbool array<array<array<array<bool, 19>, 19>, 19>, 19> +#define sqll array<array<array<array<ll, 19>, 19>, 19>, 19> vector<ll> STARTING_VALUES; @@ -36,9 +38,7 @@ ll step(ll val) { void read_input() { string line; - while (getline(cin, line)) { - STARTING_VALUES.push_back(stoll(line)); - } + while (getline(cin, line)) STARTING_VALUES.push_back(stoll(line)); } ll part1() { @@ -65,7 +65,7 @@ vector<ll> generate_price_sequence(ll start, int num_steps = 2000) { vector<ll> generate_price_changes(vector<ll> prices) { vector<ll> changes; for (int i = 0; i < prices.size() - 1; i++) { - changes.push_back(prices[i+1] - prices[i]); + changes.push_back(prices[i+1] - prices[i] + 9); } return changes; } @@ -76,48 +76,27 @@ void print_sequence(array<ll,4> sequence) { } ll part2() { - map<array<ll,4>, ll> total_sequence_values; + sqll total_sequence_values = {0}; + ll total = 0; // Loop over each monkey for (ll val : STARTING_VALUES) { - map<array<ll,4>, ll> best_values; + sqbool found_sequences = {false}; vector<ll> prices = generate_price_sequence(val); vector<ll> changes = generate_price_changes(prices); - // Check the best value that each sequence can get for this monkey - // Note to past Felix: Don't find the BEST value, find the FIRST value. + // Check the first time each sequence comes up for (int i = 3; i < changes.size(); i++) { - array<ll,4> sequence = {changes[i-3], changes[i-2], changes[i-1], changes[i]}; - - if (best_values.find(sequence) == best_values.end()) { - best_values[sequence] = prices[i+1]; - // } else { // SHIT the monkey takes the first price, not the best... Read before you code, kids. - // best_values[sequence] = max(best_values[sequence], prices[i+1]); - } - } + int q1 = changes[i-3], q2 = changes[i-2], q3 = changes[i-1], q4 = changes[i]; - // For each sequence, add this up to the total that would be generated - for (auto [sequence, value] : best_values) { - if (total_sequence_values.find(sequence) == total_sequence_values.end()) { - total_sequence_values[sequence] = value; - } else { - total_sequence_values[sequence] += value; + if (!found_sequences[q1][q2][q3][q4]) { + total_sequence_values[q1][q2][q3][q4] += prices[i+1]; + total = max(total, total_sequence_values[q1][q2][q3][q4]); + found_sequences[q1][q2][q3][q4] = true; } } } - // Find the best sequence to use - ll total = 0; - for (auto [sequence, value] : total_sequence_values) { - if (value > total) { - total = value; - if (DEBUG) { - cout << "New best sequence: "; - print_sequence(sequence); - cout << " Value: " << value << endl; - } - } - } return total; }