Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <map>
- #include <cctype>
- #include <algorithm>
- #include <vector>
- using namespace std;
- int concatenate(int firstDigit, int lastDigit)
- {
- return 10 * firstDigit + lastDigit; // Directly form the two-digit number
- }
- int main()
- {
- ifstream inFile("D2-input.txt");
- if (!inFile)
- {
- cout << "Error opening file" << endl;
- return -1;
- }
- string line;
- int runningTotal = 0;
- while (getline(inFile, line))
- {
- int redCubes = 0;
- int greenCubes = 0;
- int blueCubes = 0;
- bool validGame = true;
- size_t pos1 = line.find("Game ") + 5;
- size_t pos2 = line.find(":");
- string gameID = line.substr(pos1, pos2 - pos1);
- int gameNumber = stoi(gameID);
- std::cout << "Game ID : " << gameNumber << std::endl;
- std::replace(line.begin(), line.end(), ';', ',');
- std::replace(line.begin(), line.end(), ':', ',');
- std::cout << line << std::endl;
- vector<string> cubes;
- std::string unit;
- istringstream ss(line);
- while (getline(ss, unit, ','))
- {
- cubes.push_back(unit);
- }
- for (int i = 0; i < cubes.size(); i++)
- {
- // cout << cubes[i] << endl;
- istringstream cs(cubes[i]);
- int count;
- string color;
- cs >> count >> color;
- cout << "Count: " << count << ", Color: " << color << endl;
- // std::cout << "Red cubes: " << count << std::endl;
- if (color == "red")
- {
- redCubes += count;
- }
- else if (color == "green")
- {
- greenCubes += count;
- }
- else if (color == "blue")
- {
- blueCubes += count;
- }
- if (blueCubes > 14)
- {
- validGame = false;
- cout << "Invalid due to blue cubes: " << blueCubes << endl;
- }
- else if (greenCubes > 13)
- {
- validGame = false;
- cout << "Invalid due to green cubes: " << greenCubes << endl;
- }
- else if (redCubes > 12)
- {
- validGame = false;
- cout << "Invalid due to red cubes: " << redCubes << endl;
- }
- }
- cout << "Red: " << redCubes << " Green: " << greenCubes << " Blue: " << blueCubes << endl;
- if (validGame)
- {
- cout << "Game is valid" << endl
- << endl
- << endl;
- runningTotal = runningTotal + gameNumber;
- }
- else
- {
- cout << "Game is invalid" << endl
- << endl
- << endl;
- }
- cout << "Running Total : " << runningTotal << endl;
- }
- inFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment