blob: b725eddea95c6f4b325218b49c053fafcf98f89b (
plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <iostream>
#include <string>
#include <vector>
int consumeInt(std::string& s)
{
int n = 0;
int i;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = n * 10 + s[i] - '0';
s = s.substr(i);
return n;
}
// 5762 too high
// 5659
// 5632 too low
int main()
{
std::string left, right;
std::vector<int> correct;
int index = 1;
while (1) {
std::getline(std::cin, left);
if (std::cin.eof())
break;
std::getline(std::cin, right);
if (std::cin.eof())
break;
int lt = 0, rt = 0;
std::cout << left << std::endl;
std::cout << right << std::endl;
while (left.size() != 0 && right.size() != 0) {
if (left[0] == ']') {
if (right[0] != ']') {
std::cout << "Left side ran out of items" << std::endl;
correct.push_back(index);
break;
}
if (rt) --rt;
else right = right.substr(1);
left = left.substr(1);
} else if (right[0] == ']') {
if (left[0] != ']') {
std::cout << "Right side ran out of items" << std::endl;
break; // incorrect
}
if (lt) --lt;
else left = left.substr(1);
right = right.substr(1);
} else if (left[0] == '[') {
std::cout << "Left side opens" << std::endl;
if (right[0] != '[')
++rt;
else
right = right.substr(1);
left = left.substr(1);
} else if (right[0] == '[') {
std::cout << "Right side opens" << std::endl;
if (left[0] != '[')
++lt;
else
left = left.substr(1);
right = right.substr(1);
} else {
int ln = consumeInt(left);
int rn = consumeInt(right);
std::cout << "Compare " << ln << " to " << rn << std::endl;
if (ln < rn) {
correct.push_back(index);
break;
} else if (ln > rn) {
break;
}
int fail = 0;
while (rt) {
if (left[0] != ']') {
fail = 1;
std::cout << "Right ends early" << std::endl;
break;
}
left = left.substr(1);
--rt;
}
while (lt) {
if (right[0] != ']') {
fail = 1;
std::cout << "Left ends early" << std::endl;
correct.push_back(index);
break;
}
right = right.substr(1);
--lt;
}
if (fail) break;
}
bool lcomma = left[0] == ',';
if (lcomma) {
left = left.substr(1);
if (right[0] != ',') {
std::cout << "Right ends early" << std::endl;
break;
}
}
if (right[0] == ',') {
right = right.substr(1);
if (!lcomma) {
std::cout << "Left ends early" << std::endl;
correct.push_back(index);
break;
}
}
}
if (left.size() == 0 && right.size() == 0)
correct.push_back(index);
if (correct.size() > 0 && correct.back() == index)
std::cout << " correct" << std::endl;
else
std::cout << std::endl;
index++;
std::getline(std::cin, left);
if (std::cin.eof())
break;
}
int sum = 0;
for (auto& w : correct)
sum += w;
std::cout << "Total corrects: " << sum << std::endl;
}
|