Skip to content

Instantly share code, notes, and snippets.

@seanc
Created September 13, 2019 20:24
Show Gist options
  • Select an option

  • Save seanc/bddd2bd85c5655b0b38de540195f18a2 to your computer and use it in GitHub Desktop.

Select an option

Save seanc/bddd2bd85c5655b0b38de540195f18a2 to your computer and use it in GitHub Desktop.

Revisions

  1. seanc created this gist Sep 13, 2019.
    67 changes: 67 additions & 0 deletions main.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    #include <vector>
    #include <iostream>
    #include <cstdlib>
    #include <string>

    using namespace std;

    void printRow (string title, vector<string> row) {
    string rowString = "";
    for (const auto &piece : row) rowString += piece + " ";

    cout << title << ": " << endl << rowString << endl;
    }

    string randomColor () {
    const char* colors[] = {"R", "G", "B", "O", "P"};
    string color = colors[rand() % 5];

    return color;
    }

    vector<string> parseColorRow (vector<string> row) {
    int index = 0;
    int occurences = 0;
    int firstOccurenceIndex = 0;
    string lastColor;

    while (index < row.size())
    {
    string color = row[index];
    int nextColorIndex = index + 1;

    if (nextColorIndex < row.size()) {
    if (color == row[nextColorIndex] || color == lastColor) {
    occurences++;
    lastColor = row[index];

    if (occurences == 1) {
    firstOccurenceIndex = index;
    }

    if (occurences >= 3) {
    for (int j = firstOccurenceIndex; j <= index; j++) {
    row[j] = randomColor();
    }

    index = firstOccurenceIndex;
    occurences = 0;
    }
    } else {
    occurences = 0;
    }
    }

    index++;
    }

    return row;
    }

    int main () {
    vector<string> row{"R", "R", "R", "G", "B", "R", "R", "R", "P", "O", "O", "O", "O", "O", "G", "G", "G", "A", "B", "B", "B", "G", "G", "B", "B", "B"};
    vector<string> parsedRow = parseColorRow(row);

    printRow("Original Row", row);
    printRow("Parsed Row", parsedRow);
    }