-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc.hpp
297 lines (257 loc) · 6.24 KB
/
aoc.hpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#pragma once
#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
namespace aoc {
template <typename T>
bool in_range(const T& val, const T& low_incl, const T& high_excl)
{
return val >= low_incl && val < high_excl;
}
template <size_t SIZE>
struct Vec
{
union
{
struct
{
int x, y, z, w;
};
struct
{
int elements[SIZE];
};
};
Vec()
{
}
Vec(int x_, int y_) : x(x_), y(y_)
{
}
Vec(int x_, int y_, int z_) : x(x_), y(y_), z(z_)
{
}
Vec(int x_, int y_, int z_, int w_) : x(x_), y(y_), z(z_), w(w_)
{
}
Vec(const Vec<SIZE>& other) = default;
int operator()(size_t i) const
{
assert(i < SIZE);
return elements[i];
}
int& operator()(size_t i)
{
assert(i < SIZE);
return elements[i];
}
Vec<SIZE> operator+(const Vec<SIZE>& other) const
{
Vec<SIZE> res;
for (size_t i{0}; i < SIZE; i++)
res(i) = (*this)(i) + other(i);
return res;
}
void operator+=(const Vec<SIZE>& other)
{
for (size_t i{0}; i < SIZE; i++)
(*this)(i) += other(i);
}
Vec<SIZE> operator-(const Vec<SIZE>& other) const
{
Vec<SIZE> res;
for (size_t i{0}; i < SIZE; i++)
res(i) = (*this)(i)-other(i);
return res;
}
Vec<SIZE> operator*(const Vec<SIZE>& other) const
{
Vec<SIZE> res;
for (size_t i{0}; i < SIZE; i++)
res(i) = (*this)(i)*other(i);
return res;
}
Vec<SIZE> operator/(const Vec<SIZE>& other) const
{
Vec<SIZE> res;
for (size_t i{0}; i < SIZE; i++)
res(i) = (*this)(i) / other(i);
return res;
}
bool operator<(const Vec<SIZE>& other) const
{
for (size_t i{0}; i < SIZE; i++)
{
if ((*this)(i) < other(i))
return true;
if ((*this)(i) > other(i))
return false;
}
return false;
}
bool operator==(const Vec<SIZE>& other) const
{
for (size_t i{0}; i < SIZE; i++)
if (other(i) != (*this)(i))
return false;
return true;
}
bool operator<=(const Vec<SIZE>& other) const
{
return *this < other || *this == other;
}
bool operator!=(const Vec<SIZE>& other) const
{
return !(*this == other);
}
bool operator>(const Vec<SIZE>& other) const
{
return !(*this <= other);
}
bool operator>=(const Vec<SIZE>& other) const
{
return *this == other || *this > other;
}
};
template <size_t SIZE>
bool in_range(const Vec<SIZE>& val, const Vec<SIZE>& low_incl, const Vec<SIZE>& high_excl)
{
for (size_t i{0}; i < SIZE; i++)
if (!in_range(val(i), low_incl(i), high_excl(i)))
return false;
return true;
}
template <size_t SIZE>
std::ostream& operator<<(std::ostream& out, const Vec<SIZE>& vec)
{
out << '[';
for (size_t i{0}; i < SIZE; i++)
{
out << vec(i);
if (i < SIZE - 1)
out << ", ";
}
out << ']';
return out;
}
using Vec2 = Vec<2>;
using Vec3 = Vec<3>;
using Vec4 = Vec<4>;
const std::array<Vec2, 8> directions_diag_2{{
{0, -1},
{0, 1},
{1, 0},
{-1, 0},
{1, 1},
{-1, -1},
{1, -1},
{-1, 1},
}};
const std::array<Vec2, 8> directions_2{{
{0, -1},
{0, 1},
{1, 0},
{-1, 0},
{1, 1},
{-1, -1},
{1, -1},
{-1, 1},
}};
inline std::vector<Vec2> neighbours(const Vec2& pos, bool diagonal = false)
{
std::vector<Vec2> result;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
if (in_range(abs(i) + abs(j), 1, 2 + diagonal))
result.emplace_back(pos.x + i, pos.y + j);
return result;
}
inline std::vector<Vec3> neighbours(const Vec3& pos, bool diagonal = false)
{
std::vector<Vec3> result;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
for (int k = -1; k < 2; k++)
if (in_range(abs(i) + abs(j) + abs(k), 1, 2 + diagonal * 2))
result.emplace_back(pos.x + i, pos.y + j, pos.z + k);
return result;
}
inline std::vector<Vec4> neighbours(const Vec4& pos, bool diagonal = false)
{
assert(diagonal);
std::vector<Vec4> result;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
for (int k = -1; k < 2; k++)
for (int l = -1; l < 2; l++)
if (i != 0 || j != 0 || k != 0 || l != 0)
result.emplace_back(pos.x + i, pos.y + j, pos.z + k, pos.w + l);
return result;
}
inline std::vector<std::string> split(const std::string& str, const std::string& delim)
{
std::vector<std::string> result;
size_t start = 0, pos = 0;
while ((pos = str.find(delim, start)) != std::string::npos)
{
result.push_back(str.substr(start, pos - start));
start = pos + delim.length();
}
result.push_back(str.substr(start, str.length() - start));
return result;
}
inline std::string read_file(std::ifstream& file)
{
assert(file.good());
std::stringstream buf;
buf << file.rdbuf();
return buf.str();
}
} // namespace aoc
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& vec)
{
out << '[';
const auto size = vec.size();
for (size_t i{0}; i < size; i++)
{
out << vec[i];
if (i < size - 1)
out << ", ";
}
out << ']';
return out;
}
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u8 = uint8_t;
template <size_t SIZE>
aoc::Vec<SIZE> operator*(const aoc::Vec<SIZE>& vec, int scalar)
{
aoc::Vec<SIZE> res;
for (size_t i{0}; i < SIZE; i++)
res(i) = vec(i) * scalar;
return res;
}
template <size_t SIZE>
aoc::Vec<SIZE> operator*(int scalar, const aoc::Vec<SIZE>& vec)
{
return vec * scalar;
}
namespace std {
template <size_t SIZE>
struct hash<aoc::Vec<SIZE>>
{
size_t operator()(const aoc::Vec<SIZE>& vec) const
{
size_t res = 0;
for (size_t i{0}; i < SIZE; i++)
res ^= hash<int>()(vec(i));
return res;
}
};
} // namespace std