-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
220 lines (184 loc) · 4.09 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <set>
#include <cassert>
#include <algorithm>
using std::string;
using std::vector;
using std::set;
vector<set<size_t>> lakes;
// Tokenise a string.
vector<string> split(const string &value, char delimiter)
{
vector<string> ret;
size_t start = 0;
while (true)
{
size_t pos = value.find(delimiter, start);
if (pos == -1)
{
ret.push_back(value.substr(start));
break;
}
else
{
ret.push_back(value.substr(start, pos - start));
start = pos + 1;
}
}
return ret;
}
// for debugging purposes
void print_lakes()
{
std::cout << "{\n";
for (auto lake : lakes)
{
std::cout << " { ";
for (auto c : lake)
{
std::cout << c << " ";
}
std::cout << " }\n";
}
std::cout << "}\n";
}
// Three forest strips at a time allow to inspect all around each cell
void process(const string &row0, const string &row1, const string &row2, size_t r0_id, size_t r1_id, size_t r2_id, size_t width)
{
r0_id *= width;
r1_id *= width;
r2_id *= width;
auto surrounding_all = [&](size_t c)
{
vector<size_t> adj;
auto surrounding_one = [&](size_t c, const string &row, size_t row_start)
{
if (!row.empty())
{
if (c > 0 && row.at(c - 1) == 'o')
adj.push_back(row_start + c - 1);
if (row.at(c) == 'o')
adj.push_back(row_start + c);
if (c < width - 1 && row.at(c + 1) == 'o')
adj.push_back(row_start + c + 1);
}
};
surrounding_one(c, row0, r0_id);
surrounding_one(c, row1, r1_id);
surrounding_one(c, row2, r2_id);
return adj;
};
for (size_t c = 0; c < width; ++c)
{
if (row1.at(c) == 'o')
{
vector<size_t> adjacent = surrounding_all(c);
vector<int> overlaps;
int index = 0;
for (const auto &lake : lakes)
{
auto is_part_of_lake = [&lake](size_t val)
{
return std::find(begin(lake), end(lake), val) != end(lake);
};
if (std::any_of(begin(adjacent), end(adjacent), is_part_of_lake))
{
overlaps.push_back(index);
}
index++;
}
if (overlaps.empty())
{
// make new lake from adjacent
set<size_t> lake;
lake.insert(begin(adjacent), end(adjacent));
lakes.push_back(lake);
}
else if (overlaps.size() == 1)
{
// append adjacent to existing lake
auto &lake = lakes.at(overlaps.at(0));
lake.insert(begin(adjacent), end(adjacent));
}
else
{
// merge lakes together, and append adjacent to the result
assert(overlaps.size() == 2);
auto &lake1 = lakes.at(overlaps.at(0));
auto &lake2 = lakes.at(overlaps.at(1));
lake1.insert(begin(lake2), end(lake2));
lake1.insert(begin(adjacent), end(adjacent));
auto itr = begin(lakes) + overlaps.at(1);
lakes.erase(itr);
}
}
}
//print_lakes();
}
// Do the test for a given input line
int test_case(const string &input)
{
lakes.clear();
vector<string> rows = split(input, '|');
auto minmax = std::minmax_element(begin(rows), end(rows), [](const string &lhs, const string &rhs)
{
return lhs.size() < rhs.size();
});
size_t min_width = minmax.first->size();
size_t max_width = minmax.second->size();
size_t width = min_width;
if (min_width != max_width)
return -1;
string row0 = "";
string row1 = rows.at(0);
string row2;
size_t r0_id = 0;
size_t r1_id = 0;
size_t r2_id = 1;
for (size_t r = 1; r <= rows.size(); ++r)
{
if (r == rows.size())
row2 = "";
else
row2 = rows.at(r);
process(row0, row1, row2, r0_id, r1_id, r2_id, width);
row0 = row1;
row1 = row2;
r0_id = r1_id;
r1_id = r2_id;
r2_id++;
}
return static_cast<int>(lakes.size());
}
int main(int argc, char* argv[])
{
if (argc > 1)
{
string filename(argv[1]);
std::ifstream fin(filename.c_str());
// read the file
if (fin.is_open())
{
string line;
while (fin.good())
{
std::getline(fin, line);
line.erase(std::remove(begin(line), end(line), ' '), end(line));
if (!line.empty())
{
int num_lakes = test_case(line);
if (-1 == num_lakes)
{
std::cout << line << "\n";
assert(false);
}
std::cout << num_lakes << "\n";
}
}
}
}
return 0;
}