Guest User

Untitled

a guest
Sep 17th, 2024
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <map>
  5. #include <cctype>
  6. #include <algorithm>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. int concatenate(int firstDigit, int lastDigit)
  12. {
  13. return 10 * firstDigit + lastDigit; // Directly form the two-digit number
  14. }
  15.  
  16. int main()
  17. {
  18. ifstream inFile("D2-input.txt");
  19. if (!inFile)
  20. {
  21. cout << "Error opening file" << endl;
  22. return -1;
  23. }
  24.  
  25. string line;
  26. int runningTotal = 0;
  27.  
  28. while (getline(inFile, line))
  29. {
  30. int redCubes = 0;
  31. int greenCubes = 0;
  32. int blueCubes = 0;
  33. bool validGame = true;
  34.  
  35. size_t pos1 = line.find("Game ") + 5;
  36. size_t pos2 = line.find(":");
  37. string gameID = line.substr(pos1, pos2 - pos1);
  38. int gameNumber = stoi(gameID);
  39.  
  40. std::cout << "Game ID : " << gameNumber << std::endl;
  41.  
  42. std::replace(line.begin(), line.end(), ';', ',');
  43. std::replace(line.begin(), line.end(), ':', ',');
  44. std::cout << line << std::endl;
  45.  
  46. vector<string> cubes;
  47. std::string unit;
  48.  
  49. istringstream ss(line);
  50.  
  51. while (getline(ss, unit, ','))
  52. {
  53. cubes.push_back(unit);
  54. }
  55.  
  56. for (int i = 0; i < cubes.size(); i++)
  57. {
  58. // cout << cubes[i] << endl;
  59.  
  60. istringstream cs(cubes[i]);
  61. int count;
  62. string color;
  63.  
  64. cs >> count >> color;
  65. cout << "Count: " << count << ", Color: " << color << endl;
  66. // std::cout << "Red cubes: " << count << std::endl;
  67.  
  68. if (color == "red")
  69. {
  70. redCubes += count;
  71. }
  72. else if (color == "green")
  73. {
  74. greenCubes += count;
  75. }
  76. else if (color == "blue")
  77. {
  78. blueCubes += count;
  79. }
  80.  
  81. if (blueCubes > 14)
  82. {
  83. validGame = false;
  84. cout << "Invalid due to blue cubes: " << blueCubes << endl;
  85. }
  86. else if (greenCubes > 13)
  87. {
  88. validGame = false;
  89. cout << "Invalid due to green cubes: " << greenCubes << endl;
  90. }
  91. else if (redCubes > 12)
  92. {
  93. validGame = false;
  94. cout << "Invalid due to red cubes: " << redCubes << endl;
  95. }
  96. }
  97.  
  98. cout << "Red: " << redCubes << " Green: " << greenCubes << " Blue: " << blueCubes << endl;
  99.  
  100. if (validGame)
  101. {
  102. cout << "Game is valid" << endl
  103. << endl
  104. << endl;
  105. runningTotal = runningTotal + gameNumber;
  106. }
  107. else
  108. {
  109. cout << "Game is invalid" << endl
  110. << endl
  111. << endl;
  112. }
  113.  
  114. cout << "Running Total : " << runningTotal << endl;
  115. }
  116.  
  117. inFile.close();
  118. return 0;
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment