<p>There is a room with <code>n</code> bulbs labeled from <code>1</code> to <code>n</code> that all are turned on initially, and <strong>four buttons</strong> on the wall. Each of the four buttons has a different functionality where:</p>

<ul>
	<li><strong>Button 1:</strong> Flips the status of all the bulbs.</li>
	<li><strong>Button 2:</strong> Flips the status of all the bulbs with even labels (i.e., <code>2, 4, ...</code>).</li>
	<li><strong>Button 3:</strong> Flips the status of all the bulbs with odd labels (i.e., <code>1, 3, ...</code>).</li>
	<li><strong>Button 4:</strong> Flips the status of all the bulbs with a label <code>j = 3k + 1</code> where <code>k = 0, 1, 2, ...</code> (i.e., <code>1, 4, 7, 10, ...</code>).</li>
</ul>

<p>You must make <strong>exactly</strong> <code>presses</code> button presses in total. For each press, you may pick <strong>any</strong> of the four buttons to press.</p>

<p>Given the two integers <code>n</code> and <code>presses</code>, return <em>the number of <strong>different possible statuses</strong> after performing all </em><code>presses</code><em> button presses</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 1, presses = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> Status can be:
- [off] by pressing button 1
- [on] by pressing button 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 2, presses = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> Status can be:
- [off, off] by pressing button 1
- [on, off] by pressing button 2
- [off, on] by pressing button 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 3, presses = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> Status can be:
- [off, off, off] by pressing button 1
- [off, on, off] by pressing button 2
- [on, off, on] by pressing button 3
- [off, on, on] by pressing button 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 1000</code></li>
	<li><code>0 &lt;= presses &lt;= 1000</code></li>
</ul>

class Solution {
public:

    int flipLights(int n, int presses) {
     // Pressing a button twice will just undo the effect of the first press event.
     // Meaning: we can have max 2^4 == 16 different outcomes; however in practice
     // some of these could be identical.

     unordered_set<vector<bool>> states_tracked;
     states_tracked.insert(vector<bool>(n, true));
     unordered_set<vector<bool>> states_tracked_new;
     int tried[4] = {0, 0, 0, 0};
     for (int press = 0; press < presses; ++press) {
        for (int i = 0; i < 4; ++i) {
            ++tried[i];
            for(const auto& permutation : states_tracked) {
                states_tracked_new.insert(applyPermutation(permutation, i));
            }
        }
        states_tracked = states_tracked_new;
        states_tracked_new.clear();
        if (all_of(begin(tried), end(tried), [](int i) { return i == 3; })) {
            break;
        }
     }  
     return states_tracked.size();
    }

    vector<bool> applyPermutation(const vector<bool>& permutation, int pressedButton) {
        vector<bool> output = permutation;
        switch (pressedButton) {
            case 0:
                for (int i = 0; i < output.size(); ++i) {
                    output[i] = !output[i];
                }
                break;
            case 1:
                for (int i = 0; i < output.size(); ++i) {
                    if ((i + 1) % 2 == 0) {  // 2, 4
                        output[i] = !output[i];
                    }
                }
                break;
            case 2:
                for (int i = 0; i < output.size(); ++i) {
                    if ((i + 1) % 2 == 1) {  // 1, 3
                        output[i] = !output[i];
                    }
                }
                break;
            case 3:
                int k = 0;
                for (int i = 0; i < output.size(); ++i) {
                    if ((i + 1) == 3 * k + 1) {  // 1, 4, 7, 10
                        output[i] = !output[i];
                    }
                }
                break;
        }
        cout << "new permutation after " << pressedButton << ": ";   
        for (bool b : output) {
            cout << b << ',';
        }
        cout << endl;
        return output;
    }
};
10500