-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc1607_abba.cpp
165 lines (153 loc) Β· 5.46 KB
/
aoc1607_abba.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* ************************************************************************** */
/* */
/* \\ /`/`` */
/* ~\o o_ 0 0\ */
/* / \__) (u ); _ _ */
/* / \/ \/ / \ \/ \/ \ */
/* /( . . ) ( )\ */
/* / \_____/ \_______/ \ */
/* [] [] [[] [[] *. */
/* []] []] [[] [[] */
/* */
/* ****************************************************************** nuo *** */
#include "iostream"
#include "sstream"
#include "vector"
// DRIVE
int subset_abba_checker(std::string s, int L, int R);
int subset_xxxx_checker(std::string s, int L, int R);
int slen(std::string s);
int main()
{
int count_TLS;
int count_SSL;
std::string s;
count_SSL = count_TLS = 0;
while (std::cin >> s)
{
std::vector<std::string> arr_outer;
std::vector<std::string> arr_inner;
int should_have_none;
int should_have_some;
int has_SSL;
int i, j, x;
i = j = 0;
should_have_none = 0;
should_have_some = 0;
while (s[i])
{
j = i + 1;
while (s[j])
{
if (!s[j + 1])
{
if (subset_xxxx_checker(s, i, j)) should_have_none++;
if (subset_abba_checker(s, i, j)) should_have_some++;
x = i;
while (x + 1 < j)
{
if (s[x] == s[x + 2])
{
std::string temp;
temp += s[x];
temp += s[x + 1];
temp += s[x];
arr_outer.push_back(temp);
}
x++;
}
i = j;
break;
}
if (s[j + 1] == '[')
{
if (subset_xxxx_checker(s, i, j)) should_have_none++;
if (subset_abba_checker(s, i, j)) should_have_some++;
x = i;
while (x + 1 < j)
{
if (s[x] == s[x + 2])
{
std::string temp;
temp += s[x];
temp += s[x + 1];
temp += s[x];
arr_outer.push_back(temp);
}
x++;
}
i = j + 2;
}
if (s[j + 1] == ']')
{
if (subset_abba_checker(s, i, j)) should_have_none++;
x = i;
while (x + 1 < j)
{
if (s[x] == s[x + 2])
{
std::string temp;
temp += s[x + 1];
temp += s[x];
temp += s[x + 1];
arr_inner.push_back(temp);
}
x++;
}
i = j + 2;
}
j++;
}
i++;
}
has_SSL = i = 0;
while (i < (int) arr_outer.size() && !has_SSL)
{
j = 0;
while (j < (int) arr_inner.size() && !has_SSL)
{
if (arr_outer[i] == arr_inner[j])
{
has_SSL++;
break;
}
j++;
}
i++;
}
if (!should_have_none && should_have_some) count_TLS++;
if (has_SSL) count_SSL++;
}
std::cout << "Star 1 : " << count_TLS << std::endl;
std::cout << "Star 2 : " << count_SSL << std::endl;
return (0);
}
//
int subset_xxxx_checker(std::string s, int L, int R)
{
if (R - L < 3) return (0);
while (L < R - 2)
{
if (s[L] == s[L + 1] && s[L] == s[L + 2] && s[L] == s[L + 3])
return (1);
L++;
}
return (0);
}
int subset_abba_checker(std::string s, int L, int R)
{
if (R - L < 3) return (0);
while (L < R - 2)
{
if (s[L] == s[L + 3] && s[L + 1] == s[L + 2])
return (1);
L++;
}
return (0);
}
int slen(std::string s)
{
int i = 0;
while (s[i]) i++;
return (i);
}