-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 2.cpp
124 lines (114 loc) · 3.17 KB
/
Day 2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <istream>
#include <fstream>
#include <ostream>
#include <string>
#define cin infile
#define cout outfile
using namespace std;
int getColorVariantConstraint(string variant)
{
if (((string) "red").compare(variant) == 0)
return 12;
if (((string) "green").compare(variant) == 0)
return 13;
if (((string) "blue").compare(variant) == 0)
return 14;
return 0;
}
void computeMaxForVariant(int &r, int &g, int &b, string variant, int newValue)
{
if (((string) "red").compare(variant) == 0)
r = max(r, newValue);
if (((string) "green").compare(variant) == 0)
g = max(g, newValue);
if (((string) "blue").compare(variant) == 0)
b = max(b, newValue);
}
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
bool partTwo = true;
int res = 0;
string inputLine;
int gameId = 1;
while (getline(cin, inputLine))
{
string colorVariant = "";
int number = 0;
bool passedSemi = false;
bool numberMode = false;
bool isValid = true;
int maxR = 0, maxG = 0, maxB = 0;
for (int it = 0; it < inputLine.size(); it++)
{
if (inputLine[it] == ':')
{
passedSemi = true;
continue;
}
if (!passedSemi)
{
continue;
}
if (!numberMode && inputLine[it] == ' ')
{ // empty before number
numberMode = true;
colorVariant = "";
number = 0;
continue;
}
if (!numberMode && (inputLine[it] == ',' || inputLine[it] == ';')) // end of definition
{
if (partTwo)
computeMaxForVariant(maxR, maxG, maxB, colorVariant, number);
else
{
int cstr = getColorVariantConstraint(colorVariant);
if (number > cstr)
{
isValid = false;
break;
}
}
}
if (!numberMode)
{
colorVariant.push_back(inputLine[it]);
}
if (numberMode)
{
if (inputLine[it] >= '0' && inputLine[it] <= '9')
{
number *= 10;
number += inputLine[it] - '0';
}
if (inputLine[it] == ' ')
{
numberMode = false;
}
}
}
if (isValid && !numberMode) // last case
{
if (partTwo)
computeMaxForVariant(maxR, maxG, maxB, colorVariant, number);
else
{
int cstr = getColorVariantConstraint(colorVariant);
if (number > cstr)
{
isValid = false;
}
}
}
if (!partTwo && isValid)
res += gameId;
else if (partTwo)
res += maxR * maxG * maxB;
gameId++;
}
cout << res;
return 0;
}