-
Notifications
You must be signed in to change notification settings - Fork 13
/
custom-game-of-life.cpp
47 lines (40 loc) · 1.42 KB
/
custom-game-of-life.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
#include <bits/stdc++.h>
const auto print = [](auto&& toPrint){
std::for_each( std::begin(toPrint), std::end(toPrint), [](const auto& line) {
std::cout << line << "\n";
});
};
int main()
{
int h, w, n;
std::cin >> h >> w >> n; std::cin.ignore();
std::map<char, std::string> states;
getline(std::cin, states['O']);
getline(std::cin, states['.']);
std::istream_iterator<std::string> it(std::cin), eos;
std::vector<std::string> v (it, eos);
while(n--) {
auto tmp { v };
auto curH{ 0 };
std::for_each(std::begin(v), std::end(v), [&](auto&& line) {
auto curW{ 0 };
std::for_each( std::begin(line), std::end(line), [&](auto&& cell) {
auto neighbours = std::invoke([&]() {
auto res{ 0 };
for(auto i = -1; i <= 1; ++i)
for(auto j = -1; j <= 1; ++j) {
auto tmpH{ curH + i }, tmpW{ curW + j };
if( (i || j) && (tmpH >= 0) && (tmpH < h) && (tmpW >= 0) && (tmpW < w) )
res += v[tmpH][tmpW] == 'O';
}
return res;
});
tmp[curH][curW] = ( states[cell][neighbours] == '1' ) ? 'O' : '.';
++curW;
});
++curH;
});
v = std::move(tmp);
}
print(v);
}