0% found this document useful (0 votes)
5 views2 pages

Dan 2

The provided code defines a function 'day2' that processes input strings representing games and calculates game IDs based on specific character conditions. It checks for color values (red, green, blue) and ensures they remain non-negative throughout the processing. The final output is the sum of valid game IDs that meet the specified criteria.

Uploaded by

insertmoney2play
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Dan 2

The provided code defines a function 'day2' that processes input strings representing games and calculates game IDs based on specific character conditions. It checks for color values (red, green, blue) and ensures they remain non-negative throughout the processing. The final output is the sum of valid game IDs that meet the specified criteria.

Uploaded by

insertmoney2play
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

void day2() {

string input1, input2, input3, input4, input5;


int newindex = 0;
int IDsumcopy = 0;
int spacecheck = 0;
bool possible = true;
getline(cin, input1);
getline(cin, input2);
getline(cin, input3);
getline(cin, input4);
getline(cin, input5);
int red = 12;
int green = 13;
int blue = 14;
int IDsum = 0;
int IDsum2 = 0;
vector <int> gameIDS = {};
vector <string> games = { input1, input2, input3, input4, input5 };
for (int game = 0; game < games.size(); game++) { // loop za 4 igre
red = 12;
green = 13;
blue = 14;
possible = true;
vector <char> currentgame = {};
for (int i = 0; i < games[game].size(); i++) { // loopuje za duzinu
svake igre
currentgame.push_back(games[game][i]);
}
for (int i = 4; currentgame[i]!= 'r'; i++) { // sabiranje ID-
ova
if (currentgame[i] == ':') {
for (int b = 0; currentgame[i - b] != 'e'; b++) {
if (isdigit(currentgame[i - b-1])) {
IDsum += (currentgame[i - b-1] - '0') * pow(10, b);
}
}
gameIDS.push_back(IDsum - IDsumcopy);
}

IDsumcopy = IDsum;
newindex = i;
}
for (newindex; newindex < currentgame.size(); newindex++) { //loop za
trenutnu igru
if (possible == false) {
cout << "Game" << gameIDS[game] << " Is impossible" << endl;
break;
}
if (currentgame[newindex - 1] == ' ' && (currentgame[newindex] == 'r'
|| currentgame[newindex] == 'g' || currentgame[newindex] == 'b')) {
for (int b = 0; spacecheck < 2; b++) {

if (currentgame[newindex - b] == ' ') {


spacecheck++;
}
if (isdigit(currentgame[newindex - b - 1])) {
if(currentgame[newindex] == 'r')
red -= (currentgame[newindex - b - 1] - '0') * pow(10,
b-1);
else if(currentgame[newindex] == 'g')
green -= (currentgame[newindex - b - 1] - '0') *
pow(10, b-1);
else if (currentgame[newindex] == 'b')
blue -= (currentgame[newindex - b - 1] - '0') * pow(10,
b-1);

spacecheck = 0;
}
if (currentgame[newindex] == ';') {
red = 12;
green = 13;
blue = 14;
}
if (red < 0 || blue < 0 || green < 0) {
possible = false;
}
}
if (possible == true){
IDsum2 += gameIDS[game];
}
}
cout << IDsum2;

You might also like