-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_23b.cpp
208 lines (186 loc) · 6.29 KB
/
day_23b.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
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
struct Point {
Point (const int row = 0, const int col = 0) : row(row), col(col) {}
int row;
int col;
bool operator == (const Point& p) const {
return p.row == row && p.col == col;
}
friend std::ostream& operator<<(std::ostream& os, const Point& p);
};
struct PointHash {
std::size_t operator () (const Point& p) const {
return p.row * p.col;
}
};
std::ostream& operator<<(std::ostream& os, const Point& p) {
os << '(' << p.row << ' ' << p.col << ')';
return os;
}
struct Elf {
Point current;
Point next;
bool need_to_move = false;
bool to_move = false;
};
std::array<std::array<Point, 3>, 4> move_check_order {{
{{ Point(-1, 0), Point(-1, -1), Point(-1, 1) }}, // North
{{ Point(1, 0), Point(1, -1), Point(1, 1) }}, // South
{{ Point(0, -1), Point(-1, -1), Point(1, -1) }}, // West
{{ Point(0, 1), Point(-1, 1), Point(1, 1) }} // East
}};
std::array<Point, 4> moves {{
Point(-1, 0),
Point(1, 0),
Point(0, -1),
Point(0, 1)
}};
std::array<Point, 8> surroundings {{
Point(-1, 0),
Point(1, 0),
Point(0, -1),
Point(0, 1),
Point(-1, -1),
Point(1, 1),
Point(1, -1),
Point(-1, 1)
}};
int main(int argc, char * argv[]) {
std::string input = "../input/day_23_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<Elf> elves;
int row = 0;
while(std::getline(file, line)) {
int col = 0;
for (int col = 0; col < line.size(); col++) {
if (line[col] == '#') {
Elf elf;
elf.current = Point(row, col);
elf.to_move = false;
elves.push_back(elf);
}
}
row++;
}
// std::cout << elves.size() << '\n';
// exit(0);
int move_check_index = 0;
int round = 1;
while (true) {
// 1st half
std::unordered_set<Point, PointHash> current_positions;
std::unordered_map<Point, int, PointHash> next_positions;
// Update current positions
for (const auto& elf : elves) {
current_positions.insert(elf.current);
}
// Check whether each elf needs to move
for (auto& elf : elves) {
elf.need_to_move = false;
for (const auto& surrounding : surroundings) {
if(current_positions.find(Point(elf.current.row + surrounding.row, elf.current.col + surrounding.col)) != current_positions.end()) {
elf.need_to_move = true;
}
}
}
// Check whether any elf needs to move
if (std::all_of(std::begin(elves), std::end(elves), [](const auto elf) {return !elf.need_to_move; })) {
std::cout << round << '\n';
return 0;
}
// std::cout << "move_check_index: " << move_check_index << '\n';
for (auto& elf : elves) {
if (!elf.need_to_move) {
// std::cout << "Proposes not moving as does not need to move" << '\n';
continue;
}
bool possible = true;
for (int i = 0; i < move_check_order.size(); i++) {
possible = true;
for (const auto& delta : move_check_order[(move_check_index + i) % 4]) {
const auto new_point = Point(elf.current.row + delta.row, elf.current.col + delta.col);
if (current_positions.find(new_point) != current_positions.end()) {
possible = false;
break;
}
}
if (possible) {
const auto selected_move = moves[(move_check_index + i) % 4];
elf.next = Point(elf.current.row + selected_move.row, elf.current.col + selected_move.col);
elf.to_move = true;
// std::cout << "Proposes moving " << (move_check_index + i) % 4 << '\n';
// Avoid default init for sanity
if (next_positions.find(elf.next) == next_positions.end()) {
next_positions[elf.next] = 0;
}
next_positions[elf.next]++;
break;
}
}
if (!possible) {
// std::cout << "Proposes not moving as cannot move " << '\n';
elf.to_move = false;
}
}
move_check_index++;
move_check_index %= 4;
// 2nd half
for (auto& elf : elves) {
if (elf.to_move && next_positions.find(elf.next)->second == 1) {
// std::cout << elf.current << " ---> " << elf.next << '\n';
elf.current = elf.next;
}
elf.to_move = false;
}
// Debug
std::array<int, 4> rectangle = {{
std::numeric_limits<int>::max(),
std::numeric_limits<int>::min(),
std::numeric_limits<int>::max(),
std::numeric_limits<int>::min()
}};
for (const auto& elf : elves) {
rectangle[0] = std::min(rectangle[0], elf.current.row);
rectangle[1] = std::max(rectangle[1], elf.current.row);
rectangle[2] = std::min(rectangle[2], elf.current.col);
rectangle[3] = std::max(rectangle[3], elf.current.col);
}
auto grid = std::vector<std::vector<char>>();
for (int row = rectangle[0]; row <= rectangle[1]; row++) {
// std::cout << row << '\n';
grid.emplace_back();
for (int col = rectangle[2]; col <= rectangle[3]; col++) {
// std::cout << col << '\n';
grid[row - rectangle[0]].emplace_back('.');
}
}
for( const auto& elf : elves) {
grid[elf.current.row - rectangle[0]][elf.current.col - rectangle[2]] = '#';
}
int c = 0;
for (int row = rectangle[0]; row <= rectangle[1]; row++) {
for (int col = rectangle[2]; col <= rectangle[3]; col++) {
// std::cout << grid[row - rectangle[0]][col - rectangle[2]];
if (grid[row - rectangle[0]][col - rectangle[2]] == '.') c++;
}
// std::cout<< '\n';
}
// std::cout<< '\n';
// std::cout << grid.size() << " x " << grid[0].size() << '\n';
// std::cout << c << '\n';
round++;
}
return 0;
}