Repository navigation
Expand file tree
/
Copy path23b.cpp
More file actions
192 lines (165 loc) · 4.74 KB
/
Copy path23b.cpp
File metadata and controls
192 lines (165 loc) · 4.74 KB
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
#include <fstream>
#include <iostream>
#include <vector>
#include <assert.h>
#include <array>
typedef std::vector<std::vector<char>> Map;
enum State {
NIL = '.',
N = 'N',
S = 'S',
W = 'W',
E = 'E',
ELF = '#'
};
const std::string INPUT_FILE = "./data/23.txt";
const uint PADDING = 55 + 1;
const std::array<std::pair<int, int>, 8> NEIGHBOURS = {{
{ 1, 0},
{-1, 0},
{ 0, 1},
{ 0, -1},
{ 1, 1},
{ 1, -1},
{-1, 1},
{-1, -1},
}};
class Elves {
public:
Map m;
uint time = 0;
Elves(const Map& map) {
m = map;
}
void print() const {
std::cout << "THIS IS ROUND: " << time << std::endl;
for (const auto& row : m) {
std::cout << std::endl;
for (const auto& x : row) {
std::cout << x;
}
}
std::cout << std::endl;
}
void propose() {
for (uint y = 0; y < m.size(); ++y) {
for (uint x = 0; x < m[0].size(); ++ x) {
if (m[y][x] == NIL) continue;
assert(m[y][x] == ELF);
if (!is_anyone_close(x, y)) continue;
m[y][x] = dir(x, y);
}
}
}
void step() {
time++;
std::vector<std::pair<uint, uint>> moving;
for (uint y = 1; y < m.size() - 1; ++y) {
for (uint x = 1; x < m[0].size() - 1; ++ x) {
if (m[y][x] != NIL) continue;
moving.resize(0);
who_is_moving_here(moving, x, y);
if (moving.size() == 1) {
auto [xx, yy] = moving.front();
m[yy][xx] = NIL;
m[y][x] = ELF;
} else {
for (const auto& [xx, yy] : moving) {
m[yy][xx] = ELF;
}
}
}
}
}
uint step_until_stable() {
while (true) {
propose();
print();
if (!does_anyone_want_to_move()) break;
step();
}
++time;
return time;
}
private:
bool does_anyone_want_to_move() const {
for (const auto& row : m) {
for (const auto& el : row) {
if (el != NIL && el != ELF) return true;
}
}
return false;
}
bool is_anyone_close(uint x, uint y) {
for (const auto& [i, j] : NEIGHBOURS) {
if (m.at(y + j).at(x + i) != NIL) return true;
}
return false;
}
State dir(uint x, uint y) const {
switch (time % 4) {
case 0:
if (m[y-1][x-1] == NIL && m[y-1][x] == NIL && m[y-1][x+1] == NIL) return N;
if (m[y+1][x-1] == NIL && m[y+1][x] == NIL && m[y+1][x+1] == NIL) return S;
if (m[y-1][x-1] == NIL && m[y][x-1] == NIL && m[y+1][x-1] == NIL) return W;
if (m[y-1][x+1] == NIL && m[y][x+1] == NIL && m[y+1][x+1] == NIL) return E;
return ELF;
case 1:
if (m[y+1][x-1] == NIL && m[y+1][x] == NIL && m[y+1][x+1] == NIL) return S;
if (m[y-1][x-1] == NIL && m[y][x-1] == NIL && m[y+1][x-1] == NIL) return W;
if (m[y-1][x+1] == NIL && m[y][x+1] == NIL && m[y+1][x+1] == NIL) return E;
if (m[y-1][x-1] == NIL && m[y-1][x] == NIL && m[y-1][x+1] == NIL) return N;
return ELF;
case 2:
if (m[y-1][x-1] == NIL && m[y][x-1] == NIL && m[y+1][x-1] == NIL) return W;
if (m[y-1][x+1] == NIL && m[y][x+1] == NIL && m[y+1][x+1] == NIL) return E;
if (m[y-1][x-1] == NIL && m[y-1][x] == NIL && m[y-1][x+1] == NIL) return N;
if (m[y+1][x-1] == NIL && m[y+1][x] == NIL && m[y+1][x+1] == NIL) return S;
return ELF;
case 3:
if (m[y-1][x+1] == NIL && m[y][x+1] == NIL && m[y+1][x+1] == NIL) return E;
if (m[y-1][x-1] == NIL && m[y-1][x] == NIL && m[y-1][x+1] == NIL) return N;
if (m[y+1][x-1] == NIL && m[y+1][x] == NIL && m[y+1][x+1] == NIL) return S;
if (m[y-1][x-1] == NIL && m[y][x-1] == NIL && m[y+1][x-1] == NIL) return W;
return ELF;
}
assert(false);
}
void who_is_moving_here(std::vector<std::pair<uint, uint>>& found, uint x, uint y) {
if (m[y-1][x] == S) found.emplace_back(x, y-1);
if (m[y+1][x] == N) found.emplace_back(x, y+1);
if (m[y][x-1] == E) found.emplace_back(x-1, y);
if (m[y][x+1] == W) found.emplace_back(x+1, y);
}
};
Map read() {
std::ifstream infile(INPUT_FILE);
std::string line;
std::getline(infile, line);
const uint width = line.size();
Map map;
map.resize(PADDING);
for (uint i = 0; i < PADDING; ++i) {
map.at(i).resize(PADDING * 2 + width, NIL);
}
do {
map.push_back({});
map.back().resize(PADDING, NIL);
for (const auto& c : line) {
map.back().emplace_back(c);
}
map.back().resize(PADDING * 2 + width, NIL);
} while (std::getline(infile, line));
for (uint i = 0; i < PADDING; ++i) {
map.push_back({});
map.back().resize(PADDING * 2 + width, NIL);
}
return map;
}
int main() {
auto game = Elves(read());
game.print();
auto res = game.step_until_stable();
std::cout << "Result: " << res << std::endl;
return 0;
}