-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay8part2.cpp
More file actions
224 lines (209 loc) · 7.91 KB
/
Copy pathDay8part2.cpp
File metadata and controls
224 lines (209 loc) · 7.91 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
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
#include <iostream>
#include <string>
#include <vector>
#include "StringSplit.h"
const int rows = 50; // 12 for test map, 50 for real input
const int cols = 50; // 12 for test map, 50 for real input
char map[rows][cols];
struct Point {
public:
Point(int r_, int c_)
: r(r_), c(c_) {};
int r = -1;
int c = -1;
};
struct Signal {
public:
Signal(char feature_, int r1_, int c1_)
: feature(feature_), r1(r1_), c1(c1_) {};
Signal(char feature_, int r1_, int c1_, int r2_, int c2_)
: feature(feature_), r1(r1_), c1(c1_), r2(r2_), c2(c2_)
{
// Antennas are also antinodes
antinodes.push_back(Point(r1, c1));
antinodes.push_back(Point(r2, c2));
int diffr = r2 - r1;
int diffc = c2 - c1;
// Direction: up-left until outside
int factor = 1;
bool outside = false;
do {
int r = r1 - factor * diffr;
int c = c1 - factor * diffc;
if (r >= 0 && r < rows && c >= 0 && c < cols) {
antinodes.push_back(Point(r, c));
} else {
outside = true;
}
factor++;
} while (!outside);
// Direction: down-right until outside
factor = 1;
outside = false;
do {
int r = r2 + factor * diffr;
int c = c2 + factor * diffc;
if (r >= 0 && r < rows && c >= 0 && c < cols) {
antinodes.push_back(Point(r, c));
} else {
outside = true;
}
factor++;
} while (!outside);
};
char feature = '.';
int r1 = -1;
int c1 = -1;
int r2 = -1;
int c2 = -1;
std::vector<Point> antinodes;
};
// https://tomeko.net/online_tools/cpp_text_escape.php?lang=en
std::vector<std::string> test_map_input{
"............",
"........0...",
".....0......",
".......0....",
"....0.......",
"......A.....",
"............",
"............",
"........A...",
".........A..",
"............",
"............"};
std::vector<std::string> map_input{
"...........6.b....................................",
"........6................8........................",
"..Y.......................................o.......",
"....V...j............B.............c..............",
"............8.........X.......L...................",
".....j..v6.......3.L..................c...........",
"..Mj.....p3.......b........Z....................J.",
"..........M...X...................................",
"V..............v......p.........Z.........c.......",
"..............3...................................",
".......V......U3.............c....................",
"..........b..v.M.U8...............................",
"..........j........8.....................J........",
"..........Y......q........LH..Z...D...........y...",
"..2Y........PX......6..................BQ.........",
"...0.Y...............XP...........w...............",
".........U.......2...............oH.y.............",
"0..............9........U.........................",
"...........P..............W.......z...Oy..........",
"...................t...p.W..o.............Q.......",
".....S.................t.....Q....B...............",
"S.k..................V..W...p.......H...O......m..",
"....S.h................W.......................O..",
"..h..P.2.............Z.............J..............",
".........k.......5v.......q...t.s.................",
".....Q.....h..........................J...B.......",
"........0.........l...............................",
".S................................................",
".............................M....................",
"2..................e.....o.....y..................",
"................k.................................",
"......4......k....t...s.q.........................",
".4.......................q........................",
".......................z....E.....................",
".............0.....d..............................",
"7..........D........z.............................",
".......D..5......7..9.............................",
"......5..................E........................",
"D..............K......d..9E..........w.....1..C...",
".......K..x.........d....s...........l............",
"........7......................u...C..............",
"..K........x..............9..C...u................",
"4..............s.........................l...T..w.",
".......5.....7..................m......T......1...",
"...........................E...z.m................",
"......................................u...C.......",
".............................em...................",
"..............................................T...",
"....................x.......................e.....",
".............................1e....w....l........."
};
int main(int argc, char* argv[])
{
// Load input
for (int row = 0; row < rows; row++) {
//auto map_row = test_map_input[row];
auto map_row = map_input[row];
for (int col = 0; col < cols; col++) {
//if (map_row[col] == 'j')
map[row][col] = map_row[col];
//else
// map[row][col] = '.';
std::cout << map[row][col];
}
std::cout << std::endl;
}
// Create map features, which are map points other than '.'
std::vector<Signal> features;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (map[row][col] != '.') {
features.push_back(Signal(map[row][col], row, col));
}
}
}
std::cout << "Map features: " << features.size() << std::endl;
// Match map features and create signals
std::vector<Signal> signals;
for (int f = 0; f < features.size() - 1; f++) {
auto feature = features[f];
//std::cout << feature.feature << " " << feature.c1 << " " << feature.r1 << std::endl;
// Search through rest of map features to find matches
for (int f2 = f + 1; f2 < features.size(); f2++) {
auto feature2 = features[f2];
if (feature.feature == feature2.feature) {
signals.push_back(Signal(feature.feature,
feature.r1, feature.c1,
feature2.r1, feature2.c1));
//std::cout << feature.feature << " " << feature.r1 << "," << feature.c1 << " + " << feature2.r1 << "," << feature2.c1 << std::endl;
}
}
}
std::cout << "Signals: " << signals.size() << std::endl;
// Viasualize map with antinodes on top
char visual[rows][cols];
memset(visual, '.', sizeof(visual));
// Plot features
for (auto signal : signals) {
visual[signal.r1][signal.c1] = signal.feature;
visual[signal.r2][signal.c2] = signal.feature;
}
// Plot antinodes
for (auto signal : signals) {
for (auto antinode : signal.antinodes) {
visual[antinode.r][antinode.c] = '#';
}
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
std::cout << visual[row][col];
}
std::cout << std::endl;
}
// Antinode heat map
char antinodes[rows][cols];
memset(antinodes, 0, sizeof(antinodes));
for (auto signal : signals) {
for (auto antinode : signal.antinodes) {
antinodes[antinode.r][antinode.c]++;
}
}
// Count unique locations
int unique = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (antinodes[row][col] > 0) {
unique++;
}
}
}
std::cout << "Unique antinode locations: " << unique << std::endl;
// 1150
return 0;
}