2023 day 1-3: touchup C++ cin input

This commit is contained in:
eriedaberrie 2023-12-03 16:15:40 -08:00
parent ae27321e81
commit 3c9523ab79
5 changed files with 25 additions and 47 deletions

View file

@ -20,11 +20,7 @@ int main() {
int ret = 0; int ret = 0;
string line; string line;
for (;;) { while (getline(cin, line) && !line.empty()) {
getline(cin, line);
if (cin.eof())
break;
int pi = INT_MAX, pl = -1, vi = 0, vl = 0; int pi = INT_MAX, pl = -1, vi = 0, vl = 0;
for (int i = 0; i < line.size(); i++) { for (int i = 0; i < line.size(); i++) {
unsigned char c = line[i] - '0'; unsigned char c = line[i] - '0';

View file

@ -5,28 +5,23 @@ int main() {
ios::sync_with_stdio(0); ios::sync_with_stdio(0);
cin.tie(0); cin.tie(0);
auto& cin = std::cin >> noskipws;
int ret = 0, id = 0; int ret = 0, id = 0;
bool isPossible = true; bool isPossible = true;
for (;;) { while (!cin.eof()) {
char c; switch (cin.get()) {
cin >> c;
switch (c) {
case 'G': case 'G':
cin >> c; cin >> c; cin >> c; cin >> c; cin.ignore(4);
cin >> id; cin >> id;
break; break;
case ':': case ',': case ';': case ':': case ',': case ';':
if (!isPossible) if (!isPossible)
break; break;
cin >> c; cin.ignore(1);
int n; int n;
cin >> n; cin >> n;
cin >> c; cin.ignore(1);
cin >> c; switch (cin.get()) {
switch (c) {
case 'r': case 'r':
if (n > 12) if (n > 12)
isPossible = false; isPossible = false;
@ -42,16 +37,16 @@ int main() {
} }
break; break;
case '\n': case '\n':
if (cin.eof()) case EOF:
goto end; if (isPossible) {
if (isPossible)
ret += id; ret += id;
else id = 0;
} else {
isPossible = true; isPossible = true;
}
break; break;
} }
} }
end:
cout << ret << '\n'; cout << ret << '\n';
} }

View file

@ -5,39 +5,32 @@ int main() {
ios::sync_with_stdio(0); ios::sync_with_stdio(0);
cin.tie(0); cin.tie(0);
auto& cin = std::cin >> noskipws;
int ret = 0, id = 0, r = 0, g = 0, b = 0; int ret = 0, id = 0, r = 0, g = 0, b = 0;
for (;;) { while (!cin.eof()) {
char c; switch (cin.get()) {
cin >> c;
switch (c) {
case 'G': case 'G':
cin >> c; cin >> c; cin >> c; cin >> c; cin.ignore(4);
cin >> id; cin >> id;
break; break;
case ':': case ',': case ';': case ':': case ',': case ';':
cin >> c; cin.ignore(1);
int n; int n;
cin >> n; cin >> n;
cin >> c; cin.ignore(1);
cin >> c; switch (cin.get()) {
switch (c) {
case 'r': r = max(n, r); break; case 'r': r = max(n, r); break;
case 'g': g = max(n, g); break; case 'g': g = max(n, g); break;
case 'b': b = max(n, b); break; case 'b': b = max(n, b); break;
} }
break; break;
case '\n': case '\n':
if (cin.eof()) case EOF:
goto end;
ret += r * g * b; ret += r * g * b;
r = 0; g = 0; b = 0; r = 0; g = 0; b = 0;
break; break;
} }
} }
end:
cout << ret << '\n'; cout << ret << '\n';
} }

View file

@ -10,13 +10,10 @@ int main() {
cin.tie(0); cin.tie(0);
vector<string> lines; vector<string> lines;
do { string line;
string line; while (getline(cin, line) && !line.empty()) {
getline(cin, line);
if (line.empty())
break;
lines.push_back(line); lines.push_back(line);
} while (!cin.eof()); }
int ret = 0; int ret = 0;

View file

@ -10,13 +10,10 @@ int main() {
cin.tie(0); cin.tie(0);
vector<string> lines; vector<string> lines;
do { string line;
string line; while (getline(cin, line) && !line.empty()) {
getline(cin, line);
if (line.empty())
break;
lines.push_back(line); lines.push_back(line);
} while (!cin.eof()); }
int ret = 0; int ret = 0;