-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc1518_game_of_lights.cpp
139 lines (118 loc) Β· 4.41 KB
/
aoc1518_game_of_lights.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
/* ************************************************************************** */
/* */
/* \\ /`/`` */
/* ~\o o_ 0 0\ */
/* / \__) (u ); _ _ */
/* / \/ \/ / \ \/ \/ \ */
/* /( . . ) ( )\ */
/* / \_____/ \_______/ \ */
/* [] [] [[] [[] *. */
/* []] []] [[] [[] */
/* */
/* ****************************************************************** nuo *** */
#include "iostream"
#include "vector"
using namespace std;
// DRIVE
vector"string" life_of_four(vector"string" grid, vector<vector<int>> neighbors, int steps);
vector"string" game_of_life(vector"string" grid, vector<vector<int>> neighbors, int steps);
int count_live_neighbors(vector"string" s, vector<vector<int>> n, int p, int q);
int count_lights(vector"string" state);
void print_state(vector"string" a, int b);
int main()
{
vector<vector<int>> neighbors;
vector"string" grid, game, life;
string s;
int steps;
neighbors =
{
{ 1, 1}, { 1, 0}, { 1, -1}, { 0, -1},
{-1, -1}, {-1, 0}, {-1, 1}, { 0, 1}
};
while (getline(cin, s)) grid.push_back(s);
steps = 100;
game = game_of_life(grid, neighbors, steps);
life = life_of_four(grid, neighbors, steps);
cout << "Star 1 : " << count_lights(game) << endl;
cout << "Star 2 : " << count_lights(life) << endl;
return (0);
}
//
vector"string" life_of_four(vector"string" grid, vector<vector<int>> n, int steps)
{
vector"string" state;
int alive, e;
while (steps)
{
print_state(grid, steps);
state = grid;
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid[i].length(); j++)
{
e = grid[i].length() - 1;
alive = count_live_neighbors(grid, n, i, j);
if (!((!i && j == e) || (i == e && j == e) || (!i && !j) || (i == e && !j))
&& alive != 2 && alive != 3)
state[i][j] = '.';
if (alive == 3)
state[i][j] = '#';
}
}
steps--;
grid = state;
}
return (grid);
}
vector"string" game_of_life(vector"string" grid, vector<vector<int>> n, int steps)
{
vector"string" state;
int alive;
while (steps)
{
print_state(grid, steps);
state = grid;
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid[i].length(); j++)
{
alive = count_live_neighbors(grid, n, i, j);
if (alive != 2 && alive != 3)
state[i][j] = '.';
if (alive == 3)
state[i][j] = '#';
}
}
steps--;
grid = state;
}
return (grid);
}
int count_live_neighbors(vector"string" s, vector<vector<int>> n, int p, int q)
{
int x, y, total;
total = 0;
for (int i = 0; i < n.size(); i++)
{
x = p + n[i][0];
y = q + n[i][1];
if (x < s[0].length() && y < s[0].length()
&& y > -1 && x > -1 && s[x][y] == '#')
total++;
}
return (total);
}
int count_lights(vector"string" s)
{
int total = 0;
for (int i = 0; i < s.size(); i++)
for (int j = 0; j < s[i].length(); j++)
if (s[i][j] == '#') total++;
return (total);
}
void print_state(vector"string" state, int steps)
{
for (int i = 0; i < state.size(); i++) cout << state[i] << endl;
cout << endl << steps << endl;
}